gpt4 book ai didi

C++错误读取文件

转载 作者:行者123 更新时间:2023-11-28 07:16:15 25 4
gpt4 key购买 nike

txt 文件

(名称\t\t分数)
类似于:

雅各布 25
托马斯 48

#include <stdio.h>
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char* argv[]){
FILE *f = fopen("skore.txt","r");
if (f==NULL){
cout << "err " <<endl;
return 1;
}

char* meno= " ";
int skore;
while(fscanf(f,"%s\t\t%d",meno, &skore ) != EOF){
cout << meno << ", " << skore << endl;
}
cin.get();
fclose(f);
return 0;
}

但是,当我运行它时:
SLDTemplate2.exe 中 0x61fade8f (msvcr100d.dll) 处的未处理异常:0xC0000005:访问冲突写入位置 0x0097ca4c。

最佳答案

您不能尝试写入字符串常量!

// Bad
char* meno= " ";

// Good
char meno[31] = " "; // 30 chars + 1

关于C++错误读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20206080/

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