Need some help w/ File I/O

This is a discussion on Need some help w/ File I/O within the C++ Programming forums, part of the General Programming Boards category; I am writing a program where I have a menu set up to Add an item and display current items. ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    3

    Need some help w/ File I/O

    I am writing a program where I have a menu set up to Add an item and display current items. When I add an item, I want to send it to a file. Then, i want to be able to close the program, and do whatever else, then when I run it again, load up the old list from the file, display it, and be able to add new items to the end of it. Any suggestions?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    1. Create an ifstream object and open your file.
    2. Load contents of file into some type of data structure, STL containers work great for this, i.e. a vector/list/set/map.
    3. Close input file.
    4. As program is run, user is able to insert new elements into container. Keep doing this until user wishes to quit.
    5. Create an ofstream object using the same filename you used before, you want to overwrite the old file.
    6. Loop/iterate through the container storing your information and write contents to the file.
    7. Close output file.
    8. Exit program.

    What specifically are you having difficulties with?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    3
    I am having troubles with getting the file to load up. Once I output the first items in the list initially and close the program, I am not sure on how to get the items from the output file back into the input file, so that I may add more to the list. I do not want to erase the list, i just want to add to it.

  4. #4
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    post some code ppollard, then we will be able to help you better...remember to use code tags

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Use ios::app mode to open a file that is not empty and always add to the end of file.

    ofstream fout("myText.txt", ios::app);

    If you want to load existing file to program, add to file contents while in program, and then write modified file contents back to program, use previous posted algorithm.
    Last edited by elad; 06-04-2004 at 09:38 AM.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    Open your output file using the same name as when you opened it for input. If you are only going to write new elements to the file, then you would want to open the file in append mode so that new data gets added to the end of the existing file. However, it can be just as easy to simply overwriting the contents completely with the data (old list plus any new elements). This would be needed especially if you ever decided to implement deletion of elements to your program. As an example, the following program read in a bunch of integers into a vector, display them to the user, asks for a new integer from the user which will be added to the vector, and rewrites the file with the contents of the vector:

    Code:
    #include <fstream>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <iterator>
    
    using namespace std;
    
    int main()
    {
        vector<int> intVect;
        ifstream in("Data.Txt");
    
        // Read through file and store data into vector.
    
        copy(istream_iterator<int>(in),istream_iterator<int>(),back_inserter(intVect));
        in.close();
    
        // Output contents of vector to screen.
    
        copy(intVect.begin(),intVect.end(),ostream_iterator<int>(cout," "));
    
        // Ask user for new integer to store.
    
        int val;
        cout << "\nEnter new integer: ";
        cin >> val;
        intVect.push_back(val);
    
        // Write vector back to file overwritting existing contents.
    
        ofstream out("Data.Txt");
        copy(intVect.begin(),intVect.end(),ostream_iterator<int>(out," "));
        out.close();
    
        return 0;
    }
    Assuming Data.Txt start out containing "5 13 24" then sample output could look like this:
    Code:
    5 13 24
    Enter new integer: 43
    After running a second time:
    Code:
    5 13 24 43
    Enter new integer: 64
    After running a third time:
    Code:
    5 13 24 43 64
    Enter new integer: 31
    Etc... etc... as you see all the existing data is still there because we write everything back out to the file every time. Now... for large amounts of data you may not want to do this.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    3

    thanks for the help.

    Thank you for the example. I tweeked with it a little and got it to work for me. I have never worked with vectors before. I have a couple of quick questions though.

    1) If I wanted to delete items, then what would be the best route to go?

    2) If I wanted to print out the blank spaces in the text file, how would I do that. When I have blank spaces now, it puts the text on the next line.

    thanks.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >1) If I wanted to delete items, then what would be the best route to go?
    The vector class has an erase member function:
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    void showme(vector<int> v);
    
    int
    main()
    {
      vector<int> v;
    
      for (int i = 0; i < 5; i++) {
        v.push_back(i);
      }
      showme(v);
      v.erase(v.begin() + 1);                // Erase 1
      showme(v);
      v.erase(v.begin() + 1, v.begin() + 3); // Erase 2 and 3
      showme(v);
    }
    
    void
    showme(
      vector<int> v
      )
    {
      vector<int>::const_iterator it = v.begin();
    
      while (it != v.end()) {
        cout<< *it++ <<' ';
      }
      cout<<endl;
    }
    You can erase a single item, or a range of items with relative ease. But remember that erase takes iterators.

    >2) If I wanted to print out the blank spaces in the text file, how would I do that.
    Can you give an example of what you want?
    My best code is written with the delete key.

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

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