Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用C语言读写二进制文件_C_File - Fatal编程技术网

用C语言读写二进制文件

用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

我试着用C读写文件,但失败了。它部分工作,但原始文件和输出文件不一样。 我试着读写bmp文件

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…)@LPs
fopen
fseek
ftell
倒带
,对于大于2 GB的文件,它也会失败,
fread
fwrite
也可能失败。那又怎样?那么…用所有这些检查来纠正你提出的解决方案。。。。