1

我正在尝试打开一个文件并将内容读入几个数组。我找不到我做错了什么。我正在使用绝对路径。输入文件如下所示:

Sam     100    23   210
Bob     1     2    12
Ted     300   300   100
Bill    223   300   221
James   234   123   212
Luke    123   222   111
Seth    1     2     3
Tim     222   300   180
Dan     222   111   211
Ben     100   100     2

这是我的代码:

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main()
{
string name;
ifstream inFile;
string bowlerNames[10];
int bowlerScores[10][3] = {0};


inFile.open("C:\\Users\Seth\Documents\bowlingData.txt");

if (inFile.is_open()) //Checking if the file can be opened
{

for (int i = 0; i < 10; i++)
{
    inFile  >> bowlerNames[i] 
            >> bowlerScores[i][0]
            >> bowlerScores[i][1]
            >> bowlerScores[i][2];
}

for (int i = 0; i < 10; i++)
{
    cout    << bowlerNames[i] 
            << bowlerScores[i][0]
            << bowlerScores[i][1]
            << bowlerScores[i][2];
}
}
else cout << "Unable to open file..." <<endl; //Gives that sentence if the file can't be opened

inFile.close();

cout << endl; //spacing
system("Pause");
return 0;
}
4

1 回答 1

2

所有的反斜杠都必须是双反斜杠!

inFile.open("C:\\Users\\Seth\\Documents\\bowlingData.txt");

通过执行以下操作自己确认这一点:

string fileName = "C:\\Users\\Seth\\Documents\\bowlingData.txt");
cout << fileName << endl;
于 2013-04-12T22:58:45.053 回答