Thread: Output File Help please

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

    Output File Help please

    Hi, I'm trying to create a CD database and I want to have a .txt file that contains the database. So as I start the program, it will read in the database and anything I add or erase from it then it will update the file.

    So i have this so far:


    Code:
    ofstream file("databaseFile.txt", ios::out | ios::app);
      if ( !file.is_open() )
        {
          cout << "Error opening File!" << endl;
          exit(1);
        }

    And i think that should open the file and say that if I add anything to it then it will add it to the end instead of writing over the original file.

    Now I'm confused about how to actually get stuff on that file. The way the program is set up is the main function consists of a menu where you can add, delete, or print the CD's. So if I want to add a cd then it prompts me for the Artist, Title, and Year and then calls the addCD function in which it will add it to an empty spot in the database. How do I get it to add it to an output file?

    Someone please help

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Read this great fstream tutorial, http://www.daniweb.com/techtalkforum...2&page=1&pp=15
    it will explain how to get the input and output.

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    Code:
    file << "This goes to the file!";

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    Once I've written something to an ouput file, is there a way I can delete anything from it.

    For instance, I prompt the user what CD he wants to delete, and then I can find it in the database and delete it, but how do I delete it from the file. The program adds it to the output file in the addCD function, so is there a way I can remove it?

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You probably don't want to open the file in append mode. The easiest thing to do in most of these cases is to open the file in read mode, then read the entire contents of the file into your container (array or whatever) at the beginning of the program and then close the file. Operate upon this container within the 'guts" of your program, adds/deletes/etc... At the end of your program, you open the same file but this time in write mode and you simply write the entire contents of the container back to the file completely overwritting what was previously in the file. You cannot just delete something from the middle of a file and just expect everything around that position to shift around for you.
    "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

  6. #6
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    >>so is there a way I can remove it?
    Not easily. Unless the system supports a very specialized record-oriented file type, you have no choice but to remove from a file as you would from an array: Shift everything from the deleted item backward to fill the hole created by a deletion. A simple implementation would use a helper data structure:
    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    bool show_file(const char *name)
    {
      ifstream fin(name);
      string s;
    
      if (!fin)
        return false;
    
      while (getline(fin, s))
        cout<< s <<endl;
    
      return true;
    }
    
    int main()
    { 
      cout<<"Before:\n";
      if (!show_file("test.txt")) {
        cerr<<"Error processing file"<<endl;
        return EXIT_FAILURE;
      }
    
      string s;
      vector<string> mem_file;
      ifstream fin("test.txt");
    
      if (!fin) {
        cerr<<"Error opening file"<<endl;
        return EXIT_FAILURE;
      }
    
      while (getline(fin, s))
        mem_file.push_back(s);
    
      if (!fin.eof()) {
        cerr<<"Error processing file"<<endl;
        return EXIT_FAILURE;
      }
    
      ofstream fout("test.txt");
    
      if (!fout) {
        cerr<<"Error opening file"<<endl;
        return EXIT_FAILURE;
      }
    
      // Remove the second line
      for (vector<string>::size_type i = 0; i < mem_file.size(); i++)
      {
        if (i != 1)
          fout<< mem_file[i] <<endl;
      }
    
      cout<<"After:\n";
      if (!show_file("test.txt")) {
        cerr<<"Error processing file"<<endl;
        return EXIT_FAILURE;
      }
    
      return EXIT_SUCCESS;
    }
    Kampai!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM