fgets()/strlen unix/DOS portability question

  • Thread starter Walter Dnes (delete the 'z' to get my real address
  • Start date
W

Walter Dnes (delete the 'z' to get my real address

I'm doing some stuff at home that I can't justify doing on work time,
mostly to learn C programming. But it might possibly be useful at work.
I run linux at home at home and Windows at work. One portability
question arises.

Given fgets() on a file...

linux file with a line with 22 columns of data plus CHR(10), strlen()
of the resulting string = 23

DOS file with a line with 22 columns of data plus CHR(13) + CHR(10),
strlen() of the resulting string = 23 ??? (I'm not certain.) Is this
platform-specific or does ANSI C address this situation ?

If absolutely necessary, I suppose my program could...
- open a scratch file
- fprintf(scratch_file, "Hello world.\n")
- close the scratch file
- re-open the scratch file
- gets and take strlen() of the result.
- close the scratch file

And finally use the count of bytes in the string to adjust some
variables (bleagh).
 
R

Richard Bos

"Walter Dnes (delete the 'z' to get my real address)"
Given fgets() on a file...

linux file with a line with 22 columns of data plus CHR(10), strlen()
of the resulting string = 23

DOS file with a line with 22 columns of data plus CHR(13) + CHR(10),
strlen() of the resulting string = 23 ??? (I'm not certain.) Is this
platform-specific or does ANSI C address this situation ?

If you open a file using a text stream (which is the default), C
translates all line endings to a single \n, regardless how it is
represented by the OS. It doesn't matter whether the file on disk
contains \n, \n\r, \r\n, \r, or even byte counts _before_ each line,
when you read a line through a text stream it appears to your program as
a sequence of characters followed by a single \n.
This is, of course, very useful for writing portable programs, since all
unportable, system-specific line-ending and file-padding bits are
handled by the implementation, not by you. Sometimes it is useful to
read raw bytes even for a text file; if you want that, use a binary
stream.

Richard
 
T

those who know me have no need of my name

in comp.lang.c i read:
Sometimes it is useful to read raw bytes even for a text file; if you want
that, use a binary stream.

though doing so is not required to work.
 
W

Walter Dnes (delete the 'z' to get my real address

If you open a file using a text stream (which is the default), C
translates all line endings to a single \n, regardless how it is
represented by the OS.

Thanks for the answer. Also, thanks for the answer I wanted to hear.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top