Multiple File I / O

advertisements

Sorry about the lame title.

I have a project I'm working on and I would appreciate any suggestions as to how I should go about doing the IO stuff.

Ok, I have 3 text files. One file contains many many lines of text. This is the same for the other 2 files. Let's call them File1, File2 and File3.

I need to create a text file, for the sake of explaining, I'll name it Result.txt.

Here's what needs to be done:

  1. Extract the first line of text from File1 and append it to Result.txt.
  2. Extract the first line of text from File2 and append it to the end of the first line in Result.txt.

  3. Extract the first line of text from File3 and append it to the end of the first line in Result.txt.

  4. Create a new line in Result.txt

  5. Repeat from 1 to 4.

Note: These files can be quite large.

Anyone have any ideas as to how to best approach this?

Thank you

-

Thank you all for your very helpful answers. I've learned alot from your advice and code samples!


I think here you can use the philosophy of the producer/consumer. You can have a thread (producer) reading each line from your 3 source files, concatenating the 3 lines and put the result in a queue (in memory). Meanwhile, another thread (consumer) is constantly reading the from this queue and writing the to your result.txt file.

1: producer thread
   Reads line n from file 1,2 and 3
   concatenates the contents of the 3 lines and push_back in the queue

2: consumer thread
   Check if the queue is empty.
   If not, pop the first item in the queue and write to the result.txt