Thread: delete the file contents

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    16

    delete the file contents

    Hi All,

    I would like to delete the specific lines in a file programmatically, is there any way I can do that? Similar to the one we do it in notepad i.e. as soon as we delete a line the line below the deleted line would occupy the current position.

    Thanks in advance

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Open the file. Read the contents. Remove what you want. Save the new version. Just like how notepad works.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by jayfriend
    Hi All,

    I would like to delete the specific lines in a file programmatically, is there any way I can do that? Similar to the one we do it in notepad i.e. as soon as we delete a line the line below the deleted line would occupy the current position.

    Thanks in advance
    Text files may have a couple of unseen char's at the end of each line - Carriage Return (CR) and Line Feed (LF). This is a very old convention that is still used in Windows and DOS text files.

    When your program deletes the unwanted line, check to be sure it is also deleting the CR/LF combination of char's which will exist wherever a hard return is located in the file. "Soft" returns are handled by the displaying program, and do not require a CR/LF combo. For example:

    This is a line with a hard return at the end. CR/LF

    This line is very long, exceeding the line length of the display program, and so it won't have a hard return located at the end of the first line, but will have one at the end of the second line.

    I don't have my ascii chart handy, but CR and LF's are ascii 10 and 12, iirc.

    Adak

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    16
    Quote Originally Posted by quzah
    Open the file. Read the contents. Remove what you want. Save the new version. Just like how notepad works.


    Quzah.
    I am looking for a function or system call with which we can delete the file contents.
    Please let me know your suggestions on this.

    Thanks in advance.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Due to the nature of C being available everywhere, and the fact that different systems have different conventions for text files, there can't possibly be a standard function or a reliable "system call," since your system could be anything as far as I know.

    Make a function that does what you need. It'll be less of a hassle.

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    fopen(file, "w"); //never used C file io but from what I understand this opens a file to write and truncates it.

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    16
    Hi Citizen,

    Quote Originally Posted by citizen
    Due to the nature of C being available everywhere, and the fact that different systems have different conventions for text files, there can't possibly be a standard function or a reliable "system call," since your system could be anything as far as I know.

    Make a function that does what you need. It'll be less of a hassle.

    Could you please help me write the function which will delete the contents of a file.

    Thanks in advance.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Quote Originally Posted by c99
    7.19.4.1 The remove function
    Synopsis
    1 #include <stdio.h>
    int remove(const char *filename);
    Description
    2 The remove function causes the file whose name is the string pointed to by filename
    to be no longer accessible by that name. A subsequent attempt to open that file using that
    name will fail, unless it is created anew. If the file is open, the behavior of the remove
    function is implementation-defined.
    Returns
    3 The remove function returns zero if the operation succeeds, nonzero if it fails.
    It also exists in C89.
    It's in stdio.h
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Jan 2007
    Posts
    16
    Quote:
    Originally Posted by c99
    7.19.4.1 The remove function
    Synopsis
    1 #include <stdio.h>
    int remove(const char *filename);
    Description
    2 The remove function causes the file whose name is the string pointed to by filename
    to be no longer accessible by that name. A subsequent attempt to open that file using that
    name will fail, unless it is created anew. If the file is open, the behavior of the remove
    function is implementation-defined.
    Returns
    3 The remove function returns zero if the operation succeeds, nonzero if it fails.

    It also exists in C89.
    It's in stdio.h

    But how do I truncate the file? How I delete the selected lines of a file?

    Thanks in advance.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You've already been told how to do it. One more time.

    Open.
    Read.
    Edit.
    Write.
    Close.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Create a buffer of the file. Compress the buffer. Delete the file (remove). Write the buffer to a file.

    FILE:"This is an Example."
    Code:
    #include <stdio.h>
    #include <stdlib.h> // for malloc/free
    #include "yourfunctions.h"
    int main(void)
    {
            char *mybigbuffer;
            mybigbuffer = malloc(BIGBUFFERSIZE); // defined in yourfunctions.h
            if (mybigbuffer == NULL){
                    crapout();
                    return -1;
            }
            doreadme("FILE", mybigbuffer);
            remove("FILE");  // BTW, this is redundant.
            doremovetext(mybigbuffer, "is an Ex");
            dowriteme("FILE", mybigbuffer);
            free(mybigbuffer);
            return 0;
    }
    FILE:"This ample."
    Last edited by Kennedy; 01-17-2007 at 01:10 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM