Skip to main content
gamedev.net
🔒 Locked

Ascii and binary

Started by Austrian Coder 3 replies 300+ views
Original Post
Austrian Coder
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
Craazer
Craazer
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.
Austrian Coder
Austrian Coder
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
BeerNutts
BeerNutts
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)
Austrian Coder
Austrian Coder
I have found the mistake!

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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.