Thread: How to delete a line in a file

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

    How to delete a line in a file

    Hi there and first of all thanks for the time....

    I'm having a few problems with an issue....
    I have list.txt with:

    a1
    a2
    a3

    And I tried to do a program that would delete the whole line but instead this was what happenned

    []a2
    a3

    ([] is a line break)
    so in fact i have:

    (blank line)
    a2
    a3

    What I need is after I run the program!

    a2
    a3

    Is there a way of deleting the whole line?
    Thanks in advance!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To do so, you must move all the lines upwards.
    Or open a new file, skip the line you want to delete and copy the rest into it.
    There's no way to "just" delete a line.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You'll have to read the file and rewrite it. If the line you wanted to delete was the last one, you could truncate the file with various commands. But, since it is at the front, or if it was in the middle, you get to rewrite the file.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    3
    imagine the example I have....
    What I need is to delete one line... can also be the last one but what I do not need is the [] char to apear

    if I have :

    a1
    a2
    a3

    it also be good to keep the a1 and a2 and delete the a3 line compleetly and not:

    a1
    a2[]

    I need this for working with random codes... I have a file list.txt with 200 lines each one with a code and would like eavh time the program is runed that the list.txt would have the first or last line deleted and the presented the code.

    in the example would be something like this:

    <root@localhost>./test
    Your code is a3
    <root@localhost>./test
    Your code is a2
    <root@localhost>./test
    Your code is a1
    <root@localhost>./test
    Error - No codes left

    Can anyone help a noob on this please?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your description is so hmmm, *different*, that I have no concept of WTF you're really trying to do. Enphish skillets has goney to feces in the skools.

    Seriously, a3, then a2, then a1, doesn't look random to me. If you want a random line of code, there is a random command in C, that can be used to either select a random line, NOT select a random line, or select (or NOT select), a whole bunch of random lines.

    If you want to remove the [] char, this will do it:
    Code:
    char cr;  //cr = carriage return
    fscanf(filehandle, "%c", &cr);
    Just add it in, (or put it together with your other fscanf() code for the line, (right at the end), and your file pointer will go past the carriage return.

    You don't usually delete lines from a file for this kind of use. You select the lines you want to show, and show only those lines, but you don't delete lines. You may want an index array of int's to keep track of which lines you don't want to show (maybe 1 = show it, 0 = don't show it), but it's hard to tell from your description.

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    3
    I better strart over ...... But before all thanks for trying help me

    what I need is a program that would delete the first or last line from a file and then display it:

    The file I have is list.txt wich haves the folowing
    a1[]a2[]a3 or in pretty shape

    a1
    a2
    a3

    what I need is that the program would delete the whole line including the [] char

    so the list.txt would be , after, a2[]a3 and not []a2[]a3

    I have experience in Delphi and with objects is easy to do this... now I need to get this to work in linux and c basis....

    Can I searh the file until I find a [] and then delete the contets until there?

    Help please!

  7. #7
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Code:
    int ch;
    FILE* fp;
    fp=fopen("list.txt","r");
    
    while((ch=fgetc(fp))!='\n'); /*skips one line and also eats newline character*/
    
    /*Now copy rest of the contents to another file*/
    Last edited by abh!shek; 06-06-2008 at 06:24 AM.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There is no "delete char" command to do this. Yes, you can easily move the file pointer past the first [], or anywhere else, and *display* the lines you want only to be displayed.

    But you haven't actually deleted, anything.

    If you really want to delete some line from the file:

    1) Open a temp file for writing into
    2) Open the list.txt file for reading
    3) Move the file pointer to the first char that you want to remain in list.txt
    4) Write that char and all the char's you want to keep in list.txt, to the temp file
    5) Close both files
    6) Delete the list.txt file
    7) Rename the temp file, to list.txt.

    You can also just over-write the char's you don't want, if they are at the beginning of the list.txt file, or shorten (truncate) a file, in place, if the char's you don't want are at the end of the file.

    It is more difficult to do that, however. Using 1-7 above is very simple, and I'd recommend it to you.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The common method to solve this has already been discussed: Create a different file, read in the all data and write out the modified data to the new file (nice programs will rename the original file to the new name).

    Alternatives include:
    - Using system specific functions to truncate the file after rewriting the content "in situ" (you will still need to read all lines and write out the ones you want, but you now do it in the existing file, which is OK for deleting, but gets more tricky when extending the file - because you need to start from the back of the file).
    - Read the entire content into memory, modify, and write back out. This means that the file must not be bigger than what you can fit in memory (which will depend on the OS architecture, system configuration and other conditions [e.g. the amount of memory in the machine, how many other tasks are running in the system etc]).

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM