views:

60

answers:

2

I'm using MSVC++ to program a simple window, and I included a resource file for a menu along the lines of:

MYMENU MENU DISCARDABLE
//etc.

and I created a header file "resourcedef.h" with definitions like

#define ID_MYMENU_FILE_CLOSE 1002

I can include it in my main.cpp file without error, however, when I include it in the resource file, I get the error

.\resourcedef.h(9) : fatal error RC1004: unexpected end of file found

(resourcedef.h is exactly 9 lines long). When I add a newline the the end of the .h,

//lines 1 - 8
#define ID_MYMENU_FILE_OPEN 1001

So that there is a tenth blank line (that doesn't appear on SO), it compiles fine. If I put anything on the tenth line, even a comment, the compiler gives me an error. Does anyone know what causes this and how I can fix it?

+2  A: 

It's a bad idea to omit the trailing newline at the end of a text file - there are many tools that will fail to work properly unless it's there. (Some text editors will even warn you that's it's missing.)

Trailing newlines are standard behaviour for text files - why not go with the flow and just put one there. Everybody else does. 8-)

RichieHindle
thanx. didn't know if it was normal or not. apparrently it is :)
Keand64
IIRC, some early implementations of **SCCS** couldn't handle files without trailing newlines. And it could even screw up the repository for that file so that the admin would have to manually edit to fix it...
NVRAM
A: 

Long ago, MSVC++ at version 2 or so (1994-ish) would not handle "partial" lines at the end of the file properly. For example:

header.h

#ifndef __HEADER_H
#define __HEADER_H

void foo();

#endif        // <-- no newline here

main.cpp

#include "header.h"
#include "other.h"

In this situation, the preprocessor would see the following text after resolving includes:

#ifndef __HEADER_H
#define __HEADER_H

void foo();

#endif#include "other.h"

The bug was that the last line would run together as shown above, and the "other.h" file would not be included. With the IDE at that time, it was easy to accidentally create a file with no trailing newline and run into the above situation.

Fortunately, this bug has long since been fixed. It looks like you've found one of the situations where the compiler warns that it doesn't expect to encounter the end of the file in the middle of a line. This is generally regarded as a good thing.

Greg Hewgill