读取文件C ++

时间:2014-11-24 20:28:48

标签: c++ file

所以我必须在C ++中读取一个文件并且我认为我做的一切都正确但是当我去调试它时只出现一个黑屏,当我退出输出底部的黑屏时它说找不到或打开pdb文件。任何人都能找到我做错的事吗?我将这两个文件打开,直到资源文件。这是我的代码:

include iostream,
include string,
include fstream,
using namespace std;
struct dataStruct {
    string playerId,
    lastName,
    firstName,
    division,
    team;
    double plateAppearances,
    atBats,
    singles,
    doubles,
    triples,
    homeRuns,
    sackFlys,
    walks,
    hitByPitch,
    battingAvg,
    onBasePercentage,
    slugginPercentage,
    hits,
    sacrifices,
    sacrificeFlys;
};
int main() {
    string playerId = "",
    lastName = "",
    firstName = "",
    division = "",
    team = "";
    double plateAppearances = 0,
    atBats = 0,
    singles = 0,
    doubles = 0,
    triples = 0,
    homeRuns = 0,
    sackFlys = 0,
    walks = 0,
    hitByPitch = 0,
    battingAvg = 0,
    onBasePercentage = 0,
    slugginPercentage = 0; {
        dataStruct playerStats[500];
        dataStruct newPlayerStats[500];
        int counter = 0;
        int otherCounter = 0;
        ifstream inFile;
        ifstream masterData("Master_Data.csv", ios:: in );
        ifstream newStats("2014_League_Stats.csv");
        if (masterData.is_open()) {
            while (!masterData.eof()) {
                getline(masterData, playerStats[counter].playerId, ',');
                getline(masterData, playerStats[counter].firstName, ',');
                getline(masterData, playerStats[counter].lastName, ',');
                getline(masterData, playerStats[counter].division, ',');
                getline(masterData, playerStats[counter].team, ',');
                masterData >> playerStats[counter].plateAppearances;
                masterData.ignore(',');
                masterData >> playerStats[counter].atBats;
                masterData.ignore(',');
                masterData >> playerStats[counter].singles;
                masterData.ignore(',');
                masterData >> playerStats[counter].doubles;
                masterData.ignore(',');
                masterData >> playerStats[counter].triples;
                masterData.ignore(',');
                masterData >> playerStats[counter].homeRuns;
                masterData.ignore(',');
                masterData >> playerStats[counter].sackFlys;
                masterData.ignore(',');
                masterData >> playerStats[counter].walks;
                masterData.ignore(',');
                masterData >> playerStats[counter].hitByPitch;
                masterData.ignore(',');
                masterData >> playerStats[counter].battingAvg;
                masterData.ignore(',');
                masterData >> playerStats[counter].onBasePercentage;
                masterData.ignore(',');
                masterData >> playerStats[counter].slugginPercentage;
                masterData.ignore(',');
            }
        } else {
            cout << "Error Reading File" << endl;
        }
        if (newStats.is_open()) {
            while (!newStats.eof()) {
                getline(newStats, newPlayerStats[otherCounter].playerId, ',');
                getline(newStats, newPlayerStats[otherCounter].firstName, ',');
                getline(newStats, newPlayerStats[otherCounter].lastName, ',');
                getline(newStats, newPlayerStats[otherCounter].division, ',');
                getline(newStats, newPlayerStats[otherCounter].team, ',');
                newStats >> newPlayerStats[otherCounter].plateAppearances;
                newStats.ignore(',');
                newStats >> newPlayerStats[otherCounter].atBats;
                newStats.ignore(',');
                newStats >> newPlayerStats[otherCounter].singles;
                newStats.ignore(',');
                newStats >> newPlayerStats[otherCounter].doubles;
                newStats.ignore(',');
                newStats >> newPlayerStats[otherCounter].triples;
                newStats.ignore(',');
                newStats >> newPlayerStats[otherCounter].homeRuns;
                newStats.ignore(',');
                newStats >> newPlayerStats[otherCounter].sackFlys;
                newStats.ignore(',');
                newStats >> newPlayerStats[otherCounter].walks;
                newStats.ignore(',');
                newStats >> newPlayerStats[otherCounter].hitByPitch;
                newStats.ignore(',');
                newStats >> newPlayerStats[otherCounter].battingAvg;
                newStats.ignore(',');
                newStats >> newPlayerStats[otherCounter].onBasePercentage;
                newStats.ignore(',');
                newStats >> newPlayerStats[otherCounter].slugginPercentage;
                newStats.ignore(',');
            }
        } else {
            cout << "Error Reading File" << endl;
        }
        if (inFile.is_open()) {
            while (!masterData.eof()) {
                playerStats[counter].atBats = (playerStats[counter].plateAppearances +
                    newPlayerStats[otherCounter].plateAppearances) -
                    ((playerStats[counter].walks + newPlayerStats[otherCounter].walks) +
                    (playerStats[counter].hitByPitch + newPlayerStats[otherCounter].hitByPitch) +
                    (playerStats[counter].sacrifices + newPlayerStats[otherCounter].sacrifices));
                playerStats[counter].hits = ((playerStats[counter].singles + newPlayerStats[otherCounter].singles) +
                    (playerStats[counter].doubles + newPlayerStats[counter].doubles) +
                    (playerStats[counter].triples + newPlayerStats[otherCounter].triples) +
                    (playerStats[counter].homeRuns + newPlayerStats[otherCounter].homeRuns));
                playerStats[counter].battingAvg = ((playerStats[counter].hits) / ((playerStats[counter].atBats)));
                playerStats[counter].onBasePercentage = ((playerStats[counter].hits) +
                    (playerStats[counter].hitByPitch) + newPlayerStats[otherCounter].hitByPitch) +
                    (playerStats[counter].walks + newPlayerStats[otherCounter].walks) /
                    ((playerStats[counter].atBats) + (playerStats[counter].walks + newPlayerStats[otherCounter].walks) +
                    (playerStats[counter].hitByPitch) + newPlayerStats[otherCounter].hitByPitch) +
                    (playerStats[counter].sacrificeFlys);
                playerStats[counter].slugginPercentage = (playerStats[counter].hits / playerStats[counter].atBats);
                masterData.close();
                newStats.close();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

好吧,让我们从基础开始,然后从那里开始。由于我没有.csv文件的副本,因此我会在您阅读信息时做出一些假设,但大部分修正都与我们在c ++中读取文件的方式有关。

我理解大多数这些更正只是编辑您的帖子,但我没有理所当然:

include iostream, // should be #include <iostream> - no comma.
include string,   // same here #include <string> - no comma.
include fstream,  // ditto.

您的括号也是您的主要关注点,以便甚至编译此代码,您可以在不使用或不应该使用括号的情况下自由使用括号。只需摆脱额外的括号,你就可以了。

int main() {
string playerId = "",
lastName = "",
firstName = "",
division = "",
team = "";
double plateAppearances = 0,
atBats = 0,
singles = 0,
doubles = 0,
triples = 0,
homeRuns = 0,
sackFlys = 0,
walks = 0,
hitByPitch = 0,
battingAvg = 0,
onBasePercentage = 0,
slugginPercentage = 0; { // Why is this here?
    dataStruct playerStats[500];
    dataStruct newPlayerStats[500];

让我们看看如何从文件中读取:

if (inFile.is_open()) {
        while (!masterData.eof()) {
            playerStats[counter].atBats = (playerStats[counter].plateAppearances +
                newPlayerStats[otherCounter].plateAppearances) -
                ((playerStats[counter].walks + newPlayerStats[otherCounter].walks) +
                (playerStats[counter].hitByPitch + newPlayerStats[otherCounter].hitByPitch) +
                (playerStats[counter].sacrifices + newPlayerStats[otherCounter].sacrifices));
            playerStats[counter].hits = ((playerStats[counter].singles + newPlayerStats[otherCounter].singles) +
                (playerStats[counter].doubles + newPlayerStats[counter].doubles) +
                (playerStats[counter].triples + newPlayerStats[otherCounter].triples) +
                (playerStats[counter].homeRuns + newPlayerStats[otherCounter].homeRuns));
            playerStats[counter].battingAvg = ((playerStats[counter].hits) / ((playerStats[counter].atBats)));
            playerStats[counter].onBasePercentage = ((playerStats[counter].hits) +
                (playerStats[counter].hitByPitch) + newPlayerStats[otherCounter].hitByPitch) +
                (playerStats[counter].walks + newPlayerStats[otherCounter].walks) /
                ((playerStats[counter].atBats) + (playerStats[counter].walks + newPlayerStats[otherCounter].walks) +
                (playerStats[counter].hitByPitch) + newPlayerStats[otherCounter].hitByPitch) +
                (playerStats[counter].sacrificeFlys);
            playerStats[counter].slugginPercentage = (playerStats[counter].hits / playerStats[counter].atBats);
            masterData.close(); // You close the file in your while loop
            newStats.close(); // You close the file in your while loop
        }
    }

在收集信息时关闭while循环中的两个文件。因此,假设masterdata包含您希望在文件结束之前读取的信息 - 在完成while循环中收集的第一个信息序列后关闭它。从您的while循环和if语句中取出代码masterData.close();newStats.close();,并在退出main之前将其放置。

这引出了我的另一个评论 - 主要应该在成功完成该程序后返回一些内容。 (即return 0;return EXIT_SUCCESS;)。