Thread: Deleting text

  1. #1
    Unregistered
    Guest

    Deleting text

    How do I delete a line of text in a text file without deleteing the entire file? Would switching to binary mode and then back to text help?

    Thanks
    Jeff

  2. #2
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Good question. You might try fputs with a whole slew of blank spaces. But, I don't think that'll work. And I don't know of any functions.

    But try writing a lot of blank spaces to a line and see what happens.

    --Garfield
    1978 Silver Anniversary Corvette

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    You could read in the whole file, copy the text after the line to the line and shorten down the file.

    Pseudo code:

    data = FileRead(sizeof(file));
    memcpy(&data[startofline], &data[endofline], sizeof(file)-endofline);

    FileWrite(data, sizeof(file)-startofline);
    // Gliptic

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    If you know the full line of text in the file, as gliptic says read file.
    I would suggest using the original file and a new file. Even if you knew the first field was unique e.g. refernece number, you should be able to delete the relevant line accordingly

    Code:
     old_fp = fopen("oldfile.txt", "r")
    new_fp = fopen("newfile.txt", "w")
    
    while((fgets(str, record_length, old_fp))!= EOF)
    {
        if(strcmp(known_textinfile, str))
        {
             fputs(str, new_fp);
        }
    }
    Ofcourse you will have to manipulate the code to your own specifications, new line characters ext.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX | Drawing text
    By gavra in forum Game Programming
    Replies: 4
    Last Post: 06-08-2009, 12:23 AM
  2. Deleting Text File Record
    By 01010011 in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2009, 02:46 PM
  3. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  4. Replies: 1
    Last Post: 07-13-2002, 05:45 PM
  5. deleting records from text fle
    By kayurp in forum C Programming
    Replies: 1
    Last Post: 02-27-2002, 01:05 AM