File I/O

This is a discussion on File I/O within the C++ Programming forums, part of the General Programming Boards category; How do I get a file to truncate, not append upon closing the program. When I close it, it writes ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    7

    File I/O

    How do I get a file to truncate, not append upon closing the program. When I close it, it writes to the file, but instead of updating the numeric total count it appends to it. Using the IOS::ATE | IOS::OUT.
    Any help appreciated as I'm new to this...

    Turbo C++ v4.5 (Borland)

  2. #2
    31173 h4x0r gnu-ehacks's Avatar
    Join Date
    Nov 2001
    Posts
    556
    Try fstream.
    What will people say if they hear that I'm a Jesus freak?
    What will people do if they find that it's true?
    I don't really care if they label me a Jesus freak, there is no disguising the truth!

    Jesus Freak, D.C. Talk

    -gnu-ehacks

  3. #3
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    ios::ate puts the filepointer at the eof for input. the difference between ate and app is that ate lets you write over old data.

    just use ios:ut and it will do what you want also there is ios::trunc which i think sets the file at 0 length, though i have never used it i personally use ios:ut

    edit:
    the smiley is o

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    C++ streams that write to file (fstreams in ios:ut mode and ofstreams) use the trunc mode (which overwrites all data in the open file----maybe by setting file length to 0, I am not sure how the mode is implemented) as default. Therefore:

    ofstream fout("myFile.txt");

    opens the the file called myFile.txt in ios:ut|ios::trunc mode by default, assuming myFile.txt is located in the same directory as the the program's exe file.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    7
    Many thanks for the replies. Will give it a shot. ps I used fstream <filenames> as a global, is this acceptable..?

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Yes.....but, use of global variables unless no work around available is discouraged in every teaching item and by most programmers I respect.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    7
    Yes E-lad, I know. It's the way the college have shown us. Everything I read about is classes. When I wrote a program with classes the tutor wouldn't help me. ps I finally used IOS::TRUNC |IOS::OUT and it works. Now I just have to get the other file to read/write and I'm sorted. The code is for a PC diagnostic interface and I have to log total errors to date by category, eg, Post errors, keyboard errors etc. Used Indexes but it won't write to file so when I restart program it reads a blank file. Coding is as follows:

    filecat.open("a:\\error_count.txt", ios::trunc | ios:ut);
    if (!filecat)
    {
    cout <<"Error initialising file > error_count.txt "<<endl;
    }
    else
    {
    for (int index =0; index<7; index++)
    {
    filecat <<errors[index].no_of_faults;
    }
    }
    filecat.close();

    there are 6 fault categories but it won't write to a file on exit.

    As I've said, I'm only beginner at C++ and any books or web sites use classes...any help, thanks in advance...

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    You migth be restricted to using the ios::whatever syntax only when you declare the stream. If that is true then it could be:

    ifstream filecat("a:\\error_count.txt", ios::trunc | ios:ut);

    or

    ifstream filecat (ios::trunc | iosut);
    filecat.open("a:\\error_count.txt");

    but not

    filecat.open("a:\\error_count.txt", ios::trunc | ios:ut);

    HTH.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    7
    Much appreciated Elad...will give it a shot...

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. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-31-2004, 12:21 AM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 09:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21