C ++,fstream,从文件读取未提供正确的结果

时间:2018-07-28 01:17:16

标签: c++ fstream ifstream ofstream

我正在尝试创建一个简单的程序,将输入的数据写入文件,然后从文件中读取该数据。由于某种原因,输出不正确。它从文件中读取的第一个值显示所有三个输入值,而不仅仅是第一个。有人可以告诉我我在做什么错吗?

#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>

using namespace std;


int main() {
    double totalRainfall=0.0, highTemperature,lowTemperature, totalHigh=0.0, 
     totalLow=0.0;

// open a file in write mode.
ofstream outFile;
outFile.open("statistics.txt");


    cout << "Enter total rainfall (in inches) : ";
    cin >> totalRainfall;
    outFile <<totalRainfall;  // write inputted data into the file.



    cout << "Enter high temperature (in Fahrenheit): ";
    cin >> highTemperature;
    outFile << highTemperature;  // write inputted data into the file.


    cout << "Enter low temperature (in Fahrenheit) : ";
    cin >> lowTemperature;
    outFile << lowTemperature;  // write inputted data into the file.

    //close the opened file
    outFile.close();

    //open a file in read mode.
    ifstream inFile;
    inFile.open("statistics.txt");

    inFile >> totalRainfall; 
    cout << "The rainfall: ";
    cout<< totalRainfall<<endl; 

    inFile >> highTemperature;
    cout << "High temperature: ";
    cout << highTemperature<<endl;

    inFile >> lowTemperature;
    cout << "Low temperature: ";
    cout<<lowTemperature<<endl;

    // close the opened file.
    inFile.close();

    system("pause");
    return 0;
}

2 个答案:

答案 0 :(得分:0)

您应该在每个输出数字之间放置一个分隔符,以便在读取文件时知道数字的开始位置和结束位置。空格字符作为分隔符允许读取数字而无需特定代码来跳过分隔符(读取数字的STL函数会跳过前导空格字符)。

下面以空格作为分隔符的示例:

#include <fstream>
#include <iostream>

using namespace std;

const char sep = ' ';
const char* const file_path = "statistics.txt";

int main()
{
    double value = 0.0;

    ofstream outFile;
    outFile.open(file_path);

    cout << "Enter total rainfall (in inches): ";
    cin >> value;
    outFile << value << sep;

    cout << "Enter high temperature (in Fahrenheit): ";
    cin >> value;
    outFile << value << sep;

    cout << "Enter low temperature (in Fahrenheit): ";
    cin >> value;
    outFile << value;

    outFile.close();

    ifstream inFile;
    inFile.open(file_path);

    inFile >> value;
    cout << "The rainfall: ";
    cout << value << endl;

    inFile >> value;
    cout << "High temperature: ";
    cout << value << endl;

    inFile >> value;
    cout << "Low temperature: ";
    cout << value << endl;

    return 0;
}

答案 1 :(得分:0)

您应该使用fstream对象的write()read()方法。这些方法允许您写入/读取特定数量的字节。因此,您可以创建两个实用程序功能,如下所示:

void writeValue(ofstream& f, const double& v) {
    f.write(reinterpret_cast<const char*>(&v), sizeof(v));
}

void readValue(ifstream& f, double& v) {
    f.read(reinterpret_cast<char*>(&v), sizeof(v));
}

现在您可以在主要代码中使用它们了:

double value = 0.0;

ofstream outFile;
outFile.open(file_path);

cout << "Enter total rainfall (in inches): ";
cin >> value;
writeValue(outFile, value);

cout << "Enter high temperature (in Fahrenheit): ";
cin >> value;
writeValue(outFile, value);

cout << "Enter low temperature (in Fahrenheit): ";
cin >> value;
writeValue(outFile, value);

outFile.close();

ifstream inFile;
inFile.open(file_path);

readValue(inFile, value);
cout << "The rainfall: ";
cout << value << endl;

readValue(inFile, value);
cout << "High temperature: ";
cout << value << endl;

readValue(inFile, value);
cout << "Low temperature: ";
cout << value << endl;

改善读/写功能

我上面提到的实用程序功能只能处理double值。这不灵活,如果您想扩展代码并处理不同类型的信息,就应该成为一个问题。因此,如果您想使代码更健壮并在序列化文件中允许其他变量类型(例如int,float),则可以使用模板代替

template<typename T>
void writeValue(ofstream& f, const T& v) {
    f.write(reinterpret_cast<const char*>(&v), sizeof(v));
}

template<typename T>
constexpr void readValue(ifstream& f, T& v) {
    f.read(reinterpret_cast<char*>(&v), sizeof(v));
}

如果您还想处理std::string,则可以为该特定类型自定义模板:

template<>
constexpr void writeValue(ofstream& f, const std::string& v) {
    auto len = v.length();
    f.write(reinterpret_cast<const char*>(&len), sizeof(len));
    f.write(v.c_str(), v.length() * sizeof(char));
}

template<>
void readValue(ifstream& f, std::string& v) {
    std::string::size_type len;
    f.read(reinterpret_cast<char *>(&len), sizeof(len));

    char buffer[len + 1];
    f.read(buffer, len);
    buffer[len] = '\0';
    v = buffer;
}