尝试使用循环读取n个图像时出错?

时间:2013-01-27 01:03:40

标签: c

void main() {
int frame_number = 0;
do {
    char *filename = "";
    strcpy(filename, "frame_");
    char *frame_id = "";
    itoa(frame_number, frame_id, 10);
    strcat(filename, frame_id);
    strcat(filename, ".bmp");

    FILE* f  = fopen(filename, "rb");
    if (!f) {
        printf("Could not read!");
        system("Pause");
    }
    else
        printf("Read!");
    fclose(f);
    frame_number++;
} while (frame_number < 20);
}

每当我运行此操作时,我都会收到access violation writing location的错误! 文件夹中有大约40个图像。 怎么解决这个问题?

1 个答案:

答案 0 :(得分:1)

没有为filenameframe_id分配内存。在使用malloccalloc存储字符串之前分配内存。或者只是将它们声明为静态数组。

char filename[256]="";
char frame_id[256] = "";