找到错误了,但不知道为什么出错...好像第一次运行还是对的,总之求教啦...
程序代码:
#include <stdio.h> #include <stdlib.h> int main(void) { size_t str_count = 0; fpos_t *postition = NULL; char *buffer = NULL; size_t length = 0; int jilu = 0; char *temp = NULL; int i = 0; char *Input_filename = "E:\\才、\\Test\\test1.bin"; char *Out_filename = "E:\\才、\\Test\\tests.bin"; FILE *Input_pfile = NULL; FILE *Out_pfile = NULL; if(!(Input_pfile = fopen(Input_filename, "rb"))) { printf("读取文件%s faild!\n",Input_filename); return 1; } /* 得到字符串数目 */ while(1) { fread(&length, sizeof(size_t), 1, Input_pfile); if(feof(Input_pfile)) break; if( jilu < (int)length) { jilu = length; temp = buffer; free(temp); temp = NULL; buffer = (char*)malloc(length+1); } fread(buffer, sizeof(char), length, Input_pfile); str_count++; } printf("The count of strings are %d\n",str_count); postition = (fpos_t*)malloc(str_count-1); rewind(Input_pfile); fread(&length, sizeof(size_t), 1, Input_pfile); fread(buffer, sizeof(char), length, Input_pfile); for(i=0;i<(int)str_count-1;i++) { fgetpos(Input_pfile, &postition[i]); fread(&length, sizeof(size_t), 1, Input_pfile); fread(buffer, sizeof(char), length, Input_pfile); } printf("\nEE\n"); if(!(Out_pfile = fopen(Out_filename, "wb"))) /* 在这里出错了,运行到这里会出警告,可应该没错啊? */ { printf("error!\n"); return -1; } printf("\nEE\n"); for(i=0;i<(int)str_count-1;i++) { fsetpos(Input_pfile, &postition[str_count-2-i]); fread(&length, sizeof(size_t), 1, Input_pfile); fwrite(&length, sizeof(size_t), 1, Out_pfile); fread(buffer, 1, length, Input_pfile); fwrite(buffer, 1, length, Out_pfile); } rewind(Input_pfile); fread(&length, sizeof(size_t), 1, Input_pfile); fwrite(&length, sizeof(size_t), 1, Out_pfile); fread(buffer, 1, length, Input_pfile); fwrite(buffer, 1, length, Out_pfile); fclose(Out_pfile); fclose(Input_pfile); Out_pfile = fopen(Out_filename, "rb"); printf("The strings in the %s are:\n",Out_filename); for(i=0;i<(int)str_count;i++) { fread(&length, sizeof(size_t), 1, Out_pfile); fread(buffer, sizeof(char), length, Out_pfile); buffer[length] = '\0'; printf("%s\n",buffer); } fclose(Out_pfile); remove(Input_filename); remove(Out_filename); return 0; } /* 路径大家可以根据自己的电脑来定义,谢谢 */