gpt4 book ai didi

c++ - ifstream 无法打开文件

转载 作者:行者123 更新时间:2023-11-30 01:55:33 25 4
gpt4 key购买 nike

这是我的代码:

#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[])
{

}

因为 stream.open("scores.txt") 无法打开文件,所以它得到 scores[2] 的垃圾输出。strerror(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 for Windows Desktop 中运行它我的操作系统是 Windows 7 旗舰版 64 位。

最佳答案

当使用“\”定义路径时,使用两个而不是一个C:\\分数.txt

关于c++ - ifstream 无法打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20665648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com