回 帖 发 新 帖 刷新版面

主题:文件错误:各位大侠,此程序是关于文件不能存进数据的编程

为什么此程序不能存进文本文件中数据。在输入时,为什么不执行输入。
#include "iostream"
#include "fstream"//编写一条语句在该程序中包含头文件fstream
#include "iomanip"
#include "string"
using namespace std;

void main()
{
    ifstream inFile;//第二个问题
    ofstream outFile;//第二个问题
    int a1,b1,a2,b2;
    char studentId;

    inFile.open("F:/VC/zizhukaifa/zhzhr2/inData.txt",ios::in);
    if(!inFile)
        cout <<"没有被打开!";
    outFile.open("F:/VC/zizhukaifa/zhzhr2/outData.txt");
    outFile << fixed << showpoint;
    outFile << setprecision(2);

    cout << "Processing data" << endl;
    //编写一条语句,在程序招待一,文件OUTdATA.DATR、的内容如下所示。如果需要,可以声明额外的变量
        //语句应该足够通用,这样如果输入文件的内容修改并且程序再次运行时,它还可以输出适当的结果

    inFile >> a1;
    inFile >> b1;
    inFile >> studentId;
    inFile >> a2;
    inFile >> b2;
    outFile << "shuchu jieguo:" << endl;
    outFile << "Sum of "<< a1 << "and " << b1 << "=" << a1+b1;
    outFile << "The character that comes after A in the ASCII set is B."
        << endl<< "The product of " << a2 << "and " << b2 << "=" << a2 * b2;
    inFile.close();
    outFile.close();
}

回复列表 (共2个回复)

沙发

#include <iostream>
#include <fstream>//编写一条语句在该程序中包含头文件fstream
#include <iomanip>
#include <string>
using namespace std;

void main()
{
    ifstream inFile;//第二个问题
    ofstream outFile;//第二个问题
    int a1,b1,a2,b2;
    string studentId;

    inFile.open("in.txt",ios::in);
    if(inFile.fail())
        cout <<"没有被打开!";
    outFile.open("out.txt");
    outFile << fixed << showpoint;
    outFile << setprecision(2);

    cout << "Processing data" << endl;
    //编写一条语句,在程序招待一,文件OUTdATA.DATR、的内容如下所示。如果需要,可以声明额外的变量
        //语句应该足够通用,这样如果输入文件的内容修改并且程序再次运行时,它还可以输出适当的结果

    inFile >> a1;
    inFile >> b1;
    inFile >> studentId;
    inFile >> a2;
    inFile >> b2;
    
    outFile << "shuchu jieguo:" << endl;
    outFile << "Sum of "<< a1 << " and " << b1 << " equal " << a1+b1;
    outFile << "\nThe character that comes after A in the ASCII set is B."
        << endl<< "The product of " << a2 << "and " << b2 << " equal " << a2 * b2;
    inFile.close();
    outFile.close();
}
稍微改了下
in.txt:
41  17  1 34 100
ou.txt:
shuchu jieguo:
Sum of 41 and 17 equal 58
The character that comes after A in the ASCII set is B.
The product of 34and 100 equal 3400

in.txt 和out.txt都是当前目录下的文件

板凳

问题出在你定义studentId为char,如果学好为13,char类型的studentId只会读入1.
造成后面a2,b2提取的数据都不对.

我来回复

您尚未登录,请登录后再回复。点此登录或注册