Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cstdlib>
- #define FILENAME "input.txt"
- #define SIZE 50
- using namespace std;
- typedef struct
- {
- char name[SIZE];
- int age;
- int height;
- char test1[SIZE];
- char test2[SIZE];
- char test3[SIZE];
- } Patient;
- Patient patient_list[500];
- void readUntil(fstream &f,char *buf,char delim,int len)
- {
- char tmp;
- do
- {
- tmp = f.get();
- int len_tmp = strlen(buf);
- buf[len_tmp] = tmp;
- buf[len_tmp + 1] = '\0';
- } while ((tmp != delim) && (strlen(buf)<len-1));
- buf[strlen(buf)-1] = '\0';
- }
- int main()
- {
- fstream file;
- file.open(FILENAME);
- if (file.is_open())
- {
- char title[SIZE];
- int num_of_records;
- file >> title;
- file.ignore(225, '\n');
- //cout << title << endl;
- //get number of records
- while (file.get() != ':')
- continue;
- file.get(); //get whitespace
- file >> num_of_records;
- file.ignore(225, '\n');
- //cout << num_of_records << endl;
- for (int i = 0; i < num_of_records; i++)
- {
- char temp[SIZE] = { '\0' };
- readUntil(file, patient_list[i].name, ',', SIZE);
- //cout << patient_list[i].name << endl;
- readUntil(file, temp , ',', SIZE);
- patient_list[i].age = atoi(temp);
- temp[0] = '\0';
- readUntil(file, temp, ',', SIZE);
- patient_list[i].height = atoi(temp);
- readUntil(file, patient_list[i].test1, ',', SIZE);
- readUntil(file, patient_list[i].test2, ',', SIZE);
- readUntil(file, patient_list[i].test3, '\n', SIZE);
- }
- patient_list[0].age++;
- //write to file
- file.seekp(ios::beg);
- file << title << endl;
- file << "Number of records: " << num_of_records << endl;
- for (int i = 0; i < num_of_records; i++)
- {
- file << patient_list[i].name << ",";
- file << patient_list[i].age << ",";
- file << patient_list[i].height << ",";
- file << patient_list[i].test1 << ",";
- file << patient_list[i].test2 << ",";
- file << patient_list[i].test3 << endl;
- }
- }
- file.close();
- //system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement