odd issue with fopen (wb) and overwriting files

This is a discussion on odd issue with fopen (wb) and overwriting files within the C Programming forums, part of the General Programming Boards category; I had this issue a while ago, but I'm working on something new and I don't want to run into ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    161

    odd issue with fopen ("wb") and overwriting files

    I had this issue a while ago, but I'm working on something new and I don't want to run into the same thing. Here's the problem. I was using fopen(filename, "wb") to overwrite a file that already exists, somtimes with a smaller amount of data (hence, smaller file), and it left garbage on the end of the file. Say the existing file was 100 bytes and I only wrote 10. It overwrote the first 10 bytes and the other 90 bytes were still there. So it seems it didn't really kill the existing file like I wanted. Any ideas? Am I missing something with the "wb" in fopen()?

    "w" Create an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.
    Last edited by Viper187; 06-06-2008 at 10:43 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Post the code you are using.

    You are probably doing something else wrong, but I can't say for sure.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    161
    Would any kind of error occur if the file is in use by something else to prevent it from being erased while allowing it to be appended?

    I believe this was the code from my old program that gave me issues with the file not being cleared of the old data.
    Code:
        FILE *rFile;
        rFile = fopen(NewResFile,"wb");
        if (!(rFile)) { Free........(); return 4; }
        fwrite (hData , 1 , nfoheader , rFile);
        fwrite (Results , 1 , (filesize / ioinc / 8) , rFile);
        fclose(rFile);
    The issue may actually have been with data staying in other parts of the file. I never figured out why I was getting garbage. I changed it to this later though.
    Code:
        FILE *rFile;
        rFile = fopen(NewResFile,"wb");
        if (!(rFile)) { Free........(); return 4; }
            int rsize = (filesize / ioinc / 8) + nfoheader;
            u8 *tmpbuff;
            if (!(tmpbuff = (unsigned char*)malloc(rsize))) { Free........(); return 1; }
            
            memset (tmpbuff,0,rsize);
            fwrite (tmpbuff, 1, rsize , rFile);
            free(tmpbuff); tmpbuff=0;
            fseek(rFile,0,SEEK_SET);
        fwrite (hData , 1 , nfoheader , rFile);
        fwrite (Results , 1 , (filesize / ioinc / 8) , rFile);
        fclose(rFile);
    Last edited by Viper187; 06-06-2008 at 11:55 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you successfully open a file with fopen(..., "wb") [meaning a FILE * with a value not equal to NULL comes back] then you should have an empty file [1]. Try that, and comment out your fwrite that comes after, just close the file. If this doesn't produce an empty file, then you are definitely doing something wrong.

    Edit: If you are still having problems, please post a COMPLETE compilable program that shows the problem, as your code is far from complete, and thus very difficult to test without adding more code.

    [1] Subject to the C runtime being correct - which if you are using a common, standard implementation of C it is very likely to be correct (99.99% sure). If you or someone else wrote his/her own C runtime library, then anything is possible.

    --
    Mats
    Last edited by matsp; 06-06-2008 at 12:13 PM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21