LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 12-18-2005, 05:17 PM   #1
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
EOF and \n


Ok this is confusing me but its probably a simple thing.
in the below code is a program that simulates the cat program.

Now why when running the program does it create a newline char even tho one is not put in when the printing of all the charecters in the file is compleate?

Code:
 
#include<stdio.h> 
#include<stdlib.h> 


int main(int argc, char *argv[]) 
{ 
     FILE *my_file; 
      
     char test;   //charecter to test for EOF and printing. 
 
     
    if (argv[1] == NULL) 
       { 
          printf("Usage is mycat <text file> at this time there are no options\n"); 
          return 0; 
       } 
    my_file = fopen(argv[1],"r"); 
    
    if (my_file == NULL)
       { 
         printf("Error opening file\n");
         return 0; 
       } 
    
   
    do 
       { 
         test = fgetc(my_file); 
         if ( test != EOF) 
               printf("%c",test); 
         } 
    while( test != EOF); 
    fclose(my_file); 
    
    
    return 0; 
}

the file test contains the following text
Code:
cat cat cat mouse
ok so no return in that line of txt "NOTE: when posting on the forem its adding line below the text but this isnt present in the actual file.

here is the program output

Code:
 
user@linux$ ./mycat test
cat cat cat mouse
user@linux$ 
user@linux$ cat test 
cat cat cat mouse
user@linux$

Last edited by exvor; 12-18-2005 at 05:19 PM.
 
Old 12-18-2005, 06:35 PM   #2
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
Your editor inserted the newline without your knowledge:
Code:
cat -A test_file
The output will be
Quote:
cat cat cat mouse$
The "$" indicates a newline.
 
Old 12-23-2005, 12:26 AM   #3
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
What exactly is adding the newline charecter? Is it when the fopen function cals the file and inserts it into the data stream that i give it or is it somewhere else in the code? Not that its causeing an issue I just noticed it when I was messign around. I actually wrote in a newline charecter to be printed when the EOF was reached and saw that 2 of them were being added that is when I started wondering about this.
 
Old 12-23-2005, 09:06 AM   #4
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
Depending on how you created the file, the newline was probably added by the editor you used. If you don't want the newline, you can do something like:
Code:
echo -n cat cat cat mouse > test_file
Now, if you do a
Code:
cat -A test_file
you should see
Quote:
cat cat cat mouse
Note the lack of a newline.

Last edited by Berhanie; 12-23-2005 at 09:08 AM.
 
Old 12-27-2005, 02:20 PM   #5
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
Ohh your right lol. Silly editors.
 
  


Reply



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
EOF finding Goblin_C_Noob Programming 7 09-10-2005 09:58 AM
finding the eof nodger Programming 12 11-26-2004 02:17 AM
Premature EOF? miknight Programming 1 04-04-2004 12:19 AM
eof ... c ... linux xviddivxoggmp3 Programming 6 03-31-2004 12:56 PM
<< Eof ? sikandar Linux - Software 5 09-18-2003 11:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:14 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration