axeefectushka

Untitled

Feb 14th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.37 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "stdio.h"
  3. #include <iostream>
  4. #include <string>
  5. #include <string.h>
  6. #include <iomanip>
  7. #include <io.h>
  8. #include <Windows.h>
  9. using namespace std;
  10. #define _CRT_SECURE_NO_WARNINGS
  11.  
  12. char* sozdanie(char*, FILE*);
  13. char* zapis(char*, FILE*);
  14. char* read(char*, FILE*);
  15. char* text(char*, char*,FILE*);
  16. struct rasp
  17. {
  18.     int number;
  19.     double otpr;
  20.     char mesto[50];
  21.     double prib;
  22. }avtobus,*spisok;
  23.  
  24. int main()
  25. {
  26.     char txtfname[100];
  27.     char filename[100];
  28.     strcpy(filename, "lala");
  29.    
  30.     FILE *fl = NULL;
  31.    
  32.     /*bool bp = true;
  33.     do
  34.     {
  35.         switch (menu())
  36.         {
  37.         case 1: cout << endl << "imya faila: "  << sozdanie(filename, fl) << endl << endl; break;
  38.         case 2: cout << endl << "imya faila: "  << zapis(filename, fl) << endl << endl; break;
  39.         case 3: cout << endl << "imya faila: "  << read(filename, fl) << endl << endl; break;
  40.         case 4: cout << endl << "imya txtfaila: " << text(txtfname, filename, fl) << endl << endl; break;
  41.         case 5: bp = false; break;
  42.         default: cout << "Nepravilniy vibor."; break;
  43.         }
  44.     */} while (bp);
  45.     return 0;
  46. }
  47.  
  48. //меню
  49. int menu()
  50. {
  51.     cout << "Vybirite: " << endl;
  52.     cout << "1. Sozdat' binarnii fail" << endl;
  53.     cout << "2. Zapisat dannie v fail" << endl;
  54.     cout << "3. Otkrit fail, prochitat, vivesti resultat" << endl;
  55.     cout << "4. Vivesti v txt fail" << endl;
  56.     cout << "5. Exit" << endl;
  57.     int i;
  58.     cin >> i;
  59.     return i;
  60. }
  61.  
  62. char* sozdanie(char* filename, FILE* fl)
  63. {
  64.     cout << "Vvedite nazvanie faila: ";
  65.     gets_s(filename, 100);
  66.     strcat(filename, ".txt");//соединяет две строки
  67.     fl = fopen(filename, "w");
  68.     if ((fl = fopen(filename, "wb")) == NULL)
  69.     {
  70.         cout << "Oshibka pri sozdanii" << endl;
  71.         filename = "oshibka";
  72.     }
  73.     fclose(fl);
  74.     return filename;
  75. }
  76.  
  77. char* zapis(char* filename, FILE* fl)
  78. {
  79.     if ((fl = fopen(filename, "ab")) == NULL)
  80.     {
  81.         return "oshibka pri otkritii";
  82.     }
  83.     char ch;
  84.     do
  85.     {
  86.         cout << "Vvedite nomer poezda: "; cin >> avtobus.number;
  87.         cout << "Vvedite vremya otpravleniya: "; cin >> avtobus.otpr;
  88.         rewind(stdin);
  89.         cout << "Vvedite punkt naznacheniya: "; gets_s(avtobus.mesto, 19);
  90.         cout << "Vvedite vremya pribytiya: "; cin >> avtobus.prib;
  91.         fwrite(&avtobus, sizeof(rasp), 1, fl);
  92.         cout << endl << "Prodolzhit vvod? y/n: ";
  93.         cin >> ch;
  94.         cout << endl;
  95.     } while (ch != 'n');
  96.     fclose(fl);
  97.     return filename;
  98. }
  99. char* read(char* filename, FILE* fl)
  100. {
  101.     if ((fl = fopen(filename, "rb")) == NULL)
  102.     {
  103.         return "Oshibka pri otkritii";
  104.     }
  105.     //int n = _filelength(_fileno(fl)) / sizeof(rasp);
  106.     //for (int i = 0; i < n; i++)
  107.     {
  108.         fread(&avtobus, sizeof(rasp), 1, fl);//чтение из файла
  109.         rewind(stdin);
  110.         printf("%d, %f, %s, %f\n", avtobus.number, avtobus.otpr, avtobus.mesto, avtobus.prib);
  111.     }
  112.     fclose(fl);
  113.     return filename;
  114. }
  115.  
  116. char* text(char* txtfname, char* filename, FILE* fl)
  117. {
  118.  
  119.     FILE *ft;
  120.     cout << "Vvedite textfile name: ";
  121.     rewind(stdin);
  122.     gets_s(txtfname, 29);
  123.     if ((ft = fopen(txtfname, "w")) == NULL)
  124.         return "Oshibka pri sozdanii textovogo faila.";
  125.     if ((fl = fopen(filename, "rb")) == NULL)
  126.     {
  127.         remove(txtfname);
  128.         return "Oshibka pri otkritii.";
  129.     }
  130.     //int n = _filelength(_fileno(fl)) / sizeof(rasp);
  131.     //for (int i = 0; i < n; i++)
  132.     {
  133.         fread(&avtobus, sizeof(rasp), 1, fl);
  134.         fprintf(ft, "%d\n %f\n %s\n %f\n\n", avtobus.number, avtobus.otpr, avtobus.mesto, avtobus.prib);
  135.     }
  136.     fclose(fl);
  137.     fclose(ft);
  138.     return txtfname;
  139. }
Add Comment
Please, Sign In to add comment