Advertisement
minh1999

file_example

Jul 27th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4. #define FILENAME "input.txt"
  5. #define SIZE 50
  6. using namespace std;
  7.  
  8. typedef struct
  9. {
  10.     char name[SIZE];
  11.     int age;
  12.     int height;
  13.     char test1[SIZE];
  14.     char test2[SIZE];
  15.     char test3[SIZE];
  16. } Patient;
  17.  
  18. Patient patient_list[500];
  19.  
  20. void readUntil(fstream &f,char *buf,char delim,int len)
  21. {
  22.     char tmp;
  23.     do
  24.     {
  25.         tmp = f.get();
  26.         int len_tmp = strlen(buf);
  27.         buf[len_tmp] = tmp;
  28.         buf[len_tmp + 1] = '\0';
  29.     } while ((tmp != delim) && (strlen(buf)<len-1));
  30.     buf[strlen(buf)-1] = '\0';
  31. }
  32.  
  33. int main()
  34. {
  35.     fstream file;
  36.     file.open(FILENAME);
  37.     if (file.is_open())
  38.     {
  39.         char title[SIZE];
  40.         int num_of_records;
  41.         file >> title;
  42.         file.ignore(225, '\n');
  43.         //cout << title << endl;
  44.         //get number of records
  45.         while (file.get() != ':')
  46.             continue;
  47.         file.get(); //get whitespace
  48.         file >> num_of_records;
  49.         file.ignore(225, '\n');
  50.         //cout << num_of_records << endl;
  51.  
  52.         for (int i = 0; i < num_of_records; i++)
  53.         {
  54.             char temp[SIZE] = { '\0' };
  55.             readUntil(file, patient_list[i].name, ',', SIZE);
  56.             //cout << patient_list[i].name << endl;
  57.             readUntil(file, temp , ',', SIZE);
  58.             patient_list[i].age = atoi(temp);
  59.             temp[0] = '\0';
  60.             readUntil(file, temp, ',', SIZE);
  61.             patient_list[i].height = atoi(temp);
  62.             readUntil(file, patient_list[i].test1, ',', SIZE);
  63.             readUntil(file, patient_list[i].test2, ',', SIZE);
  64.             readUntil(file, patient_list[i].test3, '\n', SIZE);
  65.         }
  66.  
  67.         patient_list[0].age++;
  68.         //write to file
  69.         file.seekp(ios::beg);
  70.         file << title << endl;
  71.         file << "Number of records: " << num_of_records << endl;
  72.         for (int i = 0; i < num_of_records; i++)
  73.         {
  74.             file << patient_list[i].name << ",";
  75.             file << patient_list[i].age << ",";
  76.             file << patient_list[i].height << ",";
  77.             file << patient_list[i].test1 << ",";
  78.             file << patient_list[i].test2 << ",";
  79.             file << patient_list[i].test3 << endl;
  80.         }
  81.     }
  82.     file.close();
  83.     //system("pause");
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement