【发布时间】:2015-03-29 19:18:35
【问题描述】:
我正在编写一些代码,这些代码接收一个文件,然后对这些数字进行处理,我需要将数据放入它自己的数组集中。我已经使用 fscanf 完成了它,但现在我需要使用 strtok 来完成它。如果每件事都进入同一个数组,我可以这样做,但我不知道如何将每个项目放入它自己的数组中。
TL:DR;我需要这样做
/*************************************************************************
3/25/2015
This program takes in a file of the format
PART,2.000,-1,0.050,V
PART,0.975,-1,0.025,V
PART,3.000,+1,0.010,F
GAP,0.000,0.080
does the tolerance analysis
**************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main()
{
FILE *FTIN;
FTIN = fopen ("tin.txt", "r");
float nom[100], tollerance[100];
int SIGNS[100];
char V_F[100];
int status, i, x;
float Act_Gap, Act_Tollerance, Maximum_Gap, Minnimum_Gap, Spec_Minnimum, Spec_Maximum;
if (FTIN == NULL){ //file empty/broken error
printf("ERROR\n");
return (1);
}
else{
for (i=0; status != EOF; i++){ //reads until EoF, even though some guy on stackoverflow taught me it's bad
status = fscanf(FTIN,"PART,%f,%d,%f,%c\n", &nom[i], &SIGNS[i], &tollerance[i], &V_F[i]); //scans for a part
status = fscanf(FTIN, "GAP,%f,%f\n", &Spec_Minnimum, &Spec_Maximum); //scans for a gap
}
使用 strtok。
【问题讨论】:
-
你至少可以尝试使用strtok阅读any manual page
-
我已经阅读手册页好几天了,我只是愚蠢。
-
使用谷歌可以给你一些examples,比如this one,reading file line by line在stackoverflow上被问到了