TermSpar

File streams Adv

Apr 23rd, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. int main(){
  9.  
  10.     cout << "Name of file: ";
  11.     string fileName;
  12.     cin >> fileName;
  13.  
  14.     ofstream myFile(fileName);
  15.  
  16.     if(myFile.is_open()){
  17.        
  18.         vector<string> userData;
  19.  
  20.         cout << "Enter how many words you'd like to write to file: ";
  21.         int wordCount;
  22.         cin >> wordCount;
  23.  
  24.         for(int i = 0; i < wordCount; i++){
  25.            
  26.             cout << "Enter word " << i + 1 << ": ";
  27.             string uInput;
  28.             cin >> uInput;
  29.  
  30.             userData.push_back(uInput);
  31.             userData.push_back(" ");
  32.  
  33.         }
  34.  
  35.         for(unsigned int i = 0; i < userData.size(); i++){
  36.            
  37.             cout << "Data: '" << userData[i] << "' written successfully" << endl;
  38.             myFile << userData[i];
  39.  
  40.         }
  41.  
  42.         myFile.close();
  43.  
  44.     }else{
  45.         cerr << "File could not open!" << endl;
  46.     }
  47.  
  48.  
  49.     system("pause");
  50.     return 0;
  51. }
Add Comment
Please, Sign In to add comment