Appending to files and getting file size

This is a discussion on Appending to files and getting file size within the C Programming forums, part of the General Programming Boards category; I have a function that saves to a file. If it is set to append I will need to check ...

  1. #1
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218

    Appending to files and getting file size

    I have a function that saves to a file. If it is set to append I will need to check if the file has any data in it; if not then it will need to write the header info out. My problem is, how would I get the filesize? If i open it to append wouldent the file pointer would be set to the end of the file?

    Edit: Actually it would also have to check the first four bytes of the file to make sure its the valid file format as well.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Yes. Try ftell() or (for C99) fgetpos(). It should return the position in the file, which should be nonzero if the file has some data in it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Ok cool so ftell will work when appending.

    Cheers.

    Edit: I'm compiling as C99 so should I use fgetpos() instead?
    Last edited by mike_g; 07-24-2008 at 12:27 PM.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Yeah, I think it would. I've never tried it.

    Edit: Actually it would also have to check the first four bytes of the file to make sure its the valid file format as well.
    Sounds like you want the "a+" fopen mode. It lets you read from the file and append to it too. Just make sure you fflush()/rewind() the file before switching from reading to writing or vise versa.

    Ah, what do I know. http://msdn.microsoft.com/en-us/libr...0b(VS.71).aspx
    Note that when a file is opened for appending data, the current file position is determined by the last I/O operation, not by where the next write would occur. For example, if a file is opened for an append and the last operation was a read, the file position is the point where the next read operation would start, not where the next write would start. (When a file is opened for appending, the file position is moved to end of file before any write operation.) If no I/O operation has yet occurred on a file opened for appending, the file position is the beginning of the file.
    So I'd open the file for a+ mode, fseek() to the end of the file, and ftell() from there. Or you could just fgets() the first few bytes and see if it's a valid file.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    So I'd open the file for a+ mode, fseek() to the end of the file, and ftell() from there. Or you could just fgets() the first few bytes and see if it's a valid file.
    Sounds good to me. I'll do that

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