Such a question - how to write and read lines from a binary file? The fact is that I write the vector vector<string>words elementwise to the file:

 fstream tab("tab.txt", ios::binary | ios::out); for(int i = 0; i < words.size(); i++){ tab.write((char*)&words[i], 20); } tab.close(); 

and read into a temporary array (skipping every second):

  char x[21]; fstream show("tab.txt", ios::binary | ios::in); show.seekg(0, ios::beg); int length = show.tellg(); for(int i = 0; i < words.size()/2; i++){ show.read((char*)&x, 20); cout << x << endl; length+=20; show.seekg(length); } show.close(); 

displays incomprehensible characters. Tell a newbie, please, how to do it right?

  • Explain exactly what you want to write to the file. - user6550
  • I speak, a vector. A vector consists of words (zero and every even element - words) and their sequence numbers (every odd element). you need to create a table using a binary file from fixed-length records and create an ordered list. - alalambda
  • It is necessary to modify the following program under the words: pastebin.com/NaLWWuMu - alalambda

1 answer 1

 tab.write((char*)&words[i], 20); 

Explain exactly what you want to write to the file? If the garbage, then it is he who writes it there, quite successfully.

To preserve such complex structures as the plus vector, you cannot write raw data. This is the service information of the vector class itself, which has nothing to do with your data. For each type of data stored in a vector, it is necessary to develop the means of recording independently. The easiest way, of course, is to write this type of data as text:

 for(size_t i = 0; i < words.size(); i++) { tab << words[i] << "\n" << i << "\n"; } 

Or even simpler (why write a sequence number, if the lines go in order anyway?):

 for(size_t i = 0; i < words.size(); i++) { tab << words[i] << "\n"; } 

But if you just want the binary format, then somehow, for example (do not forget that the lines are of different lengths):

 fstream tab("tab.txt", ios::binary | ios::out); for(size_t i = 0; i < words.size(); i++) { size_t size = words[i].size(); // пишем в файл длину строки: tab.write( (char *)&size, sizeof(size) ); // теперь саму строку: tab.write( words[i].c_str(), size ); } tab.close(); 

Well, read:

 fstream show("tab.txt", ios::binary | ios::in); for( size_t i = 0; i < words.size(); i++ ) { size_t size; // читаем длину очередной строки: show.read( (char *)&size, sizeof(size) ); // читаем саму строку: char buf[size + 1]; show.read( buf, size ); buf[size] = 0; // нечётные записи пропускаем: if( !(i % 2) ) { // с чётными что-то делаем: cout << i < ": " << buf << "\n"; } } show.close(); 

To make entries of one length, you can first calculate the maximum length of the string in the vector, and create entries as structures:

 struct _rec { size_t order; char word[1]; } *rec = (_rec *)new char[ sizeof(_rec) + N ]; for( size_t i = 0; i < words.size(); i++ ) { memset( rec->word, 0, N+1 ); strcpy( rec->word, words[i].c_str() ); rec->order = i; // не знаю что сюда писать и откуда брать tab.write( (char *)rec, sizeof(_rec)+N ); } 

Well:

 for( size_t i = 0; i < words.size(); i++ ) { show.read( (char *)rec, sizeof(_rec)+N ); cout << rec->order << ": " << rec->word << "\n" } 

PPS It is possible, of course, then all this (reading and writing elements) can be written as operator << and operator >> .

  • file must be written in the vector. A vector consists of words (zero and every even element - words) and their sequence numbers (every odd element). you need to create a table using a binary file from fixed-length records and create an ordered list. - alalambda
  • Added something in reply - user6550
  • I will try to understand, of course, but all this hemorrhoids, which I am trying to implement, are from the formulation of the task. need fixed-length entries. The user himself must specify a sequence of words. for example (input): London 2 is 1 the 5 capital 3 .... contents of the list: is London the .... - alalambda
  • Well, adding another number (sequence number) to the record is not a problem. If you need to necessarily write the same length, but now I will add. - user6550
  • with the record, the output of both words and numbers, coped. thank. it now remains to implement the alternate output instead of the usual using seekg. - alalambda