Thread: truncate a file

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    7
    Rep Power
    0

    truncate a file


    I want to truncate a file starting from some point of it.

    The code looks as follows:

    FILE *f;
    f=fopen("name","r+");
    fseek(f,50,SEEK_SET); // for example 50 bytes from the start
    // ???????? // how to trim it after setting the
    // file pointer?
    fclose(f);

    Maybe there is a way of putting an EOF character at this point, or truncating it some other way?

    Please help, thanks.
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    India
    Posts
    9
    Rep Power
    0
    dont know if putting EOF at that place will work (though if it works
    then it's a better way) but there is another way...read 50 bytes
    from start into a buffer ,close the file ,reopen it with "w+" &
    write that buffer back into file...

    HTH
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    7
    Rep Power
    0
    It works this way, but it is not exactly what i need. I need to hash some file before, than add to this file hash value. Then I must decompose the whole file, take the hash again and compare.

    The problem is that the file structure must be identical to the original. And while reading and writing the buffer to a string, carriage return characters are not maintained, and the documents differ slightly.

    Although the file compare function doesn't see the difference, the hashing function returns different results.

    So, I need to trim the file, not to rewrite it.
    But thanks anyway

    Maybe someone knows the solution???

IMN logo majestic logo threadwatch logo seochat tools logo