c - 变量类型结构体是否必须是数组才能存储 100 个元素?

标签 c arrays structure randomaccessfile

我是新来的,很抱歉有任何误解。 我正在学习 C 语言的随机访问文件。我对变量blankClient感到困惑。它不是一个数组,但是Deitel(作者)如何使用空白Client初始化100条空白记录。 我认为它应该是这样的: struct clientdata BlankClient[100];

/*Creating a random-access file sequentially */
#include <stdio.h>

struct clientdata {
  int acctNum;          /*account number*/
  char lastname[15];    /*account last name*/
  char firstname[10];   /*account first name */
  double balance;       /*account balance*/
};

int main (void){

int i;  /*counter used to count from 1-100 */

/*create clientData with default info */
struct clientdata blankClient = {0, "","", 0.0};

FILE *cfPtr; /*credit.dat file pointer */

if ((cfPtr =fopen("credit.dat", "wb")) == NULL) {
    printf("File could not be opened. \n");
}
/*output 100 blank records to file */
else {
        for (i=1; i<=100; i++) {
            fwrite(&blankClient, sizeof( struct clientdata), 1, cfPtr);
        }
        fclose (cfPtr);
    }
return 0;
}

最佳答案

该代码将相同源记录写入 100 次。这类似于只用两只手就能发出一百个拍手声。

关于c - 变量类型结构体是否必须是数组才能存储 100 个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23877584/

相关文章:

c - TI-84 : call a function from the catalog with z88dk

c - 指针段错误

javascript - 减少 JavaScript 中的抛出 "NaN"?

c - 当您不知道接收到哪个结构时如何从结构接收数据

c++ - 结构中的数组

c - 如何在 C 中打印带有计数的字符串中的重复字母?

php - 试图在 php 数组中获取非对象的属性

arrays - 未显示所有属性

matlab - 在Matlab结构中过滤 'rows'

c - 在C中,如何使用说明符标签将变量的 "string"内容输入到文件名中以生成动态文件名