这几天在linux下写个小程序,可以把文件内容写到扇区上。不过在调用fwrite()函数的时候,返回值竟然为0,真搞不懂为什么。
代码如下所示:
int write_file_to_sec(FILE *fp,char *filename, int size ,/
unsigned long sec_write_to)
{
FILE *fp1;
char buff[size];
int count=-1;
fp1=fopen(filename,"r");
if(fp1==NULL) return -EOPENFILE;
count=fread(buff,1,size,fp1);
if(count<size) return -EREAD ;
set_offset(fp,((long long)sec_write_to)<<9);
count=fwrite(buff,sizeof(char),size,fp);
if(count < size ) return -EWRITEFILE;
fclose(fp1);
return 0;
}
其中fp是用fopen打开“/dev/hda"以后得到的指针,是有效的,set_offset()用来设定文件指针,以便把文件写入正确的扇区,读文件也正常,为什么调用fwrite()以后会返回0呢?
代码如下所示:
int write_file_to_sec(FILE *fp,char *filename, int size ,/
unsigned long sec_write_to)
{
FILE *fp1;
char buff[size];
int count=-1;
fp1=fopen(filename,"r");
if(fp1==NULL) return -EOPENFILE;
count=fread(buff,1,size,fp1);
if(count<size) return -EREAD ;
set_offset(fp,((long long)sec_write_to)<<9);
count=fwrite(buff,sizeof(char),size,fp);
if(count < size ) return -EWRITEFILE;
fclose(fp1);
return 0;
}
其中fp是用fopen打开“/dev/hda"以后得到的指针,是有效的,set_offset()用来设定文件指针,以便把文件写入正确的扇区,读文件也正常,为什么调用fwrite()以后会返回0呢?