大家好,这基本上是对我上一个问题的延伸。我已经使用
在文件中写了短的int值short int x= 254;
FILE * f1 = fopen("infile" ,"w");
fwrite (&x , 1 , sizeof(short int ) , f1 );
它工作正常,但当我试图检索这样的价值时
short int y ;
fread(&y , 2, 1 ,f1);
printf("%d" , y);
它给了我回答8180和下次12276等等......我应该怎么做
实际上我想在我的文件中存储短整数,然后逐个检索它们,我做错了,请指导我
答案 0 :(得分:1)
最喜欢你不是在写作和阅读之间关闭并重新打开文件。我建议创建一个小功能,执行两个操作,首先写入,然后关闭和刷新,然后阅读。看看你得到了什么样的结果。另外,请确保使用二进制模式。
答案 1 :(得分:0)
如果您想从刚刚写入的文件中读回来,则需要使用“w +”选项打开它以允许您进行读写。然后,您需要回到文件的开头:
// Open the file
const char* fname = "infile";
FILE * f1 = fopen("infile" ,"wb+");
if( f1 != NULL )
{
printf("opened file '%s' for writing / reading\n", fname);
// Write two bytes
short int x = 254;
size_t bytes_written = fwrite(&x, 1, sizeof(short int), f1 );
printf("%hd bytes written\n" , bytes_written);
// Seek to the start of the file
int seek_result = fseek(f1, 0, 0);
if( seek_result == 0 )
{
printf("found beginning of file\n");
}
else
{
printf("failed to seek to beginning of file\n");
}
// Read the two bytes back in
short int y = -9999;
size_t bytes_read = fread(&y, 1, sizeof(short int), f1);
printf("%hd bytes read: %hd\n", bytes_read, y);
// We're done with the file so close it
fclose(f1);
}
else
{
// Something went wrong and we failed to open the file
printf("failed to open file '%s' for writing / reading\n", fname);
}
或者,您可以作为单独的操作进行读/写:
// Open the file
const char* fname = "infile";
FILE * f1 = fopen("infile" ,"wb");
if( f1 != NULL )
{
printf("opened file '%s' for writing\n", fname);
// Write two bytes
short int x = 254;
size_t bytes_written = fwrite(&x, 1, sizeof(short int), f1 );
printf("%hd bytes written\n" , bytes_written);
// We're done with the file so close it
fclose(f1);
}
else
{
// Something went wrong and we failed to open the file
printf("failed to open file '%s' for writing\n", fname);
}
// re-open the file
FILE * f2 = fopen("infile" ,"rb");
if( f2 != NULL )
{
printf("opened file '%s' for reading\n", fname);
// Read the two bytes back in
short int y = -9999;
size_t bytes_read = fread(&y, 1, sizeof(short int), f2);
printf("%hd bytes read: %hd\n", bytes_read, y);
// We're done with the file so close it
fclose(f2);
}
else
{
// Something went wrong and we failed to open the file
printf("failed to open file '%s' for reading\n", fname);
}
请注意,在原始代码中,您没有检查fread的返回值,以检查您是否确实从文件中读取了任何内容。如果你已经这样做了,你已经看到它返回0表示它读取0字节。如果你没有从文件中读取任何内容而你没有初始化y
那么它可能只是一个随机数。
答案 2 :(得分:0)
我同意Jon Cage的回答(赞成他,不是我!)
另请注意(很久以前),Windows / DOS系统需要使用'wb','rb'而不是'w'和'r'打开二进制文件。我不确定是否仍然如此,但尝试没有问题。
这里有一些代码可以让事情变得更清晰:
short int x= 254;
int nwritten;
FILE * f1 = fopen("infile" ,"w+b");
nwritten=fwrite (&x , 1 , sizeof(short int ) , f1 ); /* check number of shorts written */
if (nwritten != 1) fprintf(stderr,"Error: %d short written\n",nwritten);
阅读部分:
short int y ;
int nread;
nread=fread(&y , sizeof(short int), 1 ,f1); /* check number of shorts read */
if (nread == 1) printf("%d" , y);
else fprintf(stderr,"Error: could not read 1 short int (%d read)\n",nread);
另外,请记住每次读/写后文件内的位置都会增加。您可能希望在阅读之前返回到文件的开头。通过在Jon回答中关闭/重新打开文件或使用以下方式寻找开头来执行此操作:
fseek(f1,0L,SEEK_SET); /* you also can use rewind(f1); for the same result */
完整代码:
short int x= 254;
int nwritten;
FILE * f1 = fopen("infile" ,"w+b");
nwritten=fwrite (&x , 1 , sizeof(short int ) , f1 ); /* check number of shorts written */
if (nwritten != 1) fprintf(stderr,"Error: %d short written\n",nwritten);
short int y ;
int nread;
fseek(f1,0L,SEEK_SET); /* get back to the start */
nread=fread(&y , sizeof(short int), 1 ,f1); /* check number of shorts read */
if (nread == 1) printf("%d" , y);
else fprintf(stderr,"Error: could not read 1 short int (%d read)\n",nread);