繁体   English   中英

有限制地在片刻中读取

[英]fread in a while cicle with restriction - c programming

我在cicle时有这个,我只需要打印apagado变量='\\ 0'的注册表。

我的结构是:

typedef struct {

char apagado; 
char prop[MAXPLEN];
char mat[6];
double valor;
} veiculo_t;

...代码示例:

{
    FILE *f=fopen("veic.dat", "rb");
    veiculo_t *t = malloc(sizeof(veiculo_t));
    while(fread(t, sizeof(char), sizeof(*t), f))
    print_registry(t); //
   }          

使用此代码可以打印所有注册表,而独立计算apagado的值。

错误使用参数:

while(fread(t, sizeof(char), sizeof(*t), f))

应该

while(fread(t, sizeof(*t), 1, f))

第一个返回值0 ... sizeof(*t) ..第二个返回值0 ... 1 因此,为了防止部分填充的结构,请使用第二种形式。

while(fread(t, sizeof(*t), 1, f)) {
  if (t->apagado == '\0') {
    print_registry(t); //
  }
}

当然,确保ft不为NULL

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM