Using ifstream to print integers from text files

I have an assignment where I am supposed to read multiple files containing integers (one on each line) and merge them into a output text file after sorting them. I am new to C++ so I do not know how everything works. I am testing my program with two .txt files. The first file is called fileone.txt, contains 1,2,7 (I do not know how to format this but they are all on different lines.) The second file is called filetwo.txt, and contains 1,3,5,9,10 (again every integer is on a different line).

I have written the following code which opens both files and prints the contents.

#include <iostream> #include <fstream> using namespace std; int main(int argc, char** argv) { ifstream iFile; ifstream jFile; iFile.open("fileone.txt"); jFile.open("filetwo.txt"); int int1 = 0; int int2 = 0; if (iFile.is_open() && jFile.is_open() ){ while (iFile.good() || jFile.good() ) { iFile >> int1; jFile >> int2; cout << "From first file:" << int1 << endl; cout << "From second file:" << int2 << endl; } } iFile.close(); jFile.close(); return 0; }

The output of this program is <img src="https://i.stack.imgur.com/L6gjJ.png" alt="enter image description here">

The problem I am having is the last number in the first file gets printed multiple times. The output I want is to stop printing after printing the last integer from the file. The problem only appears when the second file contains more integers than the first one. Is there a way to stop printing from the first file when it reaches the end and while still print all the numbers from the second file?


This will do the trick

while (iFile || jFile) { if(iFile >> int1) // make sure the read succeeded!! cout << "From first file:" << int1 << endl; if(jFile >> int2) // make sure the read succeeded!! cout << "From second file:" << int2 << endl; }

You should only really use data if you checked to see if it was successfully read.


Consider changing the <strong>while</strong> loop as follow

while (iFile.good() || jFile.good() ) { iFile >> int1; jFile >> int2; int c = iFile.peek(); int d = jFile.peek(); if (c == EOF) { if (!iFile.eof()) cout << "From first file:" << int1 << endl; } if (d == EOF) { if (!jFile.eof()) cout << "From second file:" << int2 << endl; } }

The thing is to check the end of file and handle if to print it. You can use <strong>eof()</strong> function as above.

I haven't checked the code. But the logic should be correct.


  • How to communicate multiples component
  • Format file date YYYYMMDD in batch
  • ActionBar three-dot dropdown opens at the wrong place
  • How to get from a tuple to a tuple of references to elements in the tuple?
  • How should I add a stationary progress bar to a C++ program that produces terminal output (in Linux)
  • Find substring in string using locale
  • Declare a C++ class without defining it in the current translation unit
  • Access 2007 forms with parameterized RecordSource
  • C++ Boost ptr_map serialization error
  • Need help to stop program terminating without users consent
  • How do I fix “The program issued a command but the command length is incorrect.” error when calling
  • C++ String tokenisation from 3D .obj files
  • Recursion Control flow
  • WP7 difficulties binding data to listbox itemssource - won't refresh
  • Android custom URI scheme incorrectly encoded when type in browser
  • Ruby 1.8.6 Array#uniq not removing duplicate hashes
  • Configure Spring's MappingJacksonHttpMessageConverter
  • HTML5 video only works in IE. The other browsers shows the black screen
  • In-place sed command not working
  • Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0)
  • AppleScript : find open tab in safari by name and open it
  • Z3: Convert between FP and BitVector?
  • Appending Character to Character Array In C
  • How to determine if there are bytes available to be read from boost:asio:serial_port
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • Ensure fsync did its job
  • Counter field in MS Access, how to generate?
  • Convert array of 8 bytes to signed long in C++
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • How to pass list parameters for each object using Spring MVC?
  • Buffer size for converting unsigned long to string
  • Hits per day in Google Big Query
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How to get Windows thread pool to call class member function?
  • How can I remove ASP.NET Designer.cs files?
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal