c - 循环遍历 fread 语句

标签 c loops struct eof fread

需要循环遍历 fread 语句并打印每一个。然而 feof 在末尾打印了额外的一行

代码可以工作(如获得正确的输出),只需不需要额外的行

一个 A A A A A A A A A A A A A A A 一个

一个

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

关于c - 循环遍历 fread 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57672446/

相关文章:

c++ - C++代码中的C

c - 需要帮助弄清楚为什么 strtok 导致段错误,它不能使用 const char* 参数吗?

php从多维数组动态创建导航菜单

python - 简单的向下条形图(Python 3)

ios - 查找选择了哪个结构体实例

C- 服务器/客户端,等待其他客户端然后退出

比较两个文件每 1kb 的内容,而不是一个字符一个字符地比较

r - 如何在 R 中循环/重复线性回归

c - 将 CSV 文件中的数据添加到结构中

c# - 如何使用 protoBuf 进行结构序列化