LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 01-24-2005, 08:31 PM   #1
BajaNick
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
Thanks.

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

Rep: Reputation: 31
> 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 08:48 PM.
 
1 members found this post helpful.
Old 01-24-2005, 08:57 PM   #3
BajaNick
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 09:00 PM.
 
Old 01-24-2005, 10:04 PM   #4
reddazz
Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 73
Yeah, I ended up buying a c++ book coz most of the stuff on the web is outdated.
 
Old 01-24-2005, 11:06 PM   #5
shadypalm88
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, 01:58 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,481

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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, 03:23 PM   #7
phoenix7
Member
 
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, 03:37 PM   #8
johnMG
Member
 
Registered: Jul 2003
Location: CT, USA
Distribution: Debian Sarge (server), Etch (work/home)
Posts: 601

Rep: Reputation: 31
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, 06:30 PM   #9
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
Actually, gcc warns about it because it has to according to the standard. Check out this link for a brief description: http://gcc.gnu.org/ml/gcc/2003-11/msg01568.html

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, 07:20 PM   #10
Vasileios Keleris
LQ Newbie
 
Registered: May 2013
Posts: 1

Rep: Reputation: Disabled
Great!
 
Old 05-11-2014, 11:23 PM   #11
cr0ck3t
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:

Code:
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:

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)input.read();
	temp += new String(new byte[]{subTemp}, "UTF-8");
	if(("99=0".equals(temp))){
		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 11:14 AM. 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, 11:11 AM   #12
ejspeiro
Member
 
Registered: Feb 2011
Location: San Diego, California (92115), U.S.A.
Distribution: Ubuntu 14.04 LTS (Trusty Tahr)
Posts: 201

Rep: Reputation: 26
I would call this one as solved
 
Old 05-19-2014, 08:39 PM   #13
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,231

Rep: Reputation: 1071Reputation: 1071Reputation: 1071Reputation: 1071Reputation: 1071Reputation: 1071Reputation: 1071Reputation: 1071
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.
 
  


Reply


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 09:41 AM
C: fread to read a file line by line until the end Blue_muppet Programming 2 09-19-2008 09:42 AM
[SOLVED] delete the end of each line in a file with sed angel115 Linux - Newbie 3 11-16-2005 04:41 PM
read the input file from the specified line no till end suchi_s Programming 5 09-09-2004 04:36 AM
adding new line at end of file airikah Programming 3 11-08-2003 04:27 AM


All times are GMT -5. The time now is 10:08 AM.

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