You are here:

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
Doe John
Doe Jane
Wilson Kobe

In_stream file 2

out_stream file
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.
End while

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 The first is about the C++ IOStreams library, which you may like to look at and perhaps even download.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2017 All rights reserved.