C ++ std :: cout运行时错误“无法读取内存”

时间:2018-09-03 13:34:07

标签: c++ runtime-error cout

当我使用std::cout时,出现“在0xXXXXXXXX处的指令引用了在0xXXXXXXXX处的内存。无法读取内存”的运行时错误,但是printf可以正常工作。我正在使用Windows XP。

int view(string archive_name) {

    FILE *read_ptr;
    char *buffer;
    string file_name, entry;
    float file_size;

    read_ptr = fopen(archive_name.c_str(), "rb");
    if(read_ptr == NULL) {
        cout << "\n" << archive_name << " : Archive file not found\n";
    }
    else {
        cout << "\nFiles in archive : " << archive_name << ",\n\n"
             << "Filename\t\tSize\n-------------------------------------------\n";

        for(int c = 0; c < 5; c++){
            fscanf(read_ptr, "%s %s %f ", entry, file_name, &file_size);
            float file_size_mb = (file_size/1024)/1024;
            cout << file_name << "\t\t" << file_size_mb;    //Gives error
            printf("%s\t\t%.2fMB\n", file_name, file_size_mb); //Works fine

            buffer = (char*) malloc (sizeof(char)*file_size);
            size_t result = fread(buffer, 1, file_size, read_ptr);
            free(buffer);
        }
        fclose(read_ptr);
    }
}

0 个答案:

没有答案