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

  • Home
  • SEARCH
  • 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 4322308
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:51:25+00:00 2026-05-21T08:51:25+00:00

I read numbers from a file, apply 3 functions and print out to another

  • 0

I read numbers from a file, apply 3 functions and print out to another file:

int main(int argc, char** argv) {
    std::ifstream fin;
    fin.open("input.txt");

    std::ofstream fout;
    fout.open("output.txt", std::ios::app);

    char arr[50];
    int a,b;
    int N;//number to factor

    while (!fin.eof()){
        //Print backward
        fin >> arr;
        PrintBackward( arr );
        fout << endl;

        //Greatest common divisor
        ((fin >> a) >> b);
        fout << gcd( a, b );
        fout << endl;

        //Find prime factor
        fin >> N;
        PrimeFactor(N);
        fout << endl;     
    }

    fin.close();
    fout.close();

    return 0;
}

After running, the result is duplicated:

olleh
3
2 3 7 
olleh
3
2 3 7

I read a similar article but it’s about reading into 1 variable so it seems not to be feasible.

If I set a break at the end of the while loop, it’s fine. Is there any way not to use break?

  • 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-05-21T08:51:26+00:00Added an answer on May 21, 2026 at 8:51 am

    while (!whatever.eof()) is essentially always wrong, and will never detect the end of the file correctly. In your case, it’s easiest to coalesce the reads together, and then do all the processing, something like this:

    while (fin >> arr >> a >> b >> N) {
        PrintBackwards(arr);
        fout << "\n";
    
        fout << gcd(a, b) << "\n";
    
        fout << PrimeFactor(N) << "\n";
    }
    

    The crucial part is to check the result of the read, instead of checking and reading separately from each other.

    A couple more bits of advice: I’d use an std::string instead of an array. I’d also separate reversing the string from printing it, so you can have something like:

    fout << reverse(arr)   << "\n" 
         << gcd(a, b)      << "\n" 
         << PrimeFactor(N) << "\n";
    

    Emphasizing the commonality between the operations tends to be a good thing.

    Edit: Just for fun, I’ll point out another way you could do things if you wanted. Since you’re basically reading and processing the four items as a group, you could make that grouping a bit more explicit:

    struct item { 
         std::string arr;
         int a, b, N;
    
         friend std::istream &operator>>(std::istream &is, item &i) { 
             return is >> arr >> a >> b >> N;
         }
    };
    
    struct process {
        std::string operator()(item const &i) { 
            std::ostringstream buffer;
    
            buffer << reverse(arr) << "\n" << gcd(a, b) << "\n" << PrimeFactor(N);
            return buffer.str();
        }
    }
    

    With this, you can let the standard library deal with all the details of the reading and writing, checking end of file, etc.:

    std::transform(std::istream_iterator<item>(fin), 
                   std::istream_iterator<item>(),
                   std::ostream_iterator<std::string>(std::cout, "\n"),
                   process());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am very new to Python, I need to read numbers from a file
Hey, I've been working on this problem to read numbers from a text file
I want to read in a grid of numbers (n*n) from a file and
I'd like to read numbers from file into two dimensional array. File contents: line
I was trying to make a simple bubblesort program. Read in numbers from a
I'm writing porting file-io set of functions from c into a c++ class. Magic
How should a program read and write float numbers from and into binary files
I'm having issues when I try to read a float number from a file.
I am reading set of numbers from file by fscanf(), for each number I
I'm trying to read-in a bunch of unsigned integers from a configuration file into

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.