fgets

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2009
Posts: 13
Reputation: seemant_sun is an unknown quantity at this point 
Solved Threads: 1
seemant_sun seemant_sun is offline Offline
Newbie Poster

fgets

 
0
  #1
Jun 7th, 2009
"I am a boy\r\n"

if this string is readed through using fgets in an array what will get readed
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: fgets

 
1
  #2
Jun 7th, 2009
All except '\r' and (may be) quotes

Do you really want to study C i/o basics on DaniWeb forum?
Better search for a good C i/o tutorial. There are lots of good links...
Last edited by ArkM; Jun 7th, 2009 at 6:15 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,669
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: fgets

 
0
  #3
Jun 11th, 2009
the answer to this question is not straightforward at all.

fgets will return everything up to and including the newline. But the problem is, that a newline character's ASCII representation is system dependent.

on *nix environments, the newline character ('\n') is typically represented as a single byte, the <LF> or "linefeed" character, ascii 0x0A. on windows and similar platforms, it is typically a double byte, the <CR> "carriage return" plus the <LF> "linefeed", ascii 0x0D and 0x0A.

However, when you add "fgets()", it becomes more complicated and depends on whether you're reading in text mode or binary mode....

when reading a file that was opened in text mode, "fgets" treats all newline characters as just a single linefeed. therefore, whether the newline character is represented as a single 0x0D, or a double 0x0D 0x0A... fgets pulls in only the 0x0A in either case as its newline. any <CR> component is silently discarded!

this makes "fgets()" problematic for binary files.

try to "fgets" the following stream: "I am a boy\n", you will receive 11 bytes: 0x49 20 61 6d 20 61 20 62 6f 79 0A, even though the newline character may be represented as 0x0D 0x0A in the original stream

when you fgets the folllowing stream: "I am a boy\r\n", you will receive 12 bytes: 0x49 20 61 6d 20 61 20 62 6f 79 0D 0A .. the extra byte, 0x0D, is actually from the '\r' character, and not from the '\n' character.

try this out for yourself and see. put in mulitple \r characters for a conclusive demonstration. try also varying the "fopen" to use binary mode, either "wb" or "rb". (also note: '\r' is typically translated as <CR> but even this is not guaranteed by the C standard. )


  1. fptr = fopen("filename.dat","w");
  2. fputs("I am a boy\r\r\r\n",fptr);
  3. fclose(fptr);
  4.  
  5. fptr = fopen("filename.dat","r");
  6. fgets(buffer,sizeof(buffer),fptr);
  7.  
  8. while(buffer[index])
  9. {
  10. printf("%02X ",buffer[index]);
  11. index++;
  12. }
  13. printf("\n");
  14. fclose(fptr);
Last edited by jephthah; Jun 11th, 2009 at 3:24 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,647
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1498
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: fgets

 
0
  #4
Jun 11th, 2009
Originally Posted by jephthah View Post
fgets will return everything up to and including the newline. But the problem is, that a newline character's ASCII representation is system dependent.
Not in memory -- its always '\n' regardless of the operating system. Its just the file systems on physical hard drives that are different from one os to another. When working with text files fgets() will work the same on every operating system that supports that function. So the issues with the file systems isn't really relevant to the OPs question.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,669
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: fgets

 
0
  #5
Jun 11th, 2009
> yes it is correct. - Ancient Dragon
(comment to #3)
wait, so you're saying that fgets() doesn't return the '\r' character in a string?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: fgets

 
0
  #6
Jun 11th, 2009
The new-line character is defined in C, it's '\n'. The fgets standard library function retains a single new-line character from text mode streams only, but...
be careful:
An implementation need not distinguish between text streams and binary streams. In such an implementation, there need be no new-line characters in a text stream nor any limit to the length of a line.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,124
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: fgets

 
0
  #7
Jun 11th, 2009
In other words, test text/binary on your system and write your code accordingly.

And that test is so much faster than posting on a forum and waiting 4 days, 3.5 hours for the only really definitive answer you've received so far.

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 13
Reputation: seemant_sun is an unknown quantity at this point 
Solved Threads: 1
seemant_sun seemant_sun is offline Offline
Newbie Poster

Re: fgets

 
0
  #8
Jun 12th, 2009
Originally Posted by jephthah View Post
the answer to this question is not straightforward at all.

fgets will return everything up to and including the newline. But the problem is, that a newline character's ASCII representation is system dependent.

on *nix environments, the newline character ('\n') is typically represented as a single byte, the <LF> or "linefeed" character, ascii 0x0A. on windows and similar platforms, it is typically a double byte, the <CR> "carriage return" plus the <LF> "linefeed", ascii 0x0D and 0x0A.

However, when you add "fgets()", it becomes more complicated
and depends on whether you're reading in text mode or binary mode....

when reading a file that was opened in text mode, "fgets" treats all newline characters as just a single linefeed. therefore, whether the newline character is represented as a single 0x0D, or a double 0x0D 0x0A... fgets pulls in only the 0x0A in either case as its newline. any <CR> component is silently discarded!

this makes "fgets()" problematic for binary files.

try to "fgets" the following stream: "I am a boy\n", you will receive 11 bytes: 0x49 20 61 6d 20 61 20 62 6f 79 0A, even though the newline character may be represented as 0x0D 0x0A in the original stream

when you fgets the folllowing stream: "I am a boy\r\n", you will receive 12 bytes: 0x49 20 61 6d 20 61 20 62 6f 79 0D 0A .. the extra byte, 0x0D, is actually from the '\r' character, and not from the '\n' character.

try this out for yourself and see. put in mulitple \r characters for a conclusive demonstration. try also varying the "fopen" to use binary mode, either "wb" or "rb". (also note: '\r' is typically translated as <CR> but even this is not guaranteed by the C standard. )


  1. fptr = fopen("filename.dat","w");
  2. fputs("I am a boy\r\r\r\n",fptr);
  3. fclose(fptr);
  4.  
  5. fptr = fopen("filename.dat","r");
  6. fgets(buffer,sizeof(buffer),fptr);
  7.  
  8. while(buffer[index])
  9. {
  10. printf("%02X ",buffer[index]);
  11. index++;
  12. }
  13. printf("\n");
  14. fclose(fptr);







Thanks a lot for extra Knowledge
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 604 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC