Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int getFileSize(const string &fileName);  //Task 1
  7.  
  8. void outputFile1(const string &fileName); //Task 2
  9.  
  10. void outputFile2(const string &fileName);
  11.  
  12. int main()
  13. {
  14.     char exit = '\0';    // To exit program
  15.     string fileName = "letters.txt";
  16.  
  17.     cout << "Task 1" << endl;
  18.     cout << "There are " << getFileSize(fileName) << " byte(s) in the file " << fileName << endl;
  19.  
  20.     cout << endl << "Task 2" << endl;
  21.    
  22.     outputFile1(fileName);
  23.    
  24.     cout << "Task 3" << endl;
  25.    
  26.     outputFile2(fileName);
  27.  
  28.     cin >> exit;
  29.     while (exit != '\0')
  30.         return 0;
  31. }
  32.  
  33. int getFileSize(const string & fileName)
  34. {
  35.     fstream file(fileName, ios::in);
  36.     int size = 0;   //To store size
  37.  
  38.     file.seekg(0, ios::end);
  39.     size = file.tellg();
  40.     file.close();
  41.     return size;
  42. }
  43.  
  44. void outputFile1(const string &fileName)
  45. {
  46.     char ch;
  47.     string outName = "output1.txt";  //To name output file
  48.  
  49.     fstream inFile(fileName, ios::in);
  50.     ofstream outFile(outName);
  51.  
  52.     cout << "Content of output1.txt: ";
  53.  
  54.     while (!inFile.eof())
  55.     {
  56.         inFile.get(ch);
  57.         if (inFile.eof())
  58.             break;
  59.         cout << ch;
  60.         outFile.put(ch);
  61.     }
  62.    
  63.     inFile.close();
  64.  
  65.     cout << endl;
  66.     return;
  67. }
  68.  
  69. void outputFile2(const string & fileName)
  70. {
  71.     char ch;
  72.     string outName = "output2.txt";  //To name output file
  73.  
  74.     fstream inFile(fileName, ios::in);
  75.     ofstream outFile(outName);
  76.  
  77.     cout << "Content of output2.txt: ";
  78.  
  79.     while (!inFile.eof())
  80.     {
  81.         int i = 0;
  82.         inFile.seekg(1, ios::beg);
  83.         inFile.get(ch);
  84.         if (inFile.eof())
  85.             break;
  86.         cout << ch;
  87.         outFile.put(ch);
  88.         i++;
  89.     }
  90.  
  91.     cout << endl;
  92.     return;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement