Are you writing in C ++? So let's do a normal streaming input.
#include <string> #include <vector> #include <fstream> using namespace std; struct Tourism { string country; int number_of_vouchers; float cost_of_voucher; string hotel_name; int hotel_status; int tour_duration; }; istream& operator >> (istream& s, Tourism& t) { return s >> t.country >> t.number_of_vouchers >> t.cost_of_voucher >> t.hotel_name >> t.hotel_status >> t.tour_duration; } int main(int argc, char* argv[]) { vector<Tourism> v; fstream s { тут имя файла }; Tourism t; while (s >> t) v.push_back(t); }
And do not use samopisnymi containers at the risk of a mistake.
Update :
The above solution only works correctly if there are no spaces in the lines. If there are spaces, s >> t.country reads only up to the first space. The fact is that reading a string works before the whitespace, and not to the end of the string, so it’s not so easy to subtract the entire string.
The problem is that we can not switch to using getline for t.country : getline and streaming through >> not friendly. Entering through >> ignores the start whitespace characters, and leaves the final \n in the string, but not getline , so the code will not work when they are mixed.
The solution that is usually proposed is to change for our stream s what it considers to be a space character. If we tell him that only \n is a space, he will read the lines to the end!
This requires the following spell:
int main(int argc, char* argv[]) { fstream s { тут имя файла }; // заведём пустую таблицу категорий символов ctype<char>::mask mask[ctype<char>::table_size] = {}; // обозначим `\n` как пробельный символ mask['\n'] = ctype_base::space; // объясним потоку, что он должен пользоваться этой таблицей s.imbue(locale(s.getloc(), new ctype<char>(mask))); vector<Tourism> v; Tourism t; while (s >> t) v.push_back(t); }
Now the code works.
Perhaps a simpler ideologically variant is to read everything via getline and refuse >> to enter. To convert the resulting string to a value, we use istringstream , as usual. The code is a little less elegant, but simple and straightforward (which is not so bad):
#include <string> #include <vector> #include <fstream> #include <sstream> using namespace std; struct Tourism { string country; int number_of_vouchers; float cost_of_voucher; string hotel_name; int hotel_status; int tour_duration; }; istream& getrecord(istream& s, Tourism& t) { string line; // пропускаем пустые строки while (line == "" && getline(s, line)) ; // если поток в хорошем состоянии, мы прочитали непустую строку if (s) t.country = line; if (getline(s, line)) istringstream(line) >> t.number_of_vouchers; if (getline(s, line)) istringstream(line) >> t.cost_of_voucher; if (getline(s, line)) t.hotel_name = line; if (getline(s, line)) istringstream(line) >> t.hotel_status; if (getline(s, line)) istringstream(line) >> t.tour_duration; return s; } int main(int argc, char* argv[]) { vector<Tourism> v; fstream s { тут имя файла }; Tourism t; while (getrecord(s, t)) v.push_back(t); }
Magic with a locale is not needed.
struct list_head { List *first, *last} head = {0, 0}; List *t; while(t = get_list_elem(fin)) add_to_list(&head, t);Well, inget_list_elem()place your code returningbeginor 0 if there is nothing more to read. Withadd_to_list(), I hope you canadd_to_list()it. - avp