C++/in_stream text files
How would take characters from one in_stream text file and integers from a different in_stream file and out put them together into one out_stream text file going row by row?
In_stream file 1
In_stream file 2
Doe John 00252
Doe Jane 48645
Wilson Kobe 15656
There are the same amount of names as numbers.
I am not going to write the program for you but this is the outline:
Open input file 1 for reading on std::ifstream 1
Open input file 2 for reading on std::ifstream 2
Open output file for writing, truncating existing data unless you wish to append data on std::ofstream.
In each case check you have a stream in a good state using the likes of the good(), fail() or bad() stream member functions.
If you have valid streams then:
While all streams good:
Read line from std::ifstream 1 into a character array using
the getline() member function.
Read line from std::ifstream 2 into another character array using
the getline() member function.
If both streams good then write character array data from each read to
the ofstream, using the insertion operator (operator<<) for example.
If all streams not bad and both input streams' eof() are true then there are no errors, otherwise something went wrong.
Close all files.
Note that you can open files during construction and they are closed during destruction of the associated std::ifstream or std::ofstream.
Of course you can vary the logic a little or use alternative functions - for example you could read the data from the input streams one word at a time using the extraction operator (operator>>) then write them out using some other method - maybe composing the output string in one go or reading a word, writing a word etc. However my method seems the easiest as you read whole lines in one go and only write data if you have a complete set of input data.
You can also vary the error and end of file checking. For example you might check that input stream one is not bad before reading input stream two, or you might check that input stream one is good before reading input stream two.
Note that good is not the same as not bad, and bad is different from fail. A stream has a good bit, a bad bit, a fail bit and an eof (end of file) bit.
Good means that none of the other state bits are set - so good is not fail and not bad and not end of file.
Bad means the stream is somehow corrupted or data has been lost.
Fail mean an operation was not processed correctly but the stream itself is OK. The fail bit is normally set as a result of a formatting error such as reading an integer and a letter was encountered first off.
End of file means the stream has read _past_ the end of the stream. So the eof bit is only set on the read _after_ reading the last character, and that the fail bit is also set as the read failed.
good() returns the state of the good bit, bad() returns the state of the bad bit, but fail() returns true if _either_ the bad bit is set _or_ the fail bit is set.
You will also note that I have not bothered treating the integers as integers but read them as strings. This is because I have assumed that your file is composed of strings of digit characters, as this appears to be what you have from you example data. As such they can be interpreted as character data as easily (or more easily even) than as integers. Again you could read the integers in one at a time as integers then write them out again but the only use I can see for this is to check the data is of the correct type - presumably by checking the fail bit of std::ifstream 2, however this means the program does more work converting the characters to integers and back again. So once more, this is your choice.
I would suggest you get yourself a good C++ standard library reference if you do not already have one. I find that "The C++ Standard Library A Tutorial and Reference" by Nicolai M. Josuttis is invaluable as a reference - and in fact I am using it now to make sure I get the details right in this reply. If you need more detail on the IOStream library itself then look at "Standard C++ IOStreams and Locales" by Langer and Kreft - although I must admit that I usually find that Josuttis is more than adequate in most cases.
I found this page with online documents http://www.digilife.be/quickreferences/PT.htm
. The first is about the C++ IOStreams library, which you may like to look at and perhaps even download.