用C语言读写二进制文件
我试着用C读写文件,但失败了。它部分工作,但原始文件和输出文件不一样。 我试着读写bmp文件用C语言读写二进制文件,c,file,C,File,我试着用C读写文件,但失败了。它部分工作,但原始文件和输出文件不一样。 我试着读写bmp文件 FILE* openFile = fopen(argv[1], "rb"); FILE* writeFile = fopen(strcat(argv[1], ".cpd"), "wb"); fseek(openFile, 0, SEEK_END); long size = ftell(op
FILE* openFile = fopen(argv[1], "rb");
FILE* writeFile = fopen(strcat(argv[1], ".cpd"), "wb");
fseek(openFile, 0, SEEK_END);
long size = ftell(openFile);
char* bin = (char*)malloc(sizeof(char) * (size + 1));
rewind(openFile);
fwrite(bin, size, 1, writeFile);
//closefile, free, ...
您应该将读取原始文件添加到代码中的某个位置:
FILE* openFile = fopen(argv[1], "rb");
FILE* writeFile = fopen(strcat(argv[1], ".cpd"), "wb");
fseek(openFile, 0, SEEK_END);
long size = ftell(openFile);
char* bin = (char*)malloc(sizeof(char) * (size + 1));
rewind(openFile);
fread(bin, size, 1, openFile); // <-- here, for example
fwrite(bin, size, 1, writeFile);
FILE*openFile=fopen(argv[1],“rb”);
文件*writeFile=fopen(strcat(argv[1],“.cpd”),“wb”);
fseek(openFile,0,SEEK\u END);
long size=ftell(openFile);
char*bin=(char*)malloc(sizeof(char)*(size+1));
倒带(openFile);
fread(bin,size,1,openFile);//您的代码中没有fread
。请注意,strcat(argv[1],“.cpd”)
正在等待崩溃发生。您认为您在哪里读取该文件?…输出文件存在,但与原始文件不同。请显示一个。您还需要检查fopen
是否失败二进制文件上的代码>在严格符合C代码的情况下是未定义的行为:更重要的是,对于长
为32位的任何平台(如64位Windows…)@LPsfopen
,fseek
,ftell
,倒带
,对于大于2 GB的文件,它也会失败,fread
和fwrite
也可能失败。那又怎样?那么…用所有这些检查来纠正你提出的解决方案。。。。