Editing a file at Start

Discussion in 'C Programming' started by ritesh, Jul 13, 2006.

  1. ritesh

    ritesh Guest

    Hi,

    I'm facing a problem in which I need to edit an already created file,
    and the editing needs to be done at the start of the file rather then
    appending to the file.

    OS - Linux,Solaris

    For e.g.
    I have a file test.txt created and I have the path to this file in a
    (char *) string.

    ------------------------------ CODE START --------------------------

    char * filePath[128] = getFilePath(); //implementation of this
    function is not relevant
    FILE * filePtr = fopen(filePath, "at"); //open the file in append mode

    /* the filePtr is currently pointing to the end of the file, from where
    onwards I can append data to this file. However I need to insert some
    text at the beginning of this file, so I try this - */

    rewind(filePtr); //just to make sure that the filePtr points to the
    end of the file
    struct stat file;
    stat(filePath, &file);
    long int length = file.st_size; //get the size of the text file
    fseek(filePtr, -length, SEEK_SET); //move backwards by an offset equal
    to the size of the file

    ------------------------------ CODE END --------------------------

    It seems that the last statement above dosen't work as I expected,
    since all writes to the file still appear at the end of the file.

    Also using "struct stat" makes the code non-portable. I know that
    getting the size of a file using the stat calls is not a good way, but
    it works for me right now.

    Isn't there a Standard C function which allows writting to a file at
    any location rather then append only mode. If I open the file using
    the write ("wt") mode then the existing file is truncated and I loose
    all data.

    I can also go for a non-standard/non-portable method that works on
    linux.

    Thanks,
    Ritesh
     
    ritesh, Jul 13, 2006
    #1
    1. Advertising

  2. ritesh

    goose Guest

    ritesh wrote:
    > Hi,
    >
    > I'm facing a problem in which I need to edit an already created file,
    > and the editing needs to be done at the start of the file rather then
    > appending to the file.
    >
    > OS - Linux,Solaris
    >
    > For e.g.
    > I have a file test.txt created and I have the path to this file in a
    > (char *) string.
    >
    > ------------------------------ CODE START --------------------------
    >
    > char * filePath[128] = getFilePath(); //implementation of this
    > function is not relevant


    1. I beg to disagree; does this function return a pointer to
    char? Then you should have

    char *filepath = getFilePath ();

    > FILE * filePtr = fopen(filePath, "at"); //open the file in append mode
    >


    2. You don't check if the file was opened properly or if
    fopen() returned NULL; what in case the file was not found?
    check the return from fopen before using it.

    3. What is "at"? By default, your system should open an input
    file in text mode anyway, but the "t" is not proper C.

    > /* the filePtr is currently pointing to the end of the file, from where
    > onwards I can append data to this file. However I need to insert some
    > text at the beginning of this file, so I try this - */
    >
    > rewind(filePtr); //just to make sure that the filePtr points to the
    > end of the file


    4. This will set the file pointer topthe beginning of the file,
    not the end.

    > struct stat file;
    > stat(filePath, &file);
    > long int length = file.st_size; //get the size of the text file


    5. The above 3 lines are not C. To get the size of a file in C
    try something like this:

    long position;
    fseek (filePtr, 0L, SEEK_END);
    position = ftell (filePtr);

    A better method is to just read in every byte in the file
    and keep a count of the bytes read in.

    > fseek(filePtr, -length, SEEK_SET); //move backwards by an offset equal
    > to the size of the file
    >
    > ------------------------------ CODE END --------------------------
    >
    > It seems that the last statement above dosen't work as I expected,
    > since all writes to the file still appear at the end of the file.


    What did you think "append" means? All writes are done to the end
    of the file.

    > Also using "struct stat" makes the code non-portable. I know that
    > getting the size of a file using the stat calls is not a good way, but
    > it works for me right now.


    If time is not an issue, read the file in byte by byte to get
    the file size.

    >
    > Isn't there a Standard C function which allows writting to a file at
    > any location rather then append only mode.


    No. Further, I don't know of any /non-standard/ function either.

    > If I open the file using
    > the write ("wt") mode then the existing file is truncated and I loose
    > all data.


    You "lose" the data, not "loose" the data[1]. What I suggest
    is this:

    1. Read the file in a line (or a char) at a time *until*
    you get to the place you want to insert/modify data.
    Store all the lines/chars you read in to a different
    temporary file.
    2. At that point write *your* changes to the temporary
    file.
    3. Continue reading from file and storing in temporary file
    until no more data.
    4. Rename the temporary file to the original filename.

    TADA! your changes are done without you even needing the
    filesize *and* your code is portable.

    >
    > I can also go for a non-standard/non-portable method that works on
    > linux.
    >



    note:
    [1] Sorry but this annoys mean intensely, but I did try
    to reduce the pain of inflammatory remarks by trying to
    help :)


    goose,
     
    goose, Jul 13, 2006
    #2
    1. Advertising

  3. ritesh

    Richard Bos Guest

    "goose" <> wrote:

    > ritesh wrote:
    > > I'm facing a problem in which I need to edit an already created file,
    > > and the editing needs to be done at the start of the file rather then
    > > appending to the file.


    Read the FAQ: <http://www.c-faq.com/stdio/fupdate.html>. (In short, it
    may not be possible for text streams, and may not be as straightforward
    as you think it is for binary streams.)

    > > struct stat file;
    > > stat(filePath, &file);
    > > long int length = file.st_size; //get the size of the text file

    >
    > 5. The above 3 lines are not C. To get the size of a file in C
    > try something like this:
    >
    > long position;
    > fseek (filePtr, 0L, SEEK_END);
    > position = ftell (filePtr);


    Read the FAQ: this is not required to work for either binary _or_ text
    streams. <http://www.c-faq.com/osdep/filesize.html>.

    Richard
     
    Richard Bos, Jul 13, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lucas Tam
    Replies:
    0
    Views:
    535
    Lucas Tam
    Jun 17, 2005
  2. Ashe Sjedic

    formview- start out editing

    Ashe Sjedic, Feb 1, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    1,458
    Christopher Reed
    Feb 4, 2006
  3. Anup
    Replies:
    1
    Views:
    2,828
    Mark Rae
    May 9, 2006
  4. Noam Raphael
    Replies:
    17
    Views:
    543
    Noam Raphael
    May 26, 2004
  5. Replies:
    0
    Views:
    612
Loading...

Share This Page