C ++读取文件时出错

时间:2013-11-26 00:07:04

标签: c++ scanf

txt文件

(姓名\ t \ tScore)
类似于:

雅各布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。

1 个答案:

答案 0 :(得分:1)

你不能尝试写一个字符串常量!

// Bad
char* meno= "                             ";

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