[英]assign a variable value to dynamic c array
Hello I want to assing my count value to this 'ob' structs 'p_id' variable.您好,我想将我的计数值分配给这个 'ob' 结构 'p_id' 变量。 How can I do that?
我怎样才能做到这一点? I read other values from file but I need an id for selection and I use count for that.
我从文件中读取了其他值,但我需要一个用于选择的 id,并且我使用 count 来进行选择。 But I don't know how to assing it.
但我不知道如何评估它。
My.txt file;我的.txt 文件;
0 0
0 0
0 1
0 1
1 1
1 1
FILE *fp;
struct ob{
int x;
int y;
int p_id;
};
struct ob* ptr;
int count=0;
ptr = (struct ob*)malloc(sizeof(struct ob));
while(!feof(fp))
{
++count;
fscanf (fp,"%d%d%d", &ptr->x, &ptr->y, &ptr->p_id=count); /* in here I want to assing count to p_id */
printf("%d %d %d",ptr->x, ptr->y, ptr->p_id);
}
fclose (fp);
}
If you're simply looking to assign a sequence number to ptr->p_id
, remove it from the fscanf
call and assing the value after it:如果您只是想为
ptr->p_id
分配一个序列号,请将其从fscanf
调用中删除并在其后赋值:
fscanf (fp,"%d%d", &ptr->x, &ptr->y);
ptr->p_id=count;
If you want the value to be optional, you can move the assignment before the call and let fscanf
overwrite it if it's present:如果您希望该值是可选的,您可以在调用之前移动分配并让
fscanf
覆盖它(如果存在):
ptr->p_id=count;
fscanf (fp,"%d%d%d", &ptr->x, &ptr->y, &ptr->p_id);
The return value of fscanf
allows you to see if the value was updated or not: fscanf
的返回值允许您查看该值是否已更新:
RETURN VALUE
On success, these functions return the number of input items
successfully matched and assigned; this can be fewer than provided
for, or even zero, in the event of an early matching failure.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.