Reputation: 6294
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
?
Upvotes: 0
Views: 1708
Reputation: 490108
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());
Upvotes: 3
Reputation: 5218
My guess is that you're checking eof too early - it's only set when you try to read and the read fails because you're at the end of the file. Try adding this after fin >> arr
:
if (fin.eof()) break;
Actually you should be checking for errors after every IO operation - not to do so is sloppy coding and won't be robust.
Upvotes: 0