Binary Convert

I have ben scratching my head at this code for the last 2 days now and cant figure out what i have done wrong! i am trying to get the code to read from the txt file one bite at a time and then write this bite into the binary file but i cant seem to get it working. Please help me!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
FILE *fpcust, *fpcustbin;      //<<<<<-----point to both sales and customers text files, and the new .bin files for both
    char buffer;
    int ch;
    int ch1;
    
    fpcust = fopen("c:\\customers.txt", "r");     //<<<<-----pointing to the file
    fpcustbin = fopen("c:\\customers.bin", "wb");    //<<<<<-----pointing to the new binary file, opening in writing binary 
    
    while ( ( ch = fgetc ( fpcust ) ) != EOF )
    {
        fread(buffer, 1, 1, fpcust);     //<<<----these two lines are copying the text in the first file and pasting in binary into the new binary file while not = EOF
        fwrite(buffer, 1, 1, fpcustbin);
    }
    
    fclose(fpcust);      //<<<<-----closing the text file....may have to change to file pointer
    fclose(fpcustbin);    //<<<<------closing the binary file ....may have to change to file pointer 
for fread and fwrite you need to pass a pointer to the buffer as the first parameter (currently you're passing only the char):
1
2
        fread(&buffer, 1, 1, fpcust);     // Note the address operator &
        fwrite(&buffer, 1, 1, fpcustbin);     // Note the address operator & 


See
http://cplusplus.com/reference/cstdio/fread/?kw=fread
http://cplusplus.com/reference/cstdio/fwrite/?kw=fwrite
is that the way the code should be with the &buffer? thanks for reply by the way
coder777 i got the code to work!! thanks so much for the hep!!
Last edited on
Topic archived. No new replies allowed.