ifstream ofstream help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mastern200
    New Member
    • Oct 2006
    • 16

    ifstream ofstream help

    I need to make a program where it can read from a file and write to it. But the problem is, all i can do is read from it and not write to it. This is the code i have so far:

    Code:
    # include <iostream>
    # include <fstream>
    # include <cstdlib>
    # include <iomanip>
    # include <string>
    # include <vector>
    
    using namespace std;
    
    int main()
    {
        string filePath = "C:\\documents\\test\\";
        string fileName = "";
    	string name = "";
    	char character=' ';
    
        ofstream myfile("C:\\documents\\test\\");
        ifstream inputFile;
        
        vector<string>names(4," ");
    	
    	cout << "Please enter the name of the input file: ";
    	getline(cin, fileName);
    	fileName = filePath + fileName;
    
    	inputFile.open(fileName.c_str());
    	
    	while(inputFile.get(character)) 
    	{
    		cout << character;
        }
    	
    	cin.get();
    	cin.get();
    	return 0;
    }
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    That's because you just declare it as an ifstream (input file stream), and you want it to be an fstream to both read and write to it.

    I personally like cplusplus.com's tutorials, I think they're easy to read and have good examples, but let me know if that doesn't help.

    Comment

    • mastern200
      New Member
      • Oct 2006
      • 16

      #3
      OK i get it, thanks. But here is the real problem: The program should ask the user where is a text file. The file should open and it will be a list of names. The names should be stored in a vector after being read by the program.The program then will call a function named print that prints to the monitor the list of names, numbered starting with 1. It will ask the user if they want to delete a name, insert a name, or write the names to a file and end the program. The problem here is, i can get the file to open up and display the names, but i can't get it to prompt the user what to do next. It basically just displays the file, but doesnt allow for any editing.
      Code:
      # include <iostream>
      # include <fstream>
      # include <cstdlib>
      # include <iomanip>
      # include <string>
      # include <vector>
      
      using namespace std;
      
      int choice=0;
      
      int main()
      {
          string filePath = "C:\\17\\test\\";
          string fileName = "";
          string name = "";
          char character=' ';
      
          ofstream myfile("C:\\17\\test\\");
          ifstream inputFile;
          
          vector<string>names(4," ");
          
          cout << "Please enter the name of the input file: ";
          getline(cin, fileName);
          fileName = filePath + fileName;
      
          inputFile.open(fileName.c_str());
          
          while(inputFile.get(character)) 
          {
              cout << character;
              cout<<endl;
          }
          
          cout<<"Do you want to:";
          cout<<endl;
          cout<<"1. Delete a name";
          cout<<endl;
          cout<<"2. Insert a name";
          cout<<endl;
          cout<<"3. Write the names to a file and end the program";
          cout<<endl;
          cin>>choice;
      
          
          cin.get();
          cin.get();
          return 0;
      }
      Yes, i am a beginner at C++

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Code:
            
            cout<<"Do you want to:";
            cout<<endl;
            cout<<"1. Delete a name";
            cout<<endl;
            cout<<"2. Insert a name";
            cout<<endl;
            cout<<"3. Write the names to a file and end the program";
            cout<<endl;
            cin>>choice;
        
            
            cin.get();
            cin.get();
            return 0;
        }
        You have no control structure for keeping in the program, you just get two inputs and exit. I would recommend using a while loop around the code above, and then you could call whatever functions you wanted to edit or save the vector.

        Comment

        • mastern200
          New Member
          • Oct 2006
          • 16

          #5
          I know that, thanks, but the problem is how to store the names in the vector after the file is opened.

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Originally posted by mastern200
            I know that, thanks, but the problem is how to store the names in the vector after the file is opened.
            Code:
                while(inputFile.get(character)) 
                {
                    cout << character;
                    cout<<endl;
                }
            You do it there, add each filename to the vector, while you're reading it in, that way you can close your ifstream when you're done with it. I would recommend using getline again, and then if there are multiple elements on one line, strtok() to parse those elements apart.

            Comment

            Working...