I am trying to understand how to read information form an input file and write that data into an output file. I understand how to read from a file and dispaly its contents, but I DONT understand how to write to a file or display its contents. My program runs fine but when I check my output txt file, there is nothing in it! What could I be doing wrong? input file contains 3.1415 2.718 1.414.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
float fValue;
fstream inputFile;
ofstream outputFile;
inputFile.open("C:\\Users\\David\\Desktop\\inputFile.txt");
outputFile.open("C:\\Users\\David\\Desktop\\outputfile.txt");
cout << fixed << showpoint;
cout << setprecision(3);
cout << "Items in input-file:\t " << "Items in out-put File: " << endl;
inputFile >> fValue; // gets the fiest value from the input
while (inputFile) // single loop that reads(from inputfile) and writes(to outputfile) each number at a time.
{
cout << fValue << endl; // simply prints the numbers for checking.
outputFile << fValue << ", "; // writes to the output as it reads numbers from the input.
inputFile >> fValue; // checks next input value in the file
}
开发者_JAVA百科outputFile.close();
inputFile.close();
int pause;
cin >> pause;
return 0;
}
On windows, it's likely that you have the output file open with something like notepad and your C++ program is not opening the file for output. If it can't open the file, the ofstream::open function will silently fail. You need to check the status of outputFile after you attempt to open it. Something like
outputFile.open("C:\\Users\\David\\Desktop\\outputfile.txt");
if (!outputFile) {
cerr << "can't open output file" << endl;
}
If the status of outputFile is not ok, then you can do
outputfile << "foobar";
and
outputfile.flush();
till the cows come home and you still won't get any output.
As David N. mentioned, fstreams do not throw by default when the file fails to open, or the stream gets into a bad state at some point during writing. You can make them throw on error by setting the ofstream::exceptions flag to 'ofstream::failbit | ofstream::badbit'. You can find more information about the exception mask here:
http://www.cplusplus.com/reference/iostream/ios/exceptions/
It is good practice to set the exception mask for failure, because a) it avoids race conditions and b) requires errors to be dealt with and c) allows automatic stack unwinding which is often convenient in larger programs.
Secondly, in this loop:
while (inputFile)
{
...
}
The condition that you should be checking is inputFile.eof(). However, I would suggest that you do this "The C++ Way":
#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
float fValue;
ifstream inputFile;
ofstream outputFile;
inputFile.open("input");
outputFile.open("output");
// EDIT: this is optional, but recommended
inputFile.exceptions(ifstream::badbit | ifstream::failbit);
outputFile.exceptions(ofstream::badbit | ofstream::failbit);
copy(istream_iterator<double>(inputFile)
, istream_iterator<double>()
, ostream_iterator<double>(outputFile, ", "));
/*
// EDIT: you can also check the status manually, but it looks more like C code:
if(inputFile.bad() || outputFile.bad())
return 1;
*/
outputFile.close();
inputFile.close();
cin.ignore();
return 0;
}
Notice the use of iterators and the std::copy() algorithm rather than reading directly from the stream. If you want to print out the contents of a file directly to std::cout, then you can do this:
copy(istream_iterator<char>(file), istream_iterator<char>(), ostream_iterator<char>(cout));
Note that by default istream_iterators will skip whitespace, so you have to set file >> noskipws
in order to prevent this.
HTH!
Gred
You need to close the output file for the buffer to be flushed to the file prior to your application exiting.
The code looks like it should save something to the file. The logic is all wrong, but something ought to be there. Check the states of your streams.
cout << fixed << showpoint;
cout << setprecision(3);
cout << "Items in input-file:\t " << "outputFile: " << endl;
while (inputFile >> fValue)
{
cout << fValue << endl; // this confirms that the above code read from my text file.
outputFile << fValue << '\n'
if (!outputFile) {
cout << "Error saving to file.\n";
break;
}
}
精彩评论