【问题标题】:Error when using input file使用输入文件时出错
【发布时间】:2015-06-05 01:41:50
【问题描述】:

所以这是我写的一个 void 函数,但是 << 显示为红色并且不会运行程序。这是我第一次遇到这个问题,我该怎么办?这是在 C++ 中完成的,我正在为我的编译器使用 Visual Studio。

    void readData() //reading the function
{
    ifstream inputfile;
    inputfile.open("student.txt"); //open file for the students grades and names
for (int count = 0; count<20; count++)
    {
        inputfile **>>** st[count].studentFName >> st[count].studentLName; //reading each field
        for (int i = 0; i<5; i++)
            inputfile >> st[count].test_score[i]; //reading each score
    }
    inputfile.close(); //close the file
    cout << " Reading of the file was completed!" <<endl;
}

**&gt;&gt;** 部分是我的错误发生的地方。它说它不是字符串的一部分?

【问题讨论】:

  • 请在您的问题中发布st[]的定义。
  • 对不起,st是学生,所以学生数
  • 这与您的问题无关,但最好不要将明确的openclose 调用与ifstream 一起使用。相反,请使用 RAII:ifstream inputfile("student.txt")
  • 这将如何使文件打开或关闭?
  • 流为您完成。 ifstream 的构造函数打开文件,析构函数在 inputfile 超出范围时将其关闭..

标签: c++ file input


【解决方案1】:

您可能缺少#include。我怀疑是&lt;istream&gt;

【讨论】:

  • 我添加了,但不,这不是解决方案,让我现在发布我的代码,看看是否有人能弄清楚.. 我迷路了
【解决方案2】:
#include <iostream>
#include <fstream> 
#include <string> 
#include <istream>
using namespace std;

struct StudentType
{
    string studentFName;
    string studentLName;
    int test_score[5];
    char score;  //naming my variables
};

struct StudentType stu[20]; //you cannot enter more than 20 students with grades

void data() //reading the void function
{
    ifstream inputfile;
    ifstream inputfile("student.txt"); //open file for the students grades and names
    for (int count = 0; count<20; count++)
    {
        inputfile >> stu[count].studentFName >> stu[count].studentLName; //reading each field
        for (int i = 0; i<5; i++)
            inputfile >> stu[count].test_score[i]; //reading each score
    }
    ifstream inputfile("student.txt"); //close the file
    cout << " Reading of the file was completed!" << endl;
}

void studentGrade()
{
    ofstream outputfile;
    outputfile.open("output.txt"); //Open file for writing
    int total[20];
    for (int i = 0; i<20; i++) //20 records
    {
        total[i] = 0;
        for (int j = 0; j<5; j++) //calculating scores
            total[i] += stu[i].test_score[j];
        cout << " Totals : " << total[i] << endl;
        if ((total[i] / 5) >= 90) //finding of grades
            stu[i].score = 'A';
        else if ((total[i] / 5) >= 80)
            stu[i].score = 'B';
        else if ((total[i] / 5) >= 70)
            stu[i].score = 'C';
        else
            stu[i].score = 'D';
    }
    for (int i = 0; i<20; i++) //This will be for entering in 20 different students and grades
    {
        outputfile << stu[i].studentFName << "  " << stu[i].studentLName << " "; //the data for the file
        for (int j = 0; j<5; j++) //writing of 05 scores
            outputfile << "  " << stu[i].test_score[j];
        outputfile << "  " << stu[i].score << endl; //this will be the grades for the students
    }
    outputfile.close(); //closing the file

    cout << " File content is stored in output.txt" << endl;
}
int main()
{
    cout << " Welcome to my file!" << endl;
    data();
    studentGrade();
    cout << "Thank you for using my program! " << endl;
    system("pause");
    return 0;
}

【讨论】:

  • 您可能应该编辑您的原始问题以包含此问题,而不是将其作为答案发布。
  • 你为什么要在data函数的3个不同场合重新定义inpufile?除了您遇到的原始错误之外,该代码肯定不会编译
  • 其他人告诉我这是问题之一,所以我不应该这样吗?抱歉,我仍在学习它是如何工作的,这不是答案,因为它不起作用。
猜你喜欢
  • 2017-11-06
  • 2019-06-16
  • 1970-01-01
  • 2021-12-12
  • 1970-01-01
  • 1970-01-01
  • 2020-10-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多