How to delete a line or a character?

Discussion in 'C Programming' started by Dong Ge, Oct 4, 2004.

  1. Dong Ge

    Dong Ge Guest

    Hi!
    I am a beginner of C.

    I want to delete some characters or a whole line in a text file. I
    have tried the "fprintf", "fputs", "fwrite", but no one of them can
    run rightly.

    In the below codes, the "str1" is one text line , in fact. I want
    replace "str1" (the whole line) with the "str3". In my case, "str3" is
    shooter than "str1", But I will get some strange code that can not be
    read.

    Thanks in advance!

    if ((cfPtr = fopen("client.dat","r+")) == NULL)
    printf ("File counld not be opened. \n");
    else
    {
    while (!feof(cfPtr))
    {fgets(str1, 200, cfPtr);
    printf ("OK!");
    if (strstr(str1, str2))
    printf ("NO!");
    {fseek (cfPtr, ftell(cfPtr)-strlen(str1)-1,
    SEEK_SET);
    fwrite (str3, strlen(str1), 1, cfPtr);
    exit();
    }
    }
    fclose(cfPtr);
    }

    --
    Dong Ge
    2004-10-04
     
    Dong Ge, Oct 4, 2004
    #1
    1. Advertising

  2. Dong Ge wrote:
    > Hi!
    > I am a beginner of C.
    >
    > I want to delete some characters or a whole line in a text file. I
    > have tried the "fprintf", "fputs", "fwrite", but no one of them can
    > run rightly.
    >
    > In the below codes, the "str1" is one text line , in fact. I want
    > replace "str1" (the whole line) with the "str3". In my case, "str3" is
    > shooter than "str1", But I will get some strange code that can not be
    > read.
    >
    > Thanks in advance!
    >
    > if ((cfPtr = fopen("client.dat","r+")) == NULL)


    You use cfPts to open a file.

    > printf ("File counld not be opened. \n");
    > else
    > {
    > while (!feof(cfPtr))
    > {fgets(str1, 200, cfPtr);
    > printf ("OK!");
    > if (strstr(str1, str2))
    > printf ("NO!");
    > {fseek (cfPtr, ftell(cfPtr)-strlen(str1)-1,
    > SEEK_SET);
    > fwrite (str3, strlen(str1), 1, cfPtr);


    You are using the same handle to write again ?
    Are you sure you wanna do this ?
    May be, you want to read the contents of the file to a buffer
    and then write again.

    You have not mentioned how you are opening the file !


    > exit();


    After writing, you are exiting. Well - you are not closing the file.
    That would imply that:

    * There is no guarantee that the file contents are going to be written.


    * <OT>
    whatever the lock acquired by the underlying OS over the file
    resource may not be necessary given back. But I think this depends on
    the implementation, but nevertheless this is something you have to pay
    heed to.
    </OT>.

    --
    Karthik.
    http://akktech.blogspot.com .
     
    Karthik Kumar, Oct 4, 2004
    #2
    1. Advertising

  3. Dong Ge

    Dong Ge Guest

    Hi! Karthik,

    I guess your maybe not reading my codes carefully. I use the same
    handle "cfPtr" to open, write and close the same file, not "cfPts". In
    fact, the code can be compiled and run.

    I just want to know how to delete a text line or some characters.

    Thanks for your reply.
    --
    Dong Ge
    2004-10-05



    On Mon, 04 Oct 2004 14:46:52 -0700, Karthik Kumar
    <> wrote:

    >Dong Ge wrote:
    >> Hi!
    >> I am a beginner of C.
    >>
    >> I want to delete some characters or a whole line in a text file. I
    >> have tried the "fprintf", "fputs", "fwrite", but no one of them can
    >> run rightly.
    >>
    >> In the below codes, the "str1" is one text line , in fact. I want
    >> replace "str1" (the whole line) with the "str3". In my case, "str3" is
    >> shooter than "str1", But I will get some strange code that can not be
    >> read.
    >>
    >> Thanks in advance!
    >>
    >> if ((cfPtr = fopen("client.dat","r+")) == NULL)

    >
    > You use cfPts to open a file.
    >
    >> printf ("File counld not be opened. \n");
    >> else
    >> {
    >> while (!feof(cfPtr))
    >> {fgets(str1, 200, cfPtr);
    >> printf ("OK!");
    >> if (strstr(str1, str2))
    >> printf ("NO!");
    >> {fseek (cfPtr, ftell(cfPtr)-strlen(str1)-1,
    >> SEEK_SET);
    >> fwrite (str3, strlen(str1), 1, cfPtr);

    >
    > You are using the same handle to write again ?
    >Are you sure you wanna do this ?
    > May be, you want to read the contents of the file to a buffer
    >and then write again.
    >
    > You have not mentioned how you are opening the file !
    >
    >
    >> exit();

    >
    > After writing, you are exiting. Well - you are not closing the file.
    >That would imply that:
    >
    >* There is no guarantee that the file contents are going to be written.
    >
    >
    >* <OT>
    > whatever the lock acquired by the underlying OS over the file
    >resource may not be necessary given back. But I think this depends on
    >the implementation, but nevertheless this is something you have to pay
    >heed to.
    > </OT>.
     
    Dong Ge, Oct 5, 2004
    #3
  4. Dong Ge

    Chris Torek Guest

    In article <news:>
    Dong Ge <> wrote:
    > while (!feof(cfPtr))


    Bug here, virtually guaranteed. If you ever see "while (!feof(...))"
    in a C program, look for the bug. There will be one, at least 99%
    of the time.

    Enormous hint: see the comp.lang.c FAQ, question 12.2.

    For the answer to the question in the "Subject:" line, see the
    comp.lang.c FAQ as well.
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
     
    Chris Torek, Oct 5, 2004
    #4
  5. Dong Ge wrote:
    > Hi! Karthik,
    >
    > I guess your maybe not reading my codes carefully. I use the same
    > handle "cfPtr" to open, write and close the same file, not "cfPts". In
    > fact, the code can be compiled and run.


    Oops - Sorry. That was a typo. But my question remains the same -
    how could you use the same handle to read and write .

    >
    > I just want to know how to delete a text line or some characters.



    Read the contents of the file.
    Close the file.

    Apply the filter on the contents that you have read in step 1.
    Open a file and write the contents.


    >
    > Thanks for your reply.
    > --
    > Dong Ge
    > 2004-10-05
    >


    And please don't top-post.

    --
    Karthik.
    http://akktech.blogspot.com .
    ------------ And now a word from our sponsor ------------------
    For a quality usenet news server, try DNEWS, easy to install,
    fast, efficient and reliable. For home servers or carrier class
    installations with millions of users it will allow you to grow!
    ---- See http://netwinsite.com/sponsor/sponsor_dnews.htm ----
     
    Karthik Kumar, Oct 5, 2004
    #5
  6. Karthik Kumar wrote:
    > Dong Ge wrote:
    >
    >> Hi! Karthik,
    >>
    >> I guess your maybe not reading my codes carefully. I use the same
    >> handle "cfPtr" to open, write and close the same file, not "cfPts". In
    >> fact, the code can be compiled and run.

    >
    >
    > Oops - Sorry. That was a typo. But my question remains the same - how
    > could you use the same handle to read and write .
    >
    >>
    >> I just want to know how to delete a text line or some characters.

    >
    >
    >
    > Read the contents of the file.
    > Close the file.
    >
    > Apply the filter on the contents that you have read in step 1.
    > Open a file and write the contents.


    To add further,

    remove the original file.
    rename the new file to the original file.

    --
    Karthik.
    http://akktech.blogspot.com .
     
    Karthik Kumar, Oct 5, 2004
    #6
  7. Dong Ge

    Dong Ge Guest

    In the comp.lang.c FAQ :

    Question 19.14:
    How can I insert or delete a line (or record) in the middle of a file?
    --------------------------------------------------------------------------------

    Short of rewriting the file, you probably can't. The usual solution is
    simply to rewrite the file. (Instead of deleting records, you might
    consider simply marking them as deleted, to avoid rewriting.) See also
    questions 12.30 and 19.13.

    Question 12.30
    I'm trying to update a file in place, by using fopen mode "r+",
    reading a certain string, and writing back a modified string, but it's
    not working.
    --------------------------------------------------------------------------------

    Be sure to call fseek before you write, both to seek back to the
    beginning of the string you're trying to overwrite, and because an
    fseek or fflush is always required between reading and writing in the
    read/write "+" modes. Also, remember that you can only overwrite
    characters with the same number of replacement characters; see also
    question 19.14.

    " ALSO, REMEMBER THAT YOU CAN ONLY OVERWRITE CHARACTERS WITH THE SAME
    NUMBER OF REPLACEMENT CHARACTERS."
    This means I can't write my code with the standdard lib?

    ----
    Dong Ge
    2004-10-05



    On Tue, 05 Oct 2004 12:57:36 +0800, Dong Ge <> wrote:

    >Hi! Karthik,
    >
    >I guess your maybe not reading my codes carefully. I use the same
    >handle "cfPtr" to open, write and close the same file, not "cfPts". In
    >fact, the code can be compiled and run.
    >
    >I just want to know how to delete a text line or some characters.
    >
    >Thanks for your reply.
     
    Dong Ge, Oct 5, 2004
    #7
  8. Dong Ge

    Dong Ge Guest

    Hi! Karthik,
    The orginal text file contained thousands of lines, if I always remove
    the original file and rename the new file. Maybe the program will run
    very slowly.

    Tons of thanks for your help!
    --
    Dong Ge
    2004-10-05

    On Mon, 04 Oct 2004 22:09:01 -0700, Karthik Kumar
    <> wrote:

    >Karthik Kumar wrote:
    >> Dong Ge wrote:
    >>
    >>> Hi! Karthik,
    >>>
    >>> I guess your maybe not reading my codes carefully. I use the same
    >>> handle "cfPtr" to open, write and close the same file, not "cfPts". In
    >>> fact, the code can be compiled and run.

    >>
    >>
    >> Oops - Sorry. That was a typo. But my question remains the same - how
    >> could you use the same handle to read and write .
    >>
    >>>
    >>> I just want to know how to delete a text line or some characters.

    >>
    >>
    >>
    >> Read the contents of the file.
    >> Close the file.
    >>
    >> Apply the filter on the contents that you have read in step 1.
    >> Open a file and write the contents.

    >
    > To add further,
    >
    > remove the original file.
    > rename the new file to the original file.
     
    Dong Ge, Oct 5, 2004
    #8
  9. Dong Ge

    Jason Curl Guest

    Dong Ge wrote:
    > In the comp.lang.c FAQ :
    >
    > Question 19.14:
    > How can I insert or delete a line (or record) in the middle of a file?
    > --------------------------------------------------------------------------------
    >
    > Short of rewriting the file, you probably can't. The usual solution is
    > simply to rewrite the file. (Instead of deleting records, you might
    > consider simply marking them as deleted, to avoid rewriting.) See also
    > questions 12.30 and 19.13.
    >
    > Question 12.30
    > I'm trying to update a file in place, by using fopen mode "r+",
    > reading a certain string, and writing back a modified string, but it's
    > not working.
    > --------------------------------------------------------------------------------
    >
    > Be sure to call fseek before you write, both to seek back to the
    > beginning of the string you're trying to overwrite, and because an
    > fseek or fflush is always required between reading and writing in the
    > read/write "+" modes. Also, remember that you can only overwrite
    > characters with the same number of replacement characters; see also
    > question 19.14.
    >
    > " ALSO, REMEMBER THAT YOU CAN ONLY OVERWRITE CHARACTERS WITH THE SAME
    > NUMBER OF REPLACEMENT CHARACTERS."
    > This means I can't write my code with the standdard lib?

    Of course you could try padding with spaces if that is compatible with
    how you read the file again at a later time.
    >
    > ----
    > Dong Ge
    > 2004-10-05
    >
    >
    >
    > On Tue, 05 Oct 2004 12:57:36 +0800, Dong Ge <> wrote:
    >
    >
    >>Hi! Karthik,
    >>
    >>I guess your maybe not reading my codes carefully. I use the same
    >>handle "cfPtr" to open, write and close the same file, not "cfPts". In
    >>fact, the code can be compiled and run.
    >>
    >>I just want to know how to delete a text line or some characters.
    >>
    >>Thanks for your reply.

    >
    >
     
    Jason Curl, Oct 5, 2004
    #9
    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. Andreas Leitgeb
    Replies:
    0
    Views:
    453
    Andreas Leitgeb
    May 15, 2009
  2. Mark Space
    Replies:
    0
    Views:
    487
    Mark Space
    May 15, 2009
  3. Lew
    Replies:
    0
    Views:
    951
  4. Richard Schneeman
    Replies:
    16
    Views:
    494
    Daniel Bush
    Aug 27, 2008
  5. Amy Lee
    Replies:
    6
    Views:
    129
    Gunnar Hjalmarsson
    Aug 16, 2007
Loading...

Share This Page