fwrite help needed

E

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.
 
L

Lucien Kennedy-Lamb

ern said:
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
 
E

ern

Lucien said:
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 !
 
D

Default User

ern said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

fread/fwrite 2 18
Understanding fwrite() 10
fwrite problems... 3
Can't solve problems! please Help 0
fwrite() does not write data to file 8
fwrite problems... 6
Segmentation Fault at fwrite... Help 4
URGENT 1

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top