TermSpar

Filestreams++

Apr 24th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. void readFile();
  9.  
  10. int main(){
  11.  
  12.     cout << "Would you like to create or read a file? (type: 'create' or 'read'): ";
  13.     string uDesc;
  14.     cin >> uDesc;
  15.  
  16.     if(uDesc == "create"){
  17.         vector<string> userData;
  18.  
  19.     cout << "Enter what you want to name the file (Must be .txt): ";
  20.     string fileName;
  21.     cin >> fileName;
  22.  
  23.     ofstream myFile(fileName);
  24.  
  25.     if(myFile.is_open()){
  26.        
  27.         cout << "Enter number of words (Must press ENTER after each word): ";
  28.         int wordCount;
  29.         cin >> wordCount;
  30.        
  31.         for(int i = 0; i < wordCount; i++){
  32.            
  33.             cout << "Enter word " << i + 1 << ": ";
  34.  
  35.             string uInput;
  36.             cin >> uInput;
  37.             userData.push_back(uInput);
  38.             userData.push_back(" ");
  39.  
  40.         }
  41.            
  42.         for(unsigned int i = 0; i < userData.size(); i++){
  43.  
  44.             myFile << userData[i];
  45.             cout << endl;
  46.             cout << "Data: '" << userData[i] << "' successfully written" << endl;
  47.  
  48.         }
  49.             myFile.close();
  50.             cout << endl;
  51.             cout << "----------------------------------------------" << endl;
  52.             cout << "All text to " << fileName << " has been successfully written" << endl;
  53.             cout << endl;
  54.             cout << "Would you like to read from a file? (yes or no): ";
  55.             string yOr;
  56.             cin >> yOr;
  57.  
  58.             if(yOr == "yes"){
  59.                
  60.                 readFile();
  61.  
  62.             }else{
  63.                 cout << "Completed" << endl;
  64.             }
  65.  
  66.     }else{
  67.  
  68.     cout << "Could not open file" << endl;
  69.  
  70.     }
  71.     }else if(uDesc == "read"){
  72.        
  73.         readFile();
  74.  
  75.     }else{
  76.  
  77.         cout << "Invalid option" << endl;
  78.  
  79.     }
  80.    
  81.    
  82.     system("pause");
  83.     return 0;
  84. }
  85.  
  86. void readFile(){
  87.    
  88.     vector<string> fileText;
  89.  
  90.     cout << "Name of file: ";
  91.     string fileName;
  92.     cin >> fileName;
  93.  
  94.     cout << "Look for a certain word? (yes or no): ";
  95.     bool wordSearch = false;
  96.     string yesOrNo;
  97.     string specWord;
  98.     cin >> yesOrNo;
  99.  
  100.     if(yesOrNo == "yes"){
  101.  
  102.         wordSearch = true;
  103.         cout << "What word?: ";
  104.         cin >> specWord;
  105.  
  106.     }else{
  107.         wordSearch = false;
  108.     }
  109.  
  110.     ifstream myFile;
  111.     myFile.open(fileName);
  112.  
  113.     string fileData;
  114.  
  115.     int fileSize = 0;
  116.     int specCount = 0;
  117.  
  118.     cout << endl;
  119.     cout << "Data from " << fileName << ": ";
  120.  
  121.     while(!myFile.eof()){
  122.         fileSize++;
  123.         myFile >> fileData;
  124.         cout << fileData;
  125.         cout << " ";
  126.  
  127.         if(wordSearch == true){
  128.            
  129.             if (fileData == specWord){
  130.                
  131.                 specCount++;
  132.  
  133.             }
  134.  
  135.         }
  136.     }
  137.     cout << endl;
  138.  
  139.     cout << endl;
  140.     cout << endl;
  141.     cout << "-----------------------------";
  142.     cout << endl;
  143.     cout << fileSize << " words found and displayed" << endl;
  144.     cout << "-----------------------------" << endl;
  145.     cout << endl;
  146.     cout << "-----------------------------" << endl;
  147.     if(wordSearch == true){
  148.        
  149.         cout << specWord << " was found " << specCount << " times" << endl;
  150.         cout << "-----------------------------" << endl;
  151.         cout << endl;
  152.  
  153.     }
  154.  
  155.     myFile.close();
  156. }
Add Comment
Please, Sign In to add comment