I am trying to test basic file input and output functions. I was trying to write a very simple code to output numbers entered by the user to a file, then read those numbers back in from the file.
I feel like I am missing something simple because it is letting me input numbers, and then after a negative number is entered, responding "unable to open file".
#include <iostream>
#include <fstream>
usingnamespace std;
int main ()
{
int number;
// Output data input by user to file
ofstream outputfile;
outputfile.open ("tempfile.txt");
do
{
cout << "Enter a positive number ";
cin >> number;
outputfile << number << '\n';
}
while (number >= 0);
outputfile.close();
//Read in data from file and print on screen
ifstream infile;
infile.open ("tempfile.txt");
if (infile.is_open() )
{
while ( ! infile.eof() )
{
cout << number << '\n';
}
infile.close();
}
else
cout << "Unable to open file";
return 0;
}
I don't know why it's printing "unable to open file", but the input loop isn't reading anything from the file. And you aren't testing for end-of-file properly. Using f.eof() is not really the way to do it. Instead, you should test the input functions themselves. Try this:
#include <iostream>
#include <fstream>
usingnamespace std;
int main () {
int number;
ofstream outputfile("tempfile.txt");
while (true) {
cout << "Enter a positive number ";
cin >> number;
if (number <= 0)
break;
outputfile << number << '\n';
}
outputfile.close();
ifstream infile("tempfile.txt");
if (infile.is_open()) {
while ( infile >> number )
cout << number << '\n';
infile.close();
}
else
cout << "Unable to open file";
return 0;
}
First problem, zero isn't positive. Should be > instead of >=
second problem, the number is output to the file even if its not a positive number, before the loop exits. When I cancel out of the infinite loop and examine the file, a -1 is the last number if thats the last thing I entered.
third problem, you never actually read from the file. So it just loops forever, printing that -1....
(I only fixed the third problem, which verified there was no file not found error. If you actually are getting a file not found error then you have an issue with your OS needing to have its write cache flushed. I ran into something like that coding visual basic scripts to run from an Access database 20 years ago so I know it can happen under some conditions... Not sure how you fix it though, because don't have any idea what your exact situation is.)
$ ./iotest
Enter a positive number 4
Enter a positive number 3
Enter a positive number 4
Enter a positive number -1
4
3
4
-1
-1
#include <iostream>
#include <fstream>
usingnamespace std;
int main ()
{
int number;
// Output data input by user to file
ofstream outputfile;
outputfile.open ("tempfile.txt");
do
{
cout << "Enter a positive number ";
cin >> number;
outputfile << number << '\n';
}
while (number >= 0);
outputfile.close();
//Read in data from file and print on screen
ifstream infile;
infile.open ("tempfile.txt");
if (infile.is_open() )
{
while ( ! infile.eof() )
{
infile >> number;
cout << number << '\n';
}
infile.close();
}
else
cout << "Unable to open file";
return 0;
}
Thank you for those fixes, but my problem still stands because I am not able to display anything when the program gets to the ifstream portion. it skips to the else statement and won't display anything. I figured it was because the file isn't opening? I'm still lost...
Oh wait, you aren't specifying a path for your program.
If its running from somewhere where it doesn't have permission to read and write to, then you will have problems... try running it from within a cmd.exe (Win) or terminal(Mac) window and see what happens.