By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,814 Members | 1,678 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,814 IT Pros & Developers. It's quick & easy.

fwrite help needed

P: n/a
ern
I'm writing to a file called "output.txt" It *should* contain floating
point numbers delimited by semi-colons. Instead, it contains Chinese
characters.

/* HERE IS MY CODE */

FILE *fileHandle;
const char * filePath = "C:\\output.txt";
const char delimiter[1] = {';'};
char power_string[25];
float power_number = 25.53;

if( (fileHandle = fopen( filePath, "w" )) == NULL ){
printf( "The file %s could not be opened\n",filePath );
exit(0);
}else{ printf( "The file %s was opened\n",filePath );}
_gcvt(power_number,4,power_string); //convert to
string...
fwrite( power_string,sizeof(char),25,fileHandle ); //write power to
file
fwrite( delimiter,sizeof(char),2,fileHandle );//write delimiter to
file
fclose(fileHandle);

I saw in the debugger that the _gcvt command was doing it's job well.
I think the problem is my usage of fwrite. I thought it was my
delimiter, but the problem persists when I omit that line. Help is
much appreciated.

Feb 3 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

ern wrote:
I'm writing to a file called "output.txt" It *should* contain floating
point numbers delimited by semi-colons. Instead, it contains Chinese
characters.

/* HERE IS MY CODE */

FILE *fileHandle;
const char * filePath = "C:\\output.txt";
const char delimiter[1] = {';'};
char power_string[25];
float power_number = 25.53;

if( (fileHandle = fopen( filePath, "w" )) == NULL ){
printf( "The file %s could not be opened\n",filePath );
exit(0);
}else{ printf( "The file %s was opened\n",filePath );}
_gcvt(power_number,4,power_string); //convert to
string...
fwrite( power_string,sizeof(char),25,fileHandle ); //write power to
file
fwrite( delimiter,sizeof(char),2,fileHandle );//write delimiter to
file
fclose(fileHandle);

I saw in the debugger that the _gcvt command was doing it's job well.
I think the problem is my usage of fwrite. I thought it was my
delimiter, but the problem persists when I omit that line. Help is
much appreciated.


Hi Ern,

When using fwrite() you are using byte lengths of 25 for the
power_string and 2 for the delimeter. Here I feel is your problem.

The string won't be 25 bytes long as _gcvt() won't fill all all bytes
beyond the null terminator it appends to the converted string. Those
bytes will be filled with 'junk'.

The delimeter you already specify as being 1 byte long.

Try swapping out both fwrite() calls with the single call:

fprintf(fileHandle, "%.4f;", power_number);

You wont need any intermediate buffers at all!

Good luck.

Lucien Kennedy-Lamb

Feb 3 '06 #2

P: n/a
ern

Lucien Kennedy-Lamb wrote:
Try swapping out both fwrite() calls with the single call:

fprintf(fileHandle, "%.4f;", power_number);


Thanks ! Works like a charm. How would the command change if I want
to use a tab '\t' delimiter instead of ';'... Thanks again !

Feb 3 '06 #3

P: n/a
ern wrote:

Lucien Kennedy-Lamb wrote:
Try swapping out both fwrite() calls with the single call:

fprintf(fileHandle, "%.4f;", power_number);


Thanks ! Works like a charm. How would the command change if I want
to use a tab '\t' delimiter instead of ';'... Thanks again !


If you had to guess, what would *you* say?

Brian
Feb 4 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.