未处理异常,内存不可读
程序代码:
#include<stdio.h> #include<stdlib.h> #include<string.h> #define SIZE 1024 #define N 20 #define M 30 void read(FILE * fp,int (*ptr)[M]); void writ(FILE * fp,const int (*ptr)[M]); int main(void) { FILE * fp1; FILE * fp2; char name[M],write[M]; int memory[N][M]; // 输入要打开的文件名 printf("input name of file and i will open it : "); gets(name); strcat(name,".txt"); printf("input name of file and i will write something for it : "); gets(write); strcat(write,".txt"); // 打开文件并建立缓冲 if(NULL == (fp1 = fopen(name,"rb"))) { fprintf(stderr,"Can't open file %s",name); system("pause"); exit(1); } if(setvbuf(fp1,NULL,_IONBF,SIZE) != 0) { fprintf(stderr,"Can't create input buffer."); system("pause"); exit(2); } if(NULL == (fp2 = fopen(write,"wb"))) { fprintf(stderr,"Can't open file %s",write); system("pause"); exit(1); } if(setvbuf(fp2,NULL,_IONBF,SIZE) != 0) { fprintf(stderr,"Can't create input buffer."); system("pause"); exit(2); } read(fp1,memory); // 读取文件输入到数组 if(ferror(fp1) != 0) // 查看是否正确读取 fprintf(stderr,"Error in reading file %s",name); fclose(fp1); // 关闭文件 writ(fp2,memory); //读取数组内容到文件 if(ferror(fp2) != 0) fprintf(stderr,"Error in reading file %s",write); fclose(fp2); // 关闭文件 system("pause"); return 0; } void read(FILE * fp,int (*ptr)[M]) { int i; for(i = 0;i < 20;i++) { fread(ptr[i],sizeof(int),30,fp); // 读取30块文件块,每块int大小 ptr[i][30] = '\0'; } } void writ(FILE * fp,const int (*ptr)[M]) { int i,j; static char table[] = " .':~*=&%@"; // 0~9转换为字符 for(i = 0;i < 20;i++) { for(j = 0;j < 30;j++) fputc(table[ptr[i][j]],fp); // 进行转换输出到文件 putchar('\n'); } }
调试的时候说内存不可读,然后我就用逐过程调试
在
fputc(table[ptr[i][j]],fp); // 进行转换输出到文件
这部的时候说 0x00411a63处有未处理异常:读取位置发生冲突