View Full Version : Problems with fprintf() in C
whoelse
June 1st, 2009, 11:11 AM
Hi, I am having some trouble with a C-program of mine. I want to write some chars to a file. On some PCs it is working, on others I have a lot of strange symbols in the file instead (boxes, boxes with points in it, ... - I cannot describe it any better, but the file is much bigger than it should be).
Here is the code responsible for it:
FILE *file_pointer;
char data[DATA_SIZE];
file_pointer=fopen(file_name,"wb");
fprintf(file_pointer, "%s", data);
BTW, the control output in the terminal works fine.
If anybody has an idea, any help would be appreciated.
Thesuperchang
June 1st, 2009, 11:19 AM
If your data is a null terminated string.
FILE *file_pointer;
char data[DATA_SIZE];
file_pointer=fopen(file_name,"w");
if(file_pointer == NULL) return -1; // Error state
fprintf(file_pointer, "%s", data);
Else
FILE *file_pointer;
int errCheck;
char data[DATA_SIZE];
file_pointer=fopen(file_name,"wb");
if(file_pointer == NULL) return -1; // Error state
errCheck = fwrite(data, sizeof(char), DATA_SIZE, file_pointer);
if(errCheck != sizeof(char) * DATA_SIZE) return -1; // Error state
whoelse
June 1st, 2009, 11:56 AM
Seems that I have to use fwrite() instead of fprintf(), thanks for the help.
nvteighen
June 1st, 2009, 03:10 PM
The binary mode in UNIX and Unix-like systems is redundant, being the usage of fread()/fwrite() over fscanf()/fprintf() which defines what kind of data (ASCII or binary) you're reading/writing.
Binary mode means to dump/get a certain memory location to/from disk. You take a variable and copy it exactly into/from the file... that's why you need to take the data's sizeof() in fwrite()/fread().
(Yay, this text is written conforming to both I/O :D)
Can+~
June 1st, 2009, 04:37 PM
The binary mode in UNIX and Unix-like systems is redundant, being the usage of fread()/fwrite() over fscanf()/fprintf() which defines what kind of data (ASCII or binary) you're reading/writing.
Binary mode means to dump/get a certain memory location to/from disk. You take a variable and copy it exactly into/from the file... that's why you need to take the data's sizeof() in fwrite()/fread().
(Yay, this text is written conforming to both I/O :D)
Achievement Unlocked.
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.