C语言fread和fwrite问题
#include<stdio.h>#include<stdlib.h>#defineU8chartypedefstruct{U8fileName[9];}Name;//-...
#include <stdio.h>
#include <stdlib.h>
#define U8 char
typedef struct
{
U8 fileName[9];
}Name;
//----------------------012345678
#define FILENAME_START "10000.bmp"
#define OUTPUT_FILE "OutPut.bin"
Name nxtName (Name preName);
void seekLoc (FILE *fp);
U8 * readFile (FILE *fp);
void writeFile (U8 *buf,FILE *outFile);
int main ()
{
Name name_BMP;
FILE *input,*output;
strcpy(name_BMP.fileName,FILENAME_START);//FILENAME_START;
output = fopen (OUTPUT_FILE,"w+");
name_BMP = nxtName (name_BMP);
while ((input = fopen (name_BMP.fileName,"r+")) != NULL)
{
seekLoc (input);
printf ("%s\n",name_BMP.fileName);
writeFile (readFile (input),output);
name_BMP = nxtName (name_BMP);
}
fclose (output);
return 0;
}
Name nxtName (Name preName)
{
int count;
count = (preName.fileName[1] - 48) * 1000 + (preName.fileName[2] - 48) * 100 + \
(preName.fileName[3] - 48) * 10 + (preName.fileName[4] - 48);
count ++;
preName.fileName[1] = (count / 1000) + 48;
preName.fileName[2] = ((count % 1000) /100) + 48;
preName.fileName[3] = ((count % 100) / 10) + 48;
preName.fileName[4] = (count % 10) + 48;
return preName;
}
void seekLoc (FILE *fp)
{
fseek (fp,62L,0); //locate the pt
}
U8 * readFile (FILE *fp)
{
U8 buf[1024]; //(128*64)/8 = 1024
fread (buf,1024,1,fp); //read a file
printf ("*");
fclose (fp);
return buf;
}
void writeFile (U8 *buf,FILE *outFile)
{
fwrite (buf,1024,1,outFile); //write to output file
}
代码如上,想实现的功能是将N张BMP图片去掉文件头后按顺序合并到一个文件里,编译和运行都成功,但查看输出的文件时发现文件大小小了很多(与原BMP文件大小总和相比),且似乎丢了很多信息(比如我第一张图从0x3E到0x410都是FF,但输出文件中FF到0x140时便不是FF了。)50分求解答。
之前就是wb,rb,因为有这个问题所以才换的r+,两者输出结果是一样的。
二楼的,感谢指出不足,wb和rb也解决不了问题,至于1K是因为这个程序要处理的每个文件数据区都只有1K,为了图省事所以这么写的 展开
#include <stdlib.h>
#define U8 char
typedef struct
{
U8 fileName[9];
}Name;
//----------------------012345678
#define FILENAME_START "10000.bmp"
#define OUTPUT_FILE "OutPut.bin"
Name nxtName (Name preName);
void seekLoc (FILE *fp);
U8 * readFile (FILE *fp);
void writeFile (U8 *buf,FILE *outFile);
int main ()
{
Name name_BMP;
FILE *input,*output;
strcpy(name_BMP.fileName,FILENAME_START);//FILENAME_START;
output = fopen (OUTPUT_FILE,"w+");
name_BMP = nxtName (name_BMP);
while ((input = fopen (name_BMP.fileName,"r+")) != NULL)
{
seekLoc (input);
printf ("%s\n",name_BMP.fileName);
writeFile (readFile (input),output);
name_BMP = nxtName (name_BMP);
}
fclose (output);
return 0;
}
Name nxtName (Name preName)
{
int count;
count = (preName.fileName[1] - 48) * 1000 + (preName.fileName[2] - 48) * 100 + \
(preName.fileName[3] - 48) * 10 + (preName.fileName[4] - 48);
count ++;
preName.fileName[1] = (count / 1000) + 48;
preName.fileName[2] = ((count % 1000) /100) + 48;
preName.fileName[3] = ((count % 100) / 10) + 48;
preName.fileName[4] = (count % 10) + 48;
return preName;
}
void seekLoc (FILE *fp)
{
fseek (fp,62L,0); //locate the pt
}
U8 * readFile (FILE *fp)
{
U8 buf[1024]; //(128*64)/8 = 1024
fread (buf,1024,1,fp); //read a file
printf ("*");
fclose (fp);
return buf;
}
void writeFile (U8 *buf,FILE *outFile)
{
fwrite (buf,1024,1,outFile); //write to output file
}
代码如上,想实现的功能是将N张BMP图片去掉文件头后按顺序合并到一个文件里,编译和运行都成功,但查看输出的文件时发现文件大小小了很多(与原BMP文件大小总和相比),且似乎丢了很多信息(比如我第一张图从0x3E到0x410都是FF,但输出文件中FF到0x140时便不是FF了。)50分求解答。
之前就是wb,rb,因为有这个问题所以才换的r+,两者输出结果是一样的。
二楼的,感谢指出不足,wb和rb也解决不了问题,至于1K是因为这个程序要处理的每个文件数据区都只有1K,为了图省事所以这么写的 展开
2个回答
展开全部
我只能说你写得告薯一塌糊涂...
随手写的不保证对,只是说明几个问题。1. 你每个文件不管多少只读1K;2. w+ r+是不行的,袜纯者会把0x0a变成0x0d 0x0a; 3. 你竟然直接返回局部变量
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define U8 char
typedef struct
{
U8 fileName[9];
}Name;
/裤薯/----------------------012345678
#define FILENAME_START "10000.bmp"
#define OUTPUT_FILE "OutPut.bin"
Name nxtName (Name preName);
void seekLoc (FILE *fp);
U8 * readFile (FILE *fp, long *l);
void writeFile (U8 *buf, long l, FILE *outFile);
int main ()
{
Name name_BMP;
FILE *input,*output;
strcpy(name_BMP.fileName,FILENAME_START);//FILENAME_START;
output = fopen (OUTPUT_FILE,"wb");
name_BMP = nxtName (name_BMP);
while ((input = fopen (name_BMP.fileName,"rb")) != NULL)
{
U8 *buf;
long l;
seekLoc (input);
printf ("%s\n",name_BMP.fileName);
buf = readFile (input, &l);
writeFile (buf, l,output);
name_BMP = nxtName (name_BMP);
}
fclose (output);
return 0;
}
Name nxtName (Name preName)
{
int count;
count = (preName.fileName[1] - 48) * 1000 + (preName.fileName[2] - 48) * 100 + \
(preName.fileName[3] - 48) * 10 + (preName.fileName[4] - 48);
count ++;
preName.fileName[1] = (count / 1000) + 48;
preName.fileName[2] = ((count % 1000) /100) + 48;
preName.fileName[3] = ((count % 100) / 10) + 48;
preName.fileName[4] = (count % 10) + 48;
return preName;
}
void seekLoc (FILE *fp)
{
fseek (fp,62L,0); //locate the pt
}
U8 * readFile (FILE *fp, long *l)
{
U8 *buf; //(128*64)/8 = 1024
fseek(fp, 0, SEEK_END);
buf = (U8 *)malloc(*l = ftell(fp));
fseek(fp, 0, SEEK_SET);
fread (buf,*l,1,fp); //read a file
printf ("*");
fclose (fp);
return buf;
}
void writeFile (U8 *buf, long l, FILE *outFile)
{
fwrite (buf,l,1,outFile); //write to output file
free(buf);
}
随手写的不保证对,只是说明几个问题。1. 你每个文件不管多少只读1K;2. w+ r+是不行的,袜纯者会把0x0a变成0x0d 0x0a; 3. 你竟然直接返回局部变量
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define U8 char
typedef struct
{
U8 fileName[9];
}Name;
/裤薯/----------------------012345678
#define FILENAME_START "10000.bmp"
#define OUTPUT_FILE "OutPut.bin"
Name nxtName (Name preName);
void seekLoc (FILE *fp);
U8 * readFile (FILE *fp, long *l);
void writeFile (U8 *buf, long l, FILE *outFile);
int main ()
{
Name name_BMP;
FILE *input,*output;
strcpy(name_BMP.fileName,FILENAME_START);//FILENAME_START;
output = fopen (OUTPUT_FILE,"wb");
name_BMP = nxtName (name_BMP);
while ((input = fopen (name_BMP.fileName,"rb")) != NULL)
{
U8 *buf;
long l;
seekLoc (input);
printf ("%s\n",name_BMP.fileName);
buf = readFile (input, &l);
writeFile (buf, l,output);
name_BMP = nxtName (name_BMP);
}
fclose (output);
return 0;
}
Name nxtName (Name preName)
{
int count;
count = (preName.fileName[1] - 48) * 1000 + (preName.fileName[2] - 48) * 100 + \
(preName.fileName[3] - 48) * 10 + (preName.fileName[4] - 48);
count ++;
preName.fileName[1] = (count / 1000) + 48;
preName.fileName[2] = ((count % 1000) /100) + 48;
preName.fileName[3] = ((count % 100) / 10) + 48;
preName.fileName[4] = (count % 10) + 48;
return preName;
}
void seekLoc (FILE *fp)
{
fseek (fp,62L,0); //locate the pt
}
U8 * readFile (FILE *fp, long *l)
{
U8 *buf; //(128*64)/8 = 1024
fseek(fp, 0, SEEK_END);
buf = (U8 *)malloc(*l = ftell(fp));
fseek(fp, 0, SEEK_SET);
fread (buf,*l,1,fp); //read a file
printf ("*");
fclose (fp);
return buf;
}
void writeFile (U8 *buf, long l, FILE *outFile)
{
fwrite (buf,l,1,outFile); //write to output file
free(buf);
}
2010-05-15
展开全部
打耐卜液开文件时的w+和r+是否应该改为wb和rb ?
b表示二进制模式打开文件,而+表示以读写模式打开文件(而弊咐非只读或者只昌物写)
b表示二进制模式打开文件,而+表示以读写模式打开文件(而弊咐非只读或者只昌物写)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询