【发布时间】:2018-05-15 13:35:34
【问题描述】:
我正在尝试从 txt 文件中读取变量。 文件 .txt 是这样的:
john 10
mark 230
peter 1
我想将这些值传输并保存在一个数组中,例如 array[0] = 10、array[1] = 230 等,而不考虑名称。我在下面粘贴了我的代码,我想知道如何使用下面的代码编辑它
int conf[4], i = 0, c;
FILE *file_conf;
file_conf = fopen("conf.txt", "r");
if(file_conf == NULL){
fprintf(stderr, "Error\n");
exit(EXIT_FAILURE);
} else {
while((c = fgetc(file_conf)) != EOF) {
fscanf(file_conf, "%d", &conf[i]);
printf("%d\n", conf[i]);
i++;
}
}
【问题讨论】:
标签: c arrays parsing variables