I am using MinGW in Windows 8.1 and I have an input text file of raw numbers (one per line) and I want to write them as binary in a new binary file. The example is compiling without problems with:
gcc -pedantic -Os -c my_code.c -o my_code.exe
but the output is
$ my_code.exe
sh: ./my_code.exe: Bad file number
This is the code I wrote:
#include<stdio.h>
int main ()
{
FILE *fp;
FILE *prob;
int length;
char buffer[30];
// Open file containing numbers
if ((prob = fopen("raw_numbers.txt","r")) == NULL)
{
printf("Could not open raw_numbers.txt\n");
exit(1);
}
/* Create output binary file */
fp = fopen( "file.bin" , "w" );
while( ! feof(prob) )
{
fgets(buffer, sizeof(buffer), prob);
fwrite((const void*) & buffer, 1, sizeof(buffer), fp);
}
fclose(prob);
fclose(fp);
return(0);
}
Using
$ gcc --version
gcc (GCC) 3.4.4 (msys special)