学习处理文件,尝试通过引用 fcn 传递结构数组
Learning to process files, trying to pass array of structures by reference to a fcn
我试图将一个结构数组传递给一个函数,然后让该函数处理一个文件并用值填充结构数组。该文件是一个文本文件,包含:
Gates M 60
Jobs M 55
Jane F 45
这将填充数组的结构。前任。 person[0] 应该包含 Gates M 60。
目前我没有得到这个程序的任何错误,但它只是不将文件处理到 fscanf 的结构中。我引用的结构有误吗?
我还想知道是否有一种方法可以使 for 循环适用于任何大小的结构数组——这意味着我可以用什么来替换 for 循环中的“2”条件,以便它只不断填充一个足够大小的数组,直到 运行 没有要处理的信息?
#include <stdio.h>
struct Person
{
char lastname[30];
char gender;
unsigned int age;
};
int function(struct Person array[])
{
FILE *cfPtr;
if((cfPtr = fopen("C:\Users\Nick\Desktop\textfile","r"))==NULL)
{
printf("File cannot be opened");
}
else
{
for(int i=0;i<2;i++)
{
fscanf(cfPtr,"%10s %c %3d",&array[i].lastname,&array[i].gender,&array[i].age);
}
}
}
int main(void)
{
struct Person person[3];
function(&person[3]);
printf("%s %c %d\n",person[0].lastname, person[0].gender, person[0].age);
printf("%s %c %d\n",person[1].lastname, person[1].gender, person[1].age);
printf("%s %c %d\n",person[2].lastname, person[2].gender, person[2].age);
}
Currently I do not get any errors for this program but it simply does not process the file into the structure at the fscanf. Am I referencing the structure wrong?
你快到了,只是你错过了一些事情:
for(int i=0;i<2;i++)
应该是 for(int i=0;i<3;i++)
因为你永远不会像现在这样阅读第三行。
- 在使用 scanf 读取并存储在字符串中时删除
&
。也就是把fscanf(cfPtr,"...",&array[i].lastname,...);
改成fscanf(cfPtr,"...",array[i].lastname,...);
。字符串(即 char lastname[30];
)已经是一个数组,当按名称使用数组时,您实际上会在其第一个元素上获得一个指针,因此不需要 &
.
- 函数(
function(&person[3]);
) 的调用不正确:&person[3]
表示您获取的是person 数组的第4 个(越界)元素的地址。您需要传递的是数组本身,它只是 function(person)
。这与上一条中的原因相同。
- 另外,为什么你的函数返回一个
int
而你没有返回任何东西?
I was also wondering if there was a way to make the for loop work for any size of array of structures-- meaning what could I replace the "2" condition in my for loop with in order for it to just continually fill an array of adequate size until it ran out of information to process?
有一种方法,就是使用类型为struct Person
的动态数组,先用malloc
分配space,然后在每个数组中调用realloc
函数内部的迭代。当然,您需要记录阅读了多少 objects/lines。您可以通过关注 this or this SO 问题或搜索和阅读 C 中的动态数组来找到有关您所问内容的更多信息。
我试图将一个结构数组传递给一个函数,然后让该函数处理一个文件并用值填充结构数组。该文件是一个文本文件,包含:
Gates M 60
Jobs M 55
Jane F 45
这将填充数组的结构。前任。 person[0] 应该包含 Gates M 60。 目前我没有得到这个程序的任何错误,但它只是不将文件处理到 fscanf 的结构中。我引用的结构有误吗?
我还想知道是否有一种方法可以使 for 循环适用于任何大小的结构数组——这意味着我可以用什么来替换 for 循环中的“2”条件,以便它只不断填充一个足够大小的数组,直到 运行 没有要处理的信息?
#include <stdio.h>
struct Person
{
char lastname[30];
char gender;
unsigned int age;
};
int function(struct Person array[])
{
FILE *cfPtr;
if((cfPtr = fopen("C:\Users\Nick\Desktop\textfile","r"))==NULL)
{
printf("File cannot be opened");
}
else
{
for(int i=0;i<2;i++)
{
fscanf(cfPtr,"%10s %c %3d",&array[i].lastname,&array[i].gender,&array[i].age);
}
}
}
int main(void)
{
struct Person person[3];
function(&person[3]);
printf("%s %c %d\n",person[0].lastname, person[0].gender, person[0].age);
printf("%s %c %d\n",person[1].lastname, person[1].gender, person[1].age);
printf("%s %c %d\n",person[2].lastname, person[2].gender, person[2].age);
}
Currently I do not get any errors for this program but it simply does not process the file into the structure at the fscanf. Am I referencing the structure wrong?
你快到了,只是你错过了一些事情:
for(int i=0;i<2;i++)
应该是for(int i=0;i<3;i++)
因为你永远不会像现在这样阅读第三行。- 在使用 scanf 读取并存储在字符串中时删除
&
。也就是把fscanf(cfPtr,"...",&array[i].lastname,...);
改成fscanf(cfPtr,"...",array[i].lastname,...);
。字符串(即char lastname[30];
)已经是一个数组,当按名称使用数组时,您实际上会在其第一个元素上获得一个指针,因此不需要&
. - 函数(
function(&person[3]);
) 的调用不正确:&person[3]
表示您获取的是person 数组的第4 个(越界)元素的地址。您需要传递的是数组本身,它只是function(person)
。这与上一条中的原因相同。 - 另外,为什么你的函数返回一个
int
而你没有返回任何东西?
I was also wondering if there was a way to make the for loop work for any size of array of structures-- meaning what could I replace the "2" condition in my for loop with in order for it to just continually fill an array of adequate size until it ran out of information to process?
有一种方法,就是使用类型为struct Person
的动态数组,先用malloc
分配space,然后在每个数组中调用realloc
函数内部的迭代。当然,您需要记录阅读了多少 objects/lines。您可以通过关注 this or this SO 问题或搜索和阅读 C 中的动态数组来找到有关您所问内容的更多信息。