【问题标题】:Using strtok to put things into separate arrays使用 strtok 将事物放入单独的数组中
【发布时间】: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。

【问题讨论】:

标签: c arrays input


【解决方案1】:

您所要做的就是一次读取一行(例如使用 fscanf(FTIN, "%s", line)),然后使用 strtok 拆分行,使用逗号作为标记,最后迭代标记并转换他们每个人都到相关领域。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    • 2020-11-03
    • 2017-08-14
    • 2019-03-09
    • 2021-01-03
    相关资源
    最近更新 更多