Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7725367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:55:11+00:00 2026-06-01T04:55:11+00:00

I am building a program which reads text files, containing a list of numbers.

  • 0

I am building a program which reads text files, containing a list of numbers. These lists contain a series of sorted numbers, each which may contain a varying amount of values. For example, file 1 may have a list of 2 numbers and file 2 may have a list of 20.

The point of the program is to read each of these text files (which contain numbers in ascending order) line by line, then put them into a new text file in ascending order. However, if one list is really short, then I will get an eof() error, causing the file reader to go into the fail state, which won’t allow it to read the remaining values in the longer text file.

Here is my code which iterates over the two text files:

int main() {
int numbers1 = 0, numbers2 = 0, fillCounter = 0, iteration = 0;
char grabNext = 'b';
ifstream file1, file2, fileFiller;
ofstream fout;
file1.open("numbers1.txt");
file2.open("numbers2.txt");
fout.open("output.txt");

//Check and see if the opening operation was successful
if (file1.fail() || file2.fail()) {
    cout << "The progran could not open the related necessary files. Check and see if they exist!"
         << endl;

    exit(1);
}

do {
//In theory, whenever the file1 gives an eof, I should be able to iterate over the remaining lines of file2
    if (file1.eof()) {
        do {
            fout << numbers2 << endl;
        } while(!file2.eof());

        break;
    }

//In theory, whenever the file2 gives an eof, I should be able to iterate over the remaining lines of file1
    if (file2.eof()) {
        do {
            fout << numbers1 << endl;
            break;
        } while(!file1.eof());

        break;
    }

    if (grabNext == 'b') {
        file1 >> numbers1;
        file2 >> numbers2;
    } else if (grabNext == 'l') {
        file1 >> numbers1;
    } else if (grabNext == 'u') {
        file2 >> numbers2;
    }

    cout << numbers1 << " " << numbers2 << endl;

    if (numbers1 < numbers2) {
        fout << numbers1 << endl;
        grabNext = 'l';
    } else if (numbers1 > numbers2) {
        fout << numbers2 << endl;
        grabNext = 'u';
    }

    iteration++;
} while(true);

}

Here is example text file 1:

-1
3
5
7
9

Here is example text file 2:

2
4
10
12
16

My question is, after I reach eof() from one file, that I could continue to read the other until that one reaches eof()?

I have been looking everywhere with no luck.

Thank you for your time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:55:12+00:00Added an answer on June 1, 2026 at 4:55 am

    Note that you also have infinite loops:

    do {
            fout << numbers1 << endl;
            break;
        } while(!file1.eof());
    

    Nothing is changing inside the loop to cause file1 to approach eof().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a windows program which shall have controls for 64bit numeric values. these
I am building two libraries in OCaml which contain the same variant type. The
I am building a program which helps in memory debugging of C programs. I
I'm currently building a program which adds to the current user's shell depending on
I am building a program in vb6 which asks me to get the rows
I'm currently building a program in C# which will call functions in provided python
We are building a Employee Time-Tracking program which has fields such as ------------------------ Employee
I'm building a shared library in C, which other programs use. Sometimes, these other
I have a web creation program which, when building a site, creates hundreds of
I've hit a road block while building this shell program which includes some javascript

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.