Jump to content
Tuts 4 You

fopen & fwrite Line Breaks Macintosh issue


LCF-AT

Recommended Posts

Posted

Hi guys,

I'am using those C functions above to read / write data to file and have that problem with the Macintosh CR (see notepate status bar).In normal case I write text fields like this...

db "Some Info", 13,10
db "Here too", 13,10
db "Look not!",0

....just 3 test lins with one line break between.Now if I do use the fwrite function with it then it does write the LBs in double LBs like this...

db "Some Info", 13,10,13,10
db "Here too", 13,10,13,10
db "Look not!",0

....and not in Unix style.How can I tell the function using Unix style instead of Macintosh?

greetz

Posted

First, you're mixing up line endings:

  • DOS/Windows line endings are CR+LF (in decimal 13, 10)
  • Linux/Unix line endings is LF (10)
  • Macintosh line ending is CR (13)

So, which ones do you have and which ones do you want?

 

Second - fread()/fwrite() function works differently, depending on what flags you pass to fopen(). If you open file as text (default), fread/fwrite will change line endings. If you don't want your line endings to change, just open the file with "b" flag.
Something like this:

pFile = fopen("myfile.txt","wb");
fwrite(pFile, something....)

As always, MSDN explains it in more details:
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/fopen-wfopen?view=msvc-160#generic-text-routine-mappings
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/fwrite?view=msvc-160#remarks
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/fread?view=msvc-160#remarks

 

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...