Hamza Ennaciri
Hamza Ennaciri

Reputation: 17

Binary file not found

The program shows no errors or warnings, but the only problem is that I cannot find the ".bin" file where the data is being saved. The C file is saved on my Windows desktop, but still, when I compile it, I can't find the bin file.

#include<stdio.h>
int main(){

FILE *bfile;
int i;

bfile=fopen("bfile.bin","bw");

for(i=2;i<=500;i+=2)
    fwrite(i, sizeof(int),1,bfile);
}

Upvotes: 0

Views: 1029

Answers (1)

Mike Makuch
Mike Makuch

Reputation: 1838

2 problems; 1) the b flag needs to be after the w

fopen("bfile.bin","wb") 

and 2) fwrite takes a pointer, change i to &i

fwrite(&i,sizeof(int),1,bfile);

Actually according the 'man fopen' the b is ignored on POSIX.

Upvotes: 1

Related Questions