这几天在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呢?
作者在Linux环境下编写了一个将文件内容写入指定扇区的小程序,但在使用fwrite()函数时遇到了返回值为0的情况。该问题发生在有效文件指针调用后,尽管文件读取操作正常。
2387





