Advertisement
selebry

zdgffdg

Sep 21st, 2022
932
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.33 KB | None | 0 0
  1. // SiAOD2 2.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
  2. //Создать два новых файла из значений исходного, переписав в один из них первую половину чисел исходного
  3. //, а в другой, оставшуюся часть. В исходный файл слить данные их двух новых файлов упорядоченными по возрастанию парами,
  4. //т.е. прочитать первые числа двух файлов, сначала в исходный файл записать меньшее из них, а за ним большее.
  5.  
  6.  
  7. #include <iostream>
  8. #include <fstream>
  9. #include <string>
  10. #include <vector>
  11. #include <random>
  12. #include <ctime>
  13. using namespace std;
  14. //чтение из текстового файла и вывод значений  на монитор
  15. ///постусловие 0 - sucess, 1 - error
  16. void fillFile(ofstream& otf);
  17. bool existFile(string filename);
  18. void createFile(string filename);
  19. void addNoteToFile(string filename, int n);
  20. int getCountNumbers(string filename);
  21. void printFile(string filename);
  22. int getNumber(string filename, int ind);
  23. void rewriteFiles(string filename);
  24. int main()
  25. {
  26.     setlocale(LC_ALL, "rus");
  27.     srand(time(NULL));
  28.     rewriteFiles("test1");
  29.    
  30.  
  31. }
  32.  
  33. void rewriteFiles(string filename) {
  34.     createFile("file1");
  35.     createFile("file2");
  36.     int count = getCountNumbers(filename),t,t1;
  37.     ifstream itf(filename+".txt"),itf1;
  38.     ofstream otf("file1.txt");
  39.     for (int i = 0; i < count / 2; i++) {
  40.         itf >> t;
  41.         otf << t<<' ';
  42.     }
  43.     otf.close();
  44.     otf.open("file2.txt");
  45.     for (int i = count/2; i < count; i++) {
  46.         itf >> t;
  47.         otf << t<<' ';
  48.     }
  49.     itf.close();
  50.     otf.close();
  51.     otf.open(filename + ".txt");
  52.     itf.open("file1.txt");
  53.     itf1.open("file2.txt");
  54.     while (itf >> t) {
  55.         itf1 >> t1;
  56.         if (t < t1)
  57.         {
  58.             otf << t << " ";
  59.             otf << t1 << "\n";
  60.         }
  61.         else {
  62.             otf << t1 << " ";
  63.             otf << t << "\n";
  64.         }
  65.     }
  66.     if (itf1 >> t1) otf << t1;
  67.     otf.close();
  68.     itf.close();
  69.     itf1.close();
  70. }
  71. void fillFile(ofstream& otf) {
  72.     int count = rand() % 91 + 10, t;
  73.     while (count != 0) {
  74.         t = rand() % count + 1;
  75.         for (int i = 0; i < t; i++) otf << rand() % 2001 - 1000 << ' ';
  76.         count -= t;
  77.         if (count != 0)otf << '\n';
  78.     }
  79. }
  80. bool existFile(string filename) {
  81.     ifstream itf(filename + ".txt");
  82.     itf.close();
  83.     return itf ? true : false;
  84. }
  85. void createFile(string filename) {
  86.     ofstream otf(filename + ".txt");
  87.    // fillFile(otf);
  88.     otf.close();
  89. }
  90. void addNoteToFile(string filename, int n) {
  91.     ofstream otf(filename + ".txt", ios::app);
  92.     otf << n << " ";
  93.  
  94. }
  95. int getCountNumbers(string filename) {
  96.     if (!existFile(filename)) {
  97.         cerr << "Такого файла не существует!\n";
  98.         return -1;
  99.     }
  100.     ifstream otf(filename + ".txt");
  101.     int c = 0, t;
  102.     while (otf >> t) c++;
  103.     return c;
  104.  
  105. }
  106. void printFile(string filename) {
  107.     if (!existFile(filename)) {
  108.         cerr << "Такого файла не существует!\n";
  109.         return;
  110.     }
  111.     ifstream itf(filename + ".txt");
  112.     string t;
  113.     while (!itf.eof()) {
  114.         getline(itf, t);
  115.         cout << t << '\n';
  116.     }
  117. }
  118. int getNumber(string filename, int ind) {
  119.     if ((ind > getCountNumbers(filename) || ind < 1) || !existFile(filename)) {
  120.         cerr << "Индекс > чем кол-во чисел или файла не существует!\n";
  121.         return -1;
  122.     }
  123.     ifstream itf(filename + ".txt");
  124.     int c = 0, t;
  125.     while (itf >> t) if (++c == ind) return t;
  126. }
  127.  
  128.  
  129.  
  130. ///////////////////////////////////////////////////////////////////////////////
  131. #include <string.h>
  132. #include <iostream>
  133. #include <fstream>
  134.  
  135. using namespace std;
  136.  
  137. struct Worker {
  138.     string Name;     //Фамилия сотрудника
  139.     float salary;       //Заработная плата
  140.     int age;            //Возраст рабочего
  141. };
  142.  
  143. int main()
  144. {
  145.     const char* FName = "Worker.bin"; //Путь к файлу. Вписывайте свой.
  146.     Worker teacher;     //teacher - Записываемый в файл объект структуры
  147.     Worker w1;          //w1 - читаемый из файл объект структуры
  148.     ofstream otf(FName);
  149.     // fillFile(otf);
  150.     otf.close();
  151.     /*ЗАПОЛНЯЕМ СТРУКТУРУ*/
  152.     teacher.Name="Pupkin";    //в случае работы с Си-строками, нужно копировать строку в строку
  153.     teacher.age = 30;
  154.     teacher.salary = 1523.99;
  155.  
  156.     /*Записываем структуру в файл*/
  157.     ofstream f1(FName, ios::binary);
  158.     f1.write((char*)&teacher, sizeof(teacher));
  159.     f1.close();
  160.  
  161.  
  162.     /*Читаем структуру из файла */
  163.     ifstream f2(FName, ios::binary | ios::in);
  164.     f2.read((char*)&w1, sizeof(teacher));
  165.     f2.close();
  166.  
  167.     /*Вывод данных на экран*/
  168.     cout << w1.Name << '\t' << w1.age << '\t' << w1.salary << '\n';
  169.  
  170.  
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement