简体   繁体   中英

reading input from middle of a file c++ using for loops

I am trying to write a mock bookstore program. The program is to read data from one input file and write it to one output file. I have several constraints. I can only use one input file, and one output file. I must use two for loops. One for each of the five customers listed in the file, and another for each of the books they are going to buy, with a possibility of up to 5 books.

The first for loop takes input for customer information, and the number of books to be bought(so next for loop will know how many times to iterate). Second loop takes information about the books.

Problem: I know how to keep an output file from writing over itself, but how do I get the input file to read from where the last iteration of the loop left off.

For some reason when I try to post my code it is reformatting it, and adding characters that shouldn't be there. But here is psuedo-code for what I am trying to do.

for(count=1; count<=5; count++)
   {
   fin.open("input.txt");
   fin << user information(including name, address and number of books)
   fout.open("output.txt" ios::app);
   fout >> user information

   for(count1=1; count1<=numbbooks; count1++)
      {
      fin << book information(Name, Author, Price)
      fout >> book information
      }

I know how to use getline, fin, to take in the data, however after the first iteration of each loop it begins to read from the beginning of the file. I need it to read from where the previous iteration finished. I understand ios:app for the output.

sample input file

user name
address
number of books
book1
price1
book2
user name2
address
number of books
book2.1
price2.1

etc. There is actually more information than that. Any help would be greatly appreciated. Sorry for the newb question. I searched and searched before posting here.

Simple don't open and close it each time round the loop. Open it once before the outer loop, and close once after the same loop.

When you open a file the read/write position is normally set to the start of the file (ios::app is an exception as you've already found out).

You have your << and >> symbols the wrong way round.

Create a struct (or class) for UserInformation and for BookInformation then create these overloads:

std::ostream& operator<<( std::ostream& os, const UserInformation& );
std::istream& operator>>( std::istream& is, UserInformation& );
std::ostream& operator<<( std::ostream& os, const BookInformation& );
std::istream& operator>>( std::istream& is, BookInformation& );

each of those will read or write to your struct members.

In your outer loop, you should also only be reading/writing, not opening the files, so move those outside of the loops so:

fin.open("input.txt");         
fout.open("output.txt" ios::app);      

// verify both fin and fout worked correctly then
UserInformation userInfo;
while( fin >> userInfo ) // or use a count if you only want to read a limited number
{
     // continue with reading books and outputting..
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM