我想讀一個文件按字節到一個數組,然後寫該陣列的數據在一個新文件反轉 (程序接管命令行參數文件名)更大。嘗試用txt文件 ,它的工作,但如果我嘗試在JPG文件上新文件比原來更大! 確定的文件大小保存在長尺寸;也是正確的jpg文件和寫循環 得到大小時間執行寫一個字符(字符是一個字節大,我是對的?)。 有誰知道輸出文件的大小如何大於*字節?fwrite的創建一個輸出文件,它是比輸入文件
這對我來說似乎不合邏輯!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char* argv[])
{
FILE *file;
char *buffer;
long size;
char filename[32];
if(argc>1)
{
//determine file size
file=fopen(argv[1],"r");
fseek(file,0,SEEK_END);
size=ftell(file);
rewind(file);
if(size>33554432) //32MB
{
fclose(file);
return 0;
}
//create buffer and read file content
buffer=malloc(33554432);
fread(buffer,1,size,file);
fclose(file);
//create new file name and write new file
strcpy(filename,argv[1]);
strcat(filename,"_");
file=fopen(filename,"w");
{
long i;
for(i=size-1;i>=0;i--)
{
fputc(buffer[i],file);
}
}
fclose(file);
free(buffer);
}
return 0;
}
無法重現(Linux,64位,使用gcc)。你使用的是什麼操作系統和編譯器?的[什麼是在的fopen r和RB之間的差異] – McLovin
可能重複(http://stackoverflow.com/questions/2174889/whats-the-differences-between-r-and-rb-in-fopen) –