Advertisement
Petro_zzz

10_26_cw

Oct 26th, 2022
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. void read_by_char(string filename) {
  8.     ifstream fin;
  9.     fin.open(filename);
  10.     if (fin.is_open()) {
  11.         char ch;
  12.         while (!fin.eof()) {
  13.             ch = NULL;
  14.             fin.get(ch);
  15.             cout << ch;
  16.         }
  17.         fin.close();
  18.     }
  19.     else {
  20.         cout << "Can not open the file\n";
  21.     }
  22. }
  23.  
  24. void read_by_line(string filename) {
  25.     ifstream fin;
  26.     fin.open(filename);
  27.     if (fin.is_open()) {
  28.         string str;
  29.         while (!fin.eof()) {
  30.             str = "";
  31.             getline(fin, str);
  32.             //fin.getline(); это не удобно использовать
  33.             cout << str;
  34.         }
  35.         fin.close();
  36.     }
  37.     else {
  38.         cout << "Can not open the file\n";
  39.     }
  40. }
  41.  
  42. void write_to_file(string filename) {
  43.     ofstream fout;
  44.     fout.open(filename);
  45.     if (fout.is_open()) {
  46.         int x = 0;
  47.         while (x <= 10)
  48.             fout << x++ << " ";
  49.         fout.close();
  50.         cout << "File ready\n";
  51.     } else {
  52.         cout << "Error with the file\n";
  53.     }
  54. }
  55.  
  56. void read_from_file(string filename) {
  57.     ifstream fin;
  58.     fin.open(filename);
  59.     if (fin.is_open()) {
  60.         string x;
  61.         int res = 0;
  62.         while (!fin.eof()) {
  63.             fin >> x;
  64.             if((x[0] > '0') && (x[0] < '9'))
  65.                 res += stoi(x);
  66.         }
  67.         fin.close();
  68.         cout << "File ready\n" << "Res: " << res << endl;
  69.     }
  70.     else {
  71.         cout << "Error with the file\n";
  72.     }
  73. }
  74.  
  75.  
  76.  
  77. void main() {
  78.     setlocale(LC_ALL, "Russian");
  79.     read_from_file("out.txt");
  80.     //write_to_file("out.txt");
  81.  
  82.  
  83.     /*string filename = "my_file.txt";
  84.     //read_by_char(filename);
  85.     string filename2 = "x64/Release/my_file_for_write1.txt";
  86.     ofstream fout;  // cout
  87.     fout.open(filename2, ios_base::app);
  88.     int x = 14;
  89.     if (fout.is_open()) {
  90.         fout << "Hello all\n Кирилица Это Была" << endl;
  91.         fout << x << endl;
  92.         fout.close();
  93.     }
  94.     else {
  95.         cout << "Error with the file\n";
  96.     }
  97.  
  98.     */
  99. }
  100.  
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement