Advertisement
fahimkamal63

File I/O

Nov 28th, 2019
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.61 KB | None | 0 0
  1. //  File I/O
  2. //  Date : 23.11.19
  3. //  Program will read from a file and add some value to it.
  4. //  Then write it to file again.
  5. #include<iostream>
  6. #include<fstream>
  7. #include<vector>
  8. #include<sstream>
  9. using namespace std;
  10.  
  11. void mainMenu();
  12. void ShowTheFile();
  13. vector<int> ReadFromFile();
  14. void WriteToFile();
  15. void AddValueToElementOfFile();
  16.  
  17. void ShowTheFile(){
  18.     //   loop to show the array
  19.  
  20.     vector<int> inputArray = ReadFromFile();
  21.     if(inputArray.size() == 0){
  22.         cout << "File is empty or not exist.\n";
  23.     }
  24.     else{
  25.         cout << "The file contains : " << endl;
  26.         for(int i : inputArray){
  27.             cout << i << ' ';
  28.         }
  29.         cout << endl << endl;
  30.     }
  31.     mainMenu();
  32. }
  33.  
  34. vector<int> ReadFromFile(){
  35.     ifstream readFile("file.txt");
  36.     string input;
  37.     vector<int> inputArray;
  38.     //  Loop to read all lines in file
  39.     while(getline(readFile, input)){
  40.         //  separating each word from sentence
  41.         stringstream s(input);
  42.         string number;
  43.         //  Loop to read each word and convert to int value
  44.         while(s >> number){
  45.             int k = stoi(number, nullptr, 10);
  46.             inputArray.push_back(k);
  47.         }
  48.     }
  49.     readFile.close();
  50.     return inputArray;
  51. }
  52.  
  53. void WriteToFile(){
  54.     ofstream writeFile("file.txt");
  55.     cout << "Enter number of input: ";
  56.     int n; cin >> n;
  57.     for(int i = 0; i < n; i++){
  58.         int k; cin >> k;
  59.         writeFile << k << ' ';
  60.     }
  61.     writeFile.close();
  62.     mainMenu();
  63. }
  64.  
  65. void AddValueToElementOfFile(){
  66.     vector<int> arrayInFile = ReadFromFile();
  67.     int value;
  68.     cout << "Enter the value to add with the elements of array: ";
  69.     cin >> value;
  70.     for(int i = 0; i < arrayInFile.size(); i++){
  71.         arrayInFile[i] = arrayInFile[i] + value;
  72.     }
  73.     ofstream writeFile("file.txt");
  74.     for(int i: arrayInFile){
  75.         writeFile << i << ' ';
  76.     }
  77.     writeFile.close();
  78.     mainMenu();
  79. }
  80.  
  81. void mainMenu(){
  82.     cout << "Read from the file: Press '1'" << endl
  83.          << "Take input and write to file: Press '2'" << endl
  84.          << "Take input value and add it to all values in file and the write it back : Press '3'" << endl
  85.          << "Exit : Press '4'" << endl
  86.          << endl
  87.          << "Enter your choice: ";
  88.     int choice; cin >> choice;
  89.  
  90.     switch(choice){
  91.     case 1:
  92.         ShowTheFile();
  93.         break;
  94.     case 2:
  95.         WriteToFile();
  96.         break;
  97.     case 3:
  98.         AddValueToElementOfFile();
  99.         break;
  100.     case 4:
  101.         return;
  102.         break;
  103.     default:
  104.         break;
  105.     }
  106. }
  107.  
  108. int main(){
  109.     mainMenu();
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement