2013-12-18 79 views
0

这里是我的代码:ifstream的是无法打开文件

#include<iostream> 
#include<fstream> 
#include<string> 
using namespace std; 
void getHighScores(int scores[], string names[]); 

int main() 
{ 
    ifstream stream; 
    stream.open("scores.txt"); 
    int scores[32]; 
    string names[32]; 
    stream>>scores[0]; 
    stream>>names[0]; 
    if(stream.fail()) 
     cout<<"It failed\n"<<strerror(errno)<<endl; 
    for(int i=1;i<5;i++) 
    { 
     stream>>scores[i]; 
     stream>>names[i]; 
     cout<<i<<endl; 
    } 
    cout<<scores[2]<<endl; 
    stream.close(); 

    return 0; 
} 

void getHighScores(int scores[], string names[]) 
{ 

} 

它得到的分数垃圾输出[2],因为stream.open(“scores.txt”)无法打开文件。 012rstrerror(errno)给我“没有错误”。

我检查过我的文件是否真的叫做“scores.txt.txt”。不是这样。我也尝试将我的文件移动到“C:\ scores.txt”。我试过使用完整的地址。我试过删除它并重新创建它。我尝试了其他我不记得的东西。 ![输入图像描述] [1]我一直在努力几个小时来解决这个问题,我很绝望。如果有人能帮我解决这个问题,我将不胜感激。

void gethighscores是我打算稍后使用的函数。

输入文件看起来是这样的:

Ronaldo 
10400 
Didier 
9800 
Pele 
12300 
Kaka 
8400 
Cristiano 
8000 

程序的输出看起来像这样

It failed 
No error 
1 
2 
3 
4 
-858993460 
Press any key to continue . . . 

我在Microsoft Visual Studio Express的2012运行此为Windows桌面 我的工作系统是Windows 7的终极64位。

+0

您确定您的[工作目录](http://en.wikipedia.org/wiki/Working_directory)中确实存在scores.txt?这不一定是可执行文件的路径。此外,试图直接从C:\打开文件可能会因其他原因(例如权限)失败。另外,在调用open(不仅在尝试读取之后)之后,您应该立即检查打开失败('stream.is_open()')。 – MooseBoys

+0

它可能会帮助你很大程度上实际检查你的流提取的有效性,而不是盲目地假设他们的工作。此外,您输入文件的* real *样本以及您收到的* real *输出将与您正在运行的平台一起大大改善此问题。 – WhozCraig

+0

@WhozCraig他尝试使用'“C:\ scores.txt”这个事实是对平台的一个很好的暗示。 –

回答

2

试试这个:

#include<iostream> 
#include<fstream> 
#include<string> 
using namespace std; 
void getHighScores(int scores[], string names[]); 

int main() 
{ 
    string filename = "scores.txt"; // could come from command line. 
    ifstream fin(filename.c_str()); 
    if (!fin.is_open()) 
    { 
     cout << "Could not open file: " << filename << endl; 
     return 1; 
    } 

    int scores[32]; 
    string names[32]; 

    int iter = 0; 
    while (fin >> names[iter] >> scores[iter]) 
    { 
     if (++iter >= 32) 
     { 
      break; 
     } 
     cout << iter << endl; 
    } 

    if (iter >= 2) 
    { 
     cout << scores[2] << endl; 
    } 

    fin.close(); 

    return 0; 
} 

void getHighScores(int scores[], string names[]) 
{ 

} 
+0

刚刚尝试过。我刚刚添加了声明分数变量''int score [5];'的通道;'因为您忘记了它。我的代码如下: '无法打开文件:score.txt 按任意键继续。 。 。' – user3116382

+0

尝试将“score.txt”放在与正在运行的可执行文件相同的目录中(在项目的基础上)。 –

+1

我确认上面的代码加载了文件(包含上面发布的内容),并且完全按照预期进行。 scores.txt文件与可执行文件放在同一位置。 –

1

使用 “\” 来定义路径中使用,而不是一个 C上两个时:\ \ scores.txt

-1

失败的原因是这样的:

int scores[32]; 
string names[32]; 
stream>>scores[0]; 
stream>>names[0]; 
if(stream.fail()) 
    cout<<"It failed\n"<<strerror(errno)<<endl; 

默认情况下,score [0]和names [0]没有任何设置值,它会尝试将它们分配给文件,导致文件失败。如果您尝试评论这两行:

stream>>scores[0]; 
stream>>names[0]; 

您会看到它不再失败并正常工作。

+1

Downvoted。 'score [0]'和'names [0]'在从流中写入之前不需要初始值。验证删除这两条线不能解决问题。 – MooseBoys

1

难道我难住了一下。您的C++代码是按照与输入文本相反的顺序读取分数和名称。输入文件中的第一行文字是Ronaldo,但您的第一个operator>>score[0](一个int)。这会导致failbit被设置,因此fail()返回true。它还解释了为什么最终会为目标数组元素获取垃圾。

颠倒scores.txt文件或C++解析代码(但不是两个!)中的得分/名称的顺序,你应该很好。

+0

谢谢!这解决了我的问题。 – user3116382

+1

不是'badbit',而是'failbit'。 –