【发布时间】:2010-12-16 11:09:06
【问题描述】:
我有一个如下所示的文本文件:
1 2 4
3 5 2
9 7 6
4 2 6
大小未知,最多 50 行。
我正在尝试将整数存储在结构数组中
typedef struct column{
int col_1;
int col_2;
int col_3;
} column;
我已经创建了 stuct 列的数组
column column[50];
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE * myfile;
int i = 0;
if ((myfile = fopen("/home/numbers.txt","r"))==NULL)
{
printf("File %s not found\n", "/home/numbers.txt");
exit(0);
}
if ("/home/numbers.txt" == NULL)
{
printf("There was an error reading %s", "/home/numbers.txt");
}
while(fscanf(myfile,"%d %d %d", &column[i++].col_1, &column[i].col_2, &column[i].col_3) == 3)
{
printf("\n%d %d %d", column[i].col_1, &column[i].col_2, &column[i].col_3);
}
我得到一个这样的数字列表
-420921 -420924 -420927
这似乎是一些内存地址,因为它们显然不是实际数字。
我的问题是获取整数而不是一些相当随机的数字,我在 printf 中的变量之前尝试过 &,但没有奏效,反之亦然。
非常感谢您的帮助。
【问题讨论】: