Thread: txt files

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    11

    txt files

    Hi,
    When writing my last program i ran into a problem: when writing to text file i want my program to add the data to file, not replace it (I mean like data field 1, data field 2. data field 1 again and data field 2 again with different values offcource)

  2. #2
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Code:
    ofstream file("filenamt.txt", ios::app);
    if your using standard C++ and if you are not using std as a global namespace

    then that will be std::ios::app instead.

    also, if this is not the problem.. post the source code so we can see what the problem is

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    87
    You must open the file for appending.I.e. if you use fopen(), you must specify "a" in the mode.

    Or if you want to insert data in the midle of the file, you must first shift the data and the to replace the data, which is stored in then place.

    P.S. Sorry if my English is not good. I just saw that I have did my post at the same time as another post. I think that they complement eatch other.
    Last edited by Gravedigga; 08-04-2004 at 01:52 PM.

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    11
    Thanx, got it now

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    11
    Sorry for double post, but i have unother problem. I want my program to get random values from that file (meaning randomly togeather value 1 and value 2, then again random value 1 and random value 2)

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    create a container to hold the file contents. Read the entire file into the container. randomly access the data in the container. OR, if you have perfectly symetrical data in the file--for example value1 and value2 are always have the same memory footprint and there is nothing else in the file, then you can use fseek() to place the file pointer wherever desired to access just the desired value.

    Edit: by memory footprint I mean that value1 and value2 are both ints or value1 is a double and value2 is an float or something like that. With strings, the memory footprint will only be the same if the strings written to file always have the same number of char and are therefore of predicatable, stable sizes.
    Last edited by elad; 08-06-2004 at 02:54 PM.

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    11
    Thanx, ill try the fseek(), but I dont have any idea, what do you mean under container =D (ya, i know im just unother noob taking projects, what i cant handle:P)

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    container is just generic verbage for any object that can hold a group of other objects. An array is a container, a list is a container, a queue is a container, a stack is a container, etc.

  9. #9
    Registered User
    Join Date
    Aug 2004
    Posts
    11
    Oh, ok thank you again =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. comparing 2 txt files
    By norhos in forum C Programming
    Replies: 9
    Last Post: 03-13-2008, 06:18 AM
  2. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Editing TXT files in C++
    By Nexus in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2005, 05:34 AM
  5. printing txt files
    By baphomet in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-10-2002, 09:53 PM