试图读入一个文件

时间:2013-10-14 18:39:01

标签: c++

尝试编写需要读入某些数据的程序。它不会打开文件并读取数据。它似乎与getData函数有关。

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

using namespace std;

const int NUM_STUDENTS = 20;
const int NUM_GRADES = 5;

struct StudentType {
    string name;
    int grades[NUM_GRADES];
    int average;
    char letterGrade;
};

void getData(StudentType students[]);
void printGrades(StudentType students[]);
void assignGrades(StudentType students[]);
string convert(string name);
float findHighest(StudentType students[]);
void printHighest(StudentType students[]);

int main()
{
    StudentType students[20];

    getData(students);
    printGrades(students);

    return 0;
}

void getData(StudentType students[]) {

    string name, filename;
    ifstream fin;

    cout << "Please Enter the file name: ";
    cin >> filename;
    fin.open(filename.c_str());

    while (!fin) {
        cout <<"Could not open " << filename << ", please re-enter the filename" << endl;
        cout << "Please Enter the file name: ";
        cin >> filename;
        fin.open(filename.c_str());
    }

    for (int index = 0; index < NUM_STUDENTS; ++index) {
        int total = 0;
        getline(fin, name);
        students[index].name = convert(name);

        for (int i = 0; i <NUM_GRADES; ++i) {
            fin >> students[index].grades[NUM_GRADES];
            fin.ignore(INT_MAX, '\n');
            total = total + students[index].grades[NUM_GRADES];
        }
        students[index].average = static_cast<float>(total)/NUM_GRADES;
    }
    fin.close();
}

string convert(string name) {
    string s;
    string:: size_type x;
    x = name.find(".");

    if (x != string::npos) {
        ++x;
    }
    else {
        x = name.find( " ");
    }

    s = name.substr(x+1, name.size()) + ", " + name.substr(0,x);

    return s;
}

void printGrades(StudentType students[]) {
    for (int index = 0; index < NUM_STUDENTS; ++index) {
        cout << students[index].name;
        cout << "     ";

        for (int i = 0; i <NUM_GRADES; ++i) {
            cout << students[NUM_STUDENTS].grades[NUM_GRADES];
            cout << "  ";
        }
        cout << students[NUM_STUDENTS].average;
    }
}

void assignGrades(StudentType students[]) {
    for (int index = 0; index < NUM_STUDENTS; ++index) {
        if (students[NUM_STUDENTS].average < 60)
            students[NUM_STUDENTS].letterGrade = 'F';
        else if (students[NUM_STUDENTS].average < 70)
            students[NUM_STUDENTS].letterGrade = 'D';
        else if (students[NUM_STUDENTS].average < 80)
            students[NUM_STUDENTS].letterGrade = 'C';
        else if (students[NUM_STUDENTS].average < 90)
            students[NUM_STUDENTS].letterGrade = 'B';
        else
            students[NUM_STUDENTS].letterGrade = 'A';
    }
}

float findHighest(StudentType students[]) {
    float highestAverage = 0;
    for (int index = 0; index < NUM_STUDENTS; ++index) {
        if (students[index].average > highestAverage)
            highestAverage = students[index].average;
    }
    return highestAverage;
}

void printHighest(StudentType students[]) {
    float highestAverage = findHighest(students);
    cout << "The name(s) of the student(s) with the Highest average are ";

    for (int index = 0; index < NUM_STUDENTS; ++index) {
        if (students[index].average == highestAverage)
            cout << students[index].name << "  ";
    }
}

1 个答案:

答案 0 :(得分:1)

这是一份清单:

  1. 文件是否存在?
  2. 文件是否在正确的位置?
  3. 尝试用'/'。
  4. 替换路径中的'\'
  5. 文件被锁定了吗?
  6. 您是否有权打开它?
  7. 您是否可以使用moreless
  8. 访问该文件
  9. 如果文件名有空格,那么你是否在它周围加上双引号 命名?
  10. 如果文件名有破折号,' - ',您是否将名称括起来 双引号?
  11. 尝试编写临时文件以查找默认目录。