Ascii and binary

Started by
3 comments, last by Austrian Coder 20 years, 11 months ago
Hi! I have here a littel problem. If i open an acsii file (*.txt) in binary mode via fopen and read the whole file in a buffer and write it back in binary mode then the output file is in binary. How can i avoid this? Which files must be open in ascii mode? Thanks, Christian
Advertisement
quote:Original post by Austrian Coder
Hi!

I have here a littel problem. If i open an acsii file (*.txt) in binary mode via fopen and read the whole file in a buffer and write it back in binary mode then the output file is in binary. How can i avoid this? Which files must be open in ascii mode?

Thanks, Christian


You should show the code. Or atleast tell what functions you are using.
Ok... here is my code:

FILE* s1;
unsigned int Size1;

char* buffer1;

// try to open 2 source files
if ((s1 = fopen("In.txt", "rb")) != 0)
{
// obtain file size.
fseek (s1 , 0 , SEEK_END);
Size1 = ftell (s1);
rewind (s1);

buffer1 = (char*) malloc (Size1);
fread (buffer1, 1, Size1, s1);

fclose(s1);
}

if ((s1 = fopen("Out.txt", "wt")) != 0)
{
fwrite(&buffer1, 1, Size1, s1);
fclose(s1);
}

Christian
Just write it back as binary, and it''ll write back the exact same thing. Mixing binary and ascii isn''t wise.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

I have found the mistake!

fwrite(&buffer1, 1, Size1, s1);
should be
fwrite(buffer1, 1, Size1, s1);

This topic is closed to new replies.

Advertisement