need c++ direction, gcd read from file

alright, i have this project i'm working on where one needs to read several numbers from a file and find their gcd two numbers at a time.

for example

first two numbers on the file are 43 and 5 which when read need to output:
43 5
56 4 (etc.)
with a space between them (important)
when the program calculates the gcd of the two numbers, the output needs to be
43 5 1
56 4 4
etc.

i have the algorithm for gcd, but have no idea how to read from file, put spaces, read two numbers at a time and then have the gcd algorithm calculate the gcd for those given numbers and then put them in that desired output.


even further, i need to have these numbers and output files placed into its own separate file. which of course, i have no idea how to do.

if anyone can point or hint (not asking you to do work for me) on the correct direction i need to take to do this project, i will be so grateful.
like i said, i have no idea where to start.
i know how to open a file, close a file, but i don't really understand how i am supposed to read two numbers at a time...and calc gcd and output like that specifically.

thank you so much in advance if anyone reads this. i'm backed into a corner and on the ropes.
input/output with files is just like cin and cout.
This is how you'd read two numbers, and then write them, and the gcd into another file.
1
2
3
4
5
6
7
8
//open input_file
//open output_file
while(!input_file.eof()){
   int n1, n2;
   input_file >> n1 >> n2;
   output_file << n1 << ' ' << n2 << ' ' << gcd(n1, n2) << '\n';
}
//close files 
For better error checking, it's best to loop on the stream itself because it could go into a failure state (fail() or bad()):

1
2
3
4
while(input_file)
{
    // ...
}

thanks guys, i'll try these and let you know how it goes.
alright that worked, but i'm having trouble with having the program read every number like its supposed to.
what does it read then?
Topic archived. No new replies allowed.