Thread: Erasing FILE

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    4

    Erasing FILE

    Hi,
    is there any way to erase the contend of a file without erasing the file????

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    open it in "w" mode. that will erase the file content.
    niara

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    4
    thanks
    ^^

  4. #4
    Linux User
    Join Date
    Nov 2005
    Location
    Bellingham, WA
    Posts
    35

    tag "w" doesn't always work!

    High I myself just recently created a simple program to test a few functions and opening the file in "w" (write mode) didn't erase any data in it, instead it just wrote after all the data. However I found that in such a case the following fprintf statement works just as well:

    /* Must include this .h header file */
    #include <config.h>

    /* Replace file with a char pointer pointing to the file location */
    /* The quotation marks basicly just tells fprintf to not print anything at all and instead use the systems clear comand to clear all data within a file. */
    fprintf(file, "", system("clear"));

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    No, don't make a call to the system.

    Instead of using "w" use "w+"
    Sent from my iPadŽ

  6. #6
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    tuxinator, that line does the same to the file as:
    Code:
    fprintf(file, "");
    All system("clear") does is clear your terminal, and return 0 for the third parameter of fprintf, which is ignored, because your format specifier (the second parameter) is empty.

    Quote Originally Posted by SlyMaelstrom
    Instead of using "w" use "w+"
    That shouldn't make any difference.
    Last edited by cwr; 12-01-2005 at 12:18 AM.

  7. #7
    Linux User
    Join Date
    Nov 2005
    Location
    Bellingham, WA
    Posts
    35

    Interesting

    Hm... I'll have to do some more testing with my code then because before when I first had the fopen mode set to "w" and didn't have any function to clear the file it just outputted the information after the previous information.

    P.S. I am currently working on creating a copy of my code and compiling both to see if they both give the same output. If they don't then I will post both codes online and a precompiled version in case it is a problem with my compiler.

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Hmmm I thought "w" appends.
    Sent from my iPadŽ

  9. #9
    Linux User
    Join Date
    Nov 2005
    Location
    Bellingham, WA
    Posts
    35

    Read the FAQ

    Quote Originally Posted by SlyMaelstrom
    Hmmm I thought "w" appends.
    The FAQ page has a section that explains what each mode represents.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM