Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 01-24-2005, 09:31 PM   #1
Senior Member
Registered: Jul 2003
Location: So. Cal.
Distribution: Slack 11
Posts: 1,737

Rep: Reputation: 46
No new line at end of file? What does this mean?

I am messing around with cpp and I typed up this simple code:
// first C++ program

#include <iostream.h>

int main ()
cout <<" Hello World!";
return 0;

Then I do cpp test.cpp
and lots of stuff flies by on the screen and I get the error: test.cpp:9:2: warning: no newline at end of file.
should I be using something else other than cpp progname.cpp?
Is there a way to do away with this errror?
I am using cpp version 3.3.4

Last edited by BajaNick; 01-24-2005 at 09:35 PM.
Old 01-24-2005, 09:45 PM   #2
Registered: Jul 2003
Location: CT, USA
Distribution: Debian Sarge (server), Etch (work/home)
Posts: 601

Rep: Reputation: 32
> and I get the error: test.cpp:9:2: warning: no newline at end of file.

That's not an error. It's just a warning.

Open the file in an editor, go to the last line of the file, and hit enter to add a blank line to the end of the file. :)

Though, besides that, you should be using #include <iostream> instead of <iostream.h>. Then put in a "using std::cout;" after it.

Last edited by johnMG; 01-24-2005 at 09:48 PM.
1 members found this post helpful.
Old 01-24-2005, 09:57 PM   #3
Senior Member
Registered: Jul 2003
Location: So. Cal.
Distribution: Slack 11
Posts: 1,737

Original Poster
Rep: Reputation: 46
That did it, Thanks alot.
I think some of the online tutorials might be kinda outdated.

Last edited by BajaNick; 01-24-2005 at 10:00 PM.
Old 01-24-2005, 11:04 PM   #4
LQ Guru
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 77
Yeah, I ended up buying a c++ book coz most of the stuff on the web is outdated.
Old 01-25-2005, 12:06 AM   #5
LQ Newbie
Registered: Dec 2004
Location: Wisconsin, USA
Distribution: Debian
Posts: 3

Rep: Reputation: 0
Just out of curiosity, what's the point of having the extra newline at the end of the file? gcc on OS X doesn't give this warning.
Old 01-25-2005, 02:58 AM   #6
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,783

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
It's not an extra new-line, each line should be terminated by an "end of line" tag, and this is \n under unix.
On MacOS, this used to be \r, perhaps the reason why gcc is more tolerant on newer releases of this O/S.
Old 02-07-2005, 04:23 PM   #7
Registered: Aug 2004
Distribution: Mandrake 10.2(Mandriva), SuSE 9.3, Slackware 9.1, Xandros 3.1, Knoppix 3.9, FreeBSD 5.3, RedHat9
Posts: 122

Rep: Reputation: 15
do you know real reason of that?
Old 02-07-2005, 04:37 PM   #8
Registered: Jul 2003
Location: CT, USA
Distribution: Debian Sarge (server), Etch (work/home)
Posts: 601

Rep: Reputation: 32
There's probably still some ancient compilers out there (maybe even very old versions of GCC?) that want each line to end with a newline or else they crash. My guess is that GCC is just trying to help you make your code compatible with these dino's.
Old 02-07-2005, 07:30 PM   #9
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Actually, gcc warns about it because it has to according to the standard. Check out this link for a brief description:

If you have a compiler that doesn't warn you about the lack of a newline character then that compiler doesn't meet the standards. Burn the disc it came on and get a real compiler.
2 members found this post helpful.
Old 05-04-2013, 08:20 PM   #10
Vasileios Keleris
LQ Newbie
Registered: May 2013
Posts: 1

Rep: Reputation: Disabled
Old 05-12-2014, 12:23 AM   #11
LQ Newbie
Registered: May 2014
Posts: 1

Rep: Reputation: Disabled
\n\EOF - it's not just for dino-compilers ;p

I developed some software which had to receive a configuration file via a socket connection... but the software which generated the file did not include a newline at the end - no big deal, right? They had accounted for this, and to indicate 'no more config entries' the last line was "99=0"...

The non-newline EOF was not documented, so I happily tried the following:

Socket socket = new Socket(hostName, portNumber);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

String input;
while(!(input = bufferedReader.readLine()).equals("99=0")) {
	// ... process the input
... which would block while waiting for the elusive newline until the socket connection finally timed out - which took way too long - so I called their technical support and they were at a loss as well... I finally fixed it by swapping out the above while loop with following code:

InputStream input = socket.getInputStream();
String response = "";
byte NEWLINE = (byte) '\n';
boolean getMoreData = true;

while (getMoreData) {
	String temp = "";
	byte subTemp = (byte)0;
	while(subTemp != NEWLINE)
		subTemp = (byte);
	temp += new String(new byte[]{subTemp}, "UTF-8");
		getMoreData = false;
		temp += new String(new byte[]{NEWLINE}, "UTF-8");
	response += temp;

//break out the parts of the response
String[] split = response.split("\n");

for(String inputLine:split){
	// ... process the input

Last edited by cr0ck3t; 05-12-2014 at 12:14 PM. Reason: should not try to modify code in my head and then post without debugging first...
1 members found this post helpful.
Old 05-19-2014, 12:11 PM   #12
Registered: Feb 2011
Distribution: Ubuntu 14.04 LTS (Trusty Tahr)
Posts: 203

Rep: Reputation: 26
I would call this one as solved
Old 05-19-2014, 09:39 PM   #13
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3185Reputation: 3185Reputation: 3185Reputation: 3185Reputation: 3185Reputation: 3185Reputation: 3185Reputation: 3185Reputation: 3185Reputation: 3185Reputation: 3185
Come with me, back to the days of Yore, back to the days which I remember!

(and nay, I do not apologize for those memories ... "sorry you missed it")

In those grand old days of ASCII, there were two characters which controlled the teletype(!) and DECWriter terminals of those days: CR = Carriage Return (Chr(12)) and LF = Line Feed (Chr(10)). Technically, both characters were required by the equipment. However, in actual practice, sometimes it was one, sometimes it was the other, and sometimes it was both (in either order).

And, as time marched on (and computer programmers, as usual, could never quite decide ...) all four possibilities wound up sticking around for the next X decades.

Sorry 'bout that.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
bash programming-append single line to end of file pheasand Linux - General 4 02-28-2014 10:41 AM
C: fread to read a file line by line until the end Blue_muppet Programming 2 09-19-2008 10:42 AM
[SOLVED] delete the end of each line in a file with sed angel115 Linux - Newbie 3 11-16-2005 05:41 PM
read the input file from the specified line no till end suchi_s Programming 5 09-09-2004 05:36 AM
adding new line at end of file airikah Programming 3 11-08-2003 05:27 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:11 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration