简体   繁体   English

C ++读取文件时出错

[英]C++ Error reading file

txt file txt文件

(Name\\t\\tScore) (名称\\ t \\ t分数)
something like: 就像是:

Jacob 25 雅各布25
Thomas 48 托马斯48
etc. 等等

#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;
}

But, when I run it: 但是,当我运行它时:
Unhandled exception at 0x61fade8f (msvcr100d.dll) in SLDTemplate2.exe: 0xC0000005: Access violation writing location 0x0097ca4c. SLDTemplate2.exe中0x61fade8f(msvcr100d.dll)的未处理异常:0xC0000005:访问冲突写入位置0x0097ca4c。

You must not try writing to a string constant! 您不得尝试写入字符串常量!

// Bad
char* meno= "                             ";

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM