3

我试图了解如何从输入文件中读取信息并将该数据写入输出文件。我了解如何从文件中读取并显示其内容,但我不了解如何写入文件或显示其内容。我的程序运行良好,但是当我检查我的输出 txt 文件时,里面什么都没有!我可能做错了什么?输入文件包含 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
}



outputFile.close();
inputFile.close();


int pause;
cin >> pause;

return 0;
}
4

4 回答 4

5

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.

于 2011-09-30T18:28:01.720 回答
2

正如 David N. 所提到的,默认情况下,当文件无法打开时,fstreams 不会抛出,或者流在写入过程中的某个时刻进入错误状态。您可以通过将 ofstream::exceptions 标志设置为 'ofstream::failbit | 来使它们抛出错误。ofstream::badbit'。您可以在此处找到有关异常掩码的更多信息:

http://www.cplusplus.com/reference/iostream/ios/exceptions/

为失败设置异常掩码是一种很好的做法,因为 a) 它避免了竞争条件,b) 需要处理错误,c) 允许自动堆栈展开,这在大型程序中通常很方便。

其次,在这个循环中:

while (inputFile) 
{
...
}

您应该检查的条件是 inputFile.eof()。但是,我建议您这样做“C++ 方式”:

#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;

}

注意迭代器和 std::copy() 算法的使用,而不是直接从流中读取。如果你想将文件的内容直接打印到 std::cout,那么你可以这样做:

copy(istream_iterator<char>(file), istream_iterator<char>(), ostream_iterator<char>(cout));

请注意,默认情况下 istream_iterators 将跳过空格,因此您必须进行设置file >> noskipws以防止这种情况发生。

格雷德

于 2011-10-02T14:04:53.833 回答
1

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;
    }
}
于 2011-09-30T18:27:18.587 回答
1

您需要关闭输出文件,以便在应用程序退出之前将缓冲区刷新到文件中。

于 2011-09-30T18:20:18.637 回答