遍历 fread 语句
Looping through fread statements
需要遍历 fread 语句并打印每一个。但是 feof 在末尾打印了额外的一行
代码有效(因为得到了正确的输出)只需要没有额外的行
一个
一种
一种
一种
一种
一种
一种
一种
一种
一种
一种
一种
一种
一种
一种
一种
一个
一个
struct item input;
FILE *fptr;
fptr = fopen(argv[1], "rb");
if(!fptr){
FILE *fpOut = fopen(argv[1], "w");
int c;
}
if(fptr == NULL){
fprintf(stderr, "\nError opening file\n");
exit(1);
fseek(fptr, 0, SEEK_SET);
}
while(!feof(fptr)){
fread(&input.business, sizeof(float), 1, fptr);
fread(&input.jellyfish, sizeof(char), 1, fptr);
fread(&input.death, sizeof(input.death), 1, fptr);
fread(&input.love, sizeof(input.love),1,fptr);
fread(&input.ornament, sizeof(input.ornament), 1, fptr);
fread(&input.taste, sizeof(input.taste),1,fptr);
fread(&input.cloth, sizeof(input.cloth),1,fptr);
fread(&input.name, sizeof(input.name),1,fptr);
fread(&input.camera, sizeof(input.camera),1,fptr);
fread(&input.attraction, sizeof(input.attraction),1,fptr);
fread(&input.bottle, sizeof(input.bottle),1,fptr);
fread(&input.stage, sizeof(input.stage),1,fptr);
fread(&input.square, sizeof(input.square),1,fptr);
fread(&input.bushes, sizeof(input.bushes),1,fptr);
fread(&input.heat, sizeof(input.heat),1,fptr);
fread(&input.fly, sizeof(input.fly),1,fptr);
printf("%f ", input.business);
printf("%c ", input.jellyfish);
printf("%d ", input.death);
printf("%c, ", input.love);
printf("%d, ", input.ornament);
printf("%f, ", input.taste);
printf("%ld, ", input.cloth);
printf("%d, ", input.name);
printf("%d, ", input.camera);
printf("%d, ", input.attraction);
printf("%d, ", input.bottle);
printf("%u, ", input.stage);
printf("%f, ", input.square);
printf("%d, ", input.bushes);
printf("%s, ", input.heat);`
printf("%d \n", input.fly);
eof()
的检查发生在 fread
之后。
while(!feof(fptr)){
fread(&input.business, sizeof(float), 1, fptr);
//...
printf("%f ", input.business);
//...
}
因此fread
的调用可能会失败,并且相应参数的先前值将由printf
的调用输出。
来自 C 标准(7.21.8.1 fread 函数)
3 The fread function returns the number of elements successfully read,
which may be less than nmemb if a read error or end-of-file is
encountered. If size or nmemb is zero, fread returns zero and the
contents of the array and the state of the stream remain unchanged
我可以为您的任务建议以下方法。
enum { N = 16 }; // number of fread calls
size_t n = 0, prev_n = 0;
do
{
switch ( n )
{
case 0:
n += fread( &input.business, sizeof(float), 1, fptr );
break;
case 1:
n += fread(&input.jellyfish, sizeof(char), 1, fptr );
break;
case 2:
n += fread(&input.death, sizeof(input.death), 1, fptr );
break;
// ...
}
} while ( prev_n != n && ( prev_n = n ) < N );
for ( size_t i = 0; i < n; i++ )
{
switch ( i )
{
case 0:
printf("%f ", input.business);
break;
case 1:
printf("%c ", input.jellyfish);
break;
case 2:
printf("%d ", input.death);
break;
//...
}
}
这是一个展示实际方法的演示程序。
#include <stdio.h>
int main(void)
{
enum { N = 3 };
int a[N];
size_t n = 0, prev_n = 0;
do
{
switch ( n )
{
case 0:
n += scanf( "%d", a + n );
break;
case 1:
n += scanf( "%d", a + n );
break;
case 2:
n += scanf( "%d", a + n );
break;
}
// ...
} while ( prev_n != n && ( prev_n = n ) < N );
for ( size_t i = 0; i < n; i++ )
{
switch ( i )
{
case 0:
printf( "%d ", a[i] );
break;
case 1:
printf( "%d ", a[i] );
break;
case 2:
printf( "%d ", a[i] );
break;
//...
}
}
return 0;
}
如果输入是
1 2 3
那么输出也是
1 2 3
需要遍历 fread 语句并打印每一个。但是 feof 在末尾打印了额外的一行
代码有效(因为得到了正确的输出)只需要没有额外的行
一个 一种 一种 一种 一种 一种 一种 一种 一种 一种 一种 一种 一种 一种 一种 一种 一个
一个
struct item input;
FILE *fptr;
fptr = fopen(argv[1], "rb");
if(!fptr){
FILE *fpOut = fopen(argv[1], "w");
int c;
}
if(fptr == NULL){
fprintf(stderr, "\nError opening file\n");
exit(1);
fseek(fptr, 0, SEEK_SET);
}
while(!feof(fptr)){
fread(&input.business, sizeof(float), 1, fptr);
fread(&input.jellyfish, sizeof(char), 1, fptr);
fread(&input.death, sizeof(input.death), 1, fptr);
fread(&input.love, sizeof(input.love),1,fptr);
fread(&input.ornament, sizeof(input.ornament), 1, fptr);
fread(&input.taste, sizeof(input.taste),1,fptr);
fread(&input.cloth, sizeof(input.cloth),1,fptr);
fread(&input.name, sizeof(input.name),1,fptr);
fread(&input.camera, sizeof(input.camera),1,fptr);
fread(&input.attraction, sizeof(input.attraction),1,fptr);
fread(&input.bottle, sizeof(input.bottle),1,fptr);
fread(&input.stage, sizeof(input.stage),1,fptr);
fread(&input.square, sizeof(input.square),1,fptr);
fread(&input.bushes, sizeof(input.bushes),1,fptr);
fread(&input.heat, sizeof(input.heat),1,fptr);
fread(&input.fly, sizeof(input.fly),1,fptr);
printf("%f ", input.business);
printf("%c ", input.jellyfish);
printf("%d ", input.death);
printf("%c, ", input.love);
printf("%d, ", input.ornament);
printf("%f, ", input.taste);
printf("%ld, ", input.cloth);
printf("%d, ", input.name);
printf("%d, ", input.camera);
printf("%d, ", input.attraction);
printf("%d, ", input.bottle);
printf("%u, ", input.stage);
printf("%f, ", input.square);
printf("%d, ", input.bushes);
printf("%s, ", input.heat);`
printf("%d \n", input.fly);
eof()
的检查发生在 fread
之后。
while(!feof(fptr)){
fread(&input.business, sizeof(float), 1, fptr);
//...
printf("%f ", input.business);
//...
}
因此fread
的调用可能会失败,并且相应参数的先前值将由printf
的调用输出。
来自 C 标准(7.21.8.1 fread 函数)
3 The fread function returns the number of elements successfully read, which may be less than nmemb if a read error or end-of-file is encountered. If size or nmemb is zero, fread returns zero and the contents of the array and the state of the stream remain unchanged
我可以为您的任务建议以下方法。
enum { N = 16 }; // number of fread calls
size_t n = 0, prev_n = 0;
do
{
switch ( n )
{
case 0:
n += fread( &input.business, sizeof(float), 1, fptr );
break;
case 1:
n += fread(&input.jellyfish, sizeof(char), 1, fptr );
break;
case 2:
n += fread(&input.death, sizeof(input.death), 1, fptr );
break;
// ...
}
} while ( prev_n != n && ( prev_n = n ) < N );
for ( size_t i = 0; i < n; i++ )
{
switch ( i )
{
case 0:
printf("%f ", input.business);
break;
case 1:
printf("%c ", input.jellyfish);
break;
case 2:
printf("%d ", input.death);
break;
//...
}
}
这是一个展示实际方法的演示程序。
#include <stdio.h>
int main(void)
{
enum { N = 3 };
int a[N];
size_t n = 0, prev_n = 0;
do
{
switch ( n )
{
case 0:
n += scanf( "%d", a + n );
break;
case 1:
n += scanf( "%d", a + n );
break;
case 2:
n += scanf( "%d", a + n );
break;
}
// ...
} while ( prev_n != n && ( prev_n = n ) < N );
for ( size_t i = 0; i < n; i++ )
{
switch ( i )
{
case 0:
printf( "%d ", a[i] );
break;
case 1:
printf( "%d ", a[i] );
break;
case 2:
printf( "%d ", a[i] );
break;
//...
}
}
return 0;
}
如果输入是
1 2 3
那么输出也是
1 2 3