Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fprintf isn't writing to file

Tags:

c

printf

I am trying to use the following C code to print out an array that I have passed in. It should output the text in hexadecimal format one on each line and I have no problems opening the file. When I first wrote it, I had no problems with it working I opened the output file and my array was there. I changed the fileOutName parameter and now I can't get it to print out anything I have tried changing it back and to a few other things and nothing seems to work. Also when I debug it seems like pOutfile is a bad pointer, but like I said it still creates the file it just won't write anything in it. Any help would be appreciated. Thanks

printoutput(int output[], char * fileOutName){
    int i = 0;
    FILE * pOutfile;
    pOutfile = fopen( fileOutName, "w" );
    while(output[i] != 0){
        fprintf( pOutfile, "0x%0.4X\n", output[i] );
        i++;
    }
}
like image 311
farnett Avatar asked Nov 06 '25 19:11

farnett


2 Answers

Always clean up after yourself. You're missing an fclose(pOutfile).

like image 149
Kevin Avatar answered Nov 09 '25 09:11

Kevin


It should output the text in hexadecimal format one on each line ...

This line

fprintf( pOutfile, "0x%0.4X\n", 5 );

always formats the same number - 5. It probably should be

fprintf( pOutfile, "0x%0.4X\n", output[i] );
like image 37
Sergey Kalinichenko Avatar answered Nov 09 '25 10:11

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!