Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于文件-C中的数字创建新变量_C_Variables_Struct - Fatal编程技术网

基于文件-C中的数字创建新变量

基于文件-C中的数字创建新变量,c,variables,struct,C,Variables,Struct,我的任务是根据文件中的输入信息创建发票 我的一切都很好,但这是基于我对输入文件中不同人员数量的事先了解(即2个“客户”=我制作了2个结构) 我的问题是:我如何获得给定的客户数量(在输入文件的第一行中),并使用它创建那个数量的独立结构 示例:我从第一行读入“2”,并创建了2个结构 示例:我从第一行读入“4”,并创建了4个结构 我知道这是一个非常简单的解决方案,使用某种循环,从第一行开始计算直到达到所说的数字,但是我如何创建/初始化正确数量的新结构 我试过的一个例子大致如下: while(j<

我的任务是根据文件中的输入信息创建发票

我的一切都很好,但这是基于我对输入文件中不同人员数量的事先了解(即2个“客户”=我制作了2个结构)

我的问题是:我如何获得给定的客户数量(在输入文件的第一行中),并使用它创建那个数量的独立结构

示例:我从第一行读入“2”,并创建了2个结构

示例:我从第一行读入“4”,并创建了4个结构

我知道这是一个非常简单的解决方案,使用某种循环,从第一行开始计算直到达到所说的数字,但是我如何创建/初始化正确数量的新结构

我试过的一个例子大致如下:

while(j<numOrders){
customer c[j];
j++;
}

谢谢

您需要声明一个指向客户数组的指针。动态初始化会有所帮助。大致如下:

customer *listOfCustomers;
fscanf(input,"%d",&numberOfCustomers);
listOfCustomers = (customer*)malloc(sizeof(customer)*numberOfCustomers);
然后,您可以通过以下方式访问此客户列表:

int i=0;
while(i<numberOfCustomers){
    //Do Something with listOfCustomers[i]
}
访问客户列表的方法保持不变

编辑:在这种情况下如何使用数组

使用上面写的循环,我们将其写成:

//we declare the pointer to customer c in the same manner as explained above.
customer *c;
fscanf(input,"%d",&numberOfCustomers);
c = (customer*)malloc(sizeof(customer)*numberOfCustomers);
while(i<numberOfCustomers){
    fscanf(input,"%s %s",&c[i].first, &c[i].last);
    fscanf(input,"%d",&c[i].numProducts);
    //while loop here
    int j=0;
    while (j<c[i].numProducts){
        fscanf(input,"%s %f %f", &c[i].itemList[j].name, &c1.itemList[j].price, &c[i].itemList[j].weight);
        j++;
    }
    i++
}
//Then for printing the customer information we use a similar loop
i=0;
while(i<numberOfCustomers){
    printf("FirstName: %s LastName: %s",c[i].first, c[i].last);
    int j=0;
    while (j<c[i].numProducts){
        printf(input,"%s %f %f", c[i].itemList[j].name, c1.itemList[j].price, c[i].itemList[j].weight);
        j++;
    }
    i++;
}
//我们以与上述相同的方式声明指向客户c的指针。
客户*c;
fscanf(输入、%d、&numberOfCustomers);
c=(客户*)malloc(客户规模)*客户数量);

而(i您需要首先将输入作为客户数,然后使用循环获取所有客户的信息并将其存储在您想要的位置。

您需要使用动态初始化。2/4结构是什么意思?@Sohaib基本上我会在数字“2”中读取然后是为两个不同的客户提供的信息。但是,这个数量的客户可能会发生变化,所以我到目前为止的:
c1
c2
只起作用,因为我知道文件中有两个客户。谢谢!!!!我想我仍然有点不清楚这些客户到底是如何保存的。比如,我该如何创建基于此的客户结构的数量?你不需要创建大量的客户结构。你只需要创建一个数组。阅读数组:我毫不谦虚地说这句话,只是试图弄明白并学习-我对数组有很好的理解,我的任务要求结构(可能应该在OP中添加)。在这种情况下,我如何使数组正确工作?@bravesaint我以一种示例方式添加了复制您的代码。可以编写类似的循环来打印客户列表,即,
c
。我遇到了与以前相同的错误:
“c”未定义(此函数首次使用)
在读取并保存整个文件后,我需要打印出信息。很抱歉,我应该在OP中提到这一点。我认为这是需要将循环的每个迭代的信息保存到一个单独的结构中。在循环中运行所有这些不是简单地重新编写我以前创建的结构吗?谢谢!谢谢需要制作结构数组并在其中存储客户信息。只需制作一个结构并制作数组即可。
#define N 50
customer listOfCustomers[N]
//we declare the pointer to customer c in the same manner as explained above.
customer *c;
fscanf(input,"%d",&numberOfCustomers);
c = (customer*)malloc(sizeof(customer)*numberOfCustomers);
while(i<numberOfCustomers){
    fscanf(input,"%s %s",&c[i].first, &c[i].last);
    fscanf(input,"%d",&c[i].numProducts);
    //while loop here
    int j=0;
    while (j<c[i].numProducts){
        fscanf(input,"%s %f %f", &c[i].itemList[j].name, &c1.itemList[j].price, &c[i].itemList[j].weight);
        j++;
    }
    i++
}
//Then for printing the customer information we use a similar loop
i=0;
while(i<numberOfCustomers){
    printf("FirstName: %s LastName: %s",c[i].first, c[i].last);
    int j=0;
    while (j<c[i].numProducts){
        printf(input,"%s %f %f", c[i].itemList[j].name, c1.itemList[j].price, c[i].itemList[j].weight);
        j++;
    }
    i++;
}