By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

ifstream ofstream help

P: 16
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:

Expand|Select|Wrap|Line Numbers
  1. # include <iostream>
  2. # include <fstream>
  3. # include <cstdlib>
  4. # include <iomanip>
  5. # include <string>
  6. # include <vector>
  7.  
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12.     string filePath = "C:\\documents\\test\\";
  13.     string fileName = "";
  14.     string name = "";
  15.     char character=' ';
  16.  
  17.     ofstream myfile("C:\\documents\\test\\");
  18.     ifstream inputFile;
  19.  
  20.     vector<string>names(4," ");
  21.  
  22.     cout << "Please enter the name of the input file: ";
  23.     getline(cin, fileName);
  24.     fileName = filePath + fileName;
  25.  
  26.     inputFile.open(fileName.c_str());
  27.  
  28.     while(inputFile.get(character)) 
  29.     {
  30.         cout << character;
  31.     }
  32.  
  33.     cin.get();
  34.     cin.get();
  35.     return 0;
  36. }
  37.  
Mar 23 '07 #1
Share this Question
Share on Google+
5 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
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.
Mar 23 '07 #2

P: 16
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.
Expand|Select|Wrap|Line Numbers
  1. # include <iostream>
  2. # include <fstream>
  3. # include <cstdlib>
  4. # include <iomanip>
  5. # include <string>
  6. # include <vector>
  7.  
  8. using namespace std;
  9.  
  10. int choice=0;
  11.  
  12. int main()
  13. {
  14.     string filePath = "C:\\17\\test\\";
  15.     string fileName = "";
  16.     string name = "";
  17.     char character=' ';
  18.  
  19.     ofstream myfile("C:\\17\\test\\");
  20.     ifstream inputFile;
  21.  
  22.     vector<string>names(4," ");
  23.  
  24.     cout << "Please enter the name of the input file: ";
  25.     getline(cin, fileName);
  26.     fileName = filePath + fileName;
  27.  
  28.     inputFile.open(fileName.c_str());
  29.  
  30.     while(inputFile.get(character)) 
  31.     {
  32.         cout << character;
  33.         cout<<endl;
  34.     }
  35.  
  36.     cout<<"Do you want to:";
  37.     cout<<endl;
  38.     cout<<"1. Delete a name";
  39.     cout<<endl;
  40.     cout<<"2. Insert a name";
  41.     cout<<endl;
  42.     cout<<"3. Write the names to a file and end the program";
  43.     cout<<endl;
  44.     cin>>choice;
  45.  
  46.  
  47.     cin.get();
  48.     cin.get();
  49.     return 0;
  50. }
  51.  
Yes, i am a beginner at C++
Mar 25 '07 #3

sicarie
Expert Mod 2.5K+
P: 4,677
Expand|Select|Wrap|Line Numbers
  1.  
  2.     cout<<"Do you want to:";
  3.     cout<<endl;
  4.     cout<<"1. Delete a name";
  5.     cout<<endl;
  6.     cout<<"2. Insert a name";
  7.     cout<<endl;
  8.     cout<<"3. Write the names to a file and end the program";
  9.     cout<<endl;
  10.     cin>>choice;
  11.  
  12.  
  13.     cin.get();
  14.     cin.get();
  15.     return 0;
  16. }
  17.  
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.
Mar 25 '07 #4

P: 16
I know that, thanks, but the problem is how to store the names in the vector after the file is opened.
Mar 25 '07 #5

sicarie
Expert Mod 2.5K+
P: 4,677
I know that, thanks, but the problem is how to store the names in the vector after the file is opened.
Expand|Select|Wrap|Line Numbers
  1.     while(inputFile.get(character)) 
  2.     {
  3.         cout << character;
  4.         cout<<endl;
  5.     }
  6.  
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.
Mar 25 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.