C - 从命令行到C程序的管道文件,使用strtok()

时间:2014-03-12 02:31:25

标签: c bash file strtok

我回来了!我正在寻找帮助从命令行(bash)将文件传递到C程序的一些帮助,一切正常,除非我尝试每隔一行标记值,它适用于第二行,但不适用于第四行,第六行等。这是我的代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int numExperiments = 0;



void sendToArray(int data[20][10], char *experiments[20]){
    char line[100];
    char line2[100];


    char *temp1;
    int temp;
    int i=0, c=0;
    while(c<20 && strcmp(line, "*** END ***")!=0){
    fflush(stdin);
        scanf("%[^\n]", line);
    scanf("%*c");
        scanf("%[^\n]", line2);
    scanf("%*c");
    temp1=strtok(line2, " ");
        while(i<10 && temp1 !=NULL){

        sscanf(temp1, "%d", &temp);
        data[c][i]=temp;
            temp1=strtok(NULL, " ");
i++;
} 

        experiments[c]=strdup(line);
        c++;

    }
    numExperiments = c-1;

}

void displayAll(int data[20][10], char *experiments[20]){
    int i=0,c=0;

    for(i=0; i<numExperiments; i++){
    printf("\n");
    printf("%s", experiments[i]);
    printf("\n");
        for(c=0;c<10;c++){
            printf("%d ", data[i][c]);

        }
    }
}
void individualAverage(char *name, char *experiments[20], int data[20][10]){
    int i=0, c=0;
    float dataRes=0;
    while( strcmp( name , experiments[i] )!=0 && i<20){
        i++;
    }
    printf("%s\n", experiments[i]);
    for(c=0;c<10;c++){
        dataRes = dataRes+ data[i][c];
    }
    printf("\nAverage is: %f", (dataRes/10));

}

void allAverage(char *experiments[20], int data[20][10]){
    int i=0,a=0;
    float totalAvg=0;
    for(i=0;i<numExperiments;i++){
        for(a=0;a<10;a++)
            totalAvg = totalAvg + data[i][a];
    }
    totalAvg = totalAvg/numExperiments;
    printf("The total average is: %f \n", totalAvg);
}


int main(int argc, const char * argv[])
{
    char *experiments[20];
    int data[20][10];

    int b=0;
    char name[100];
    sendToArray(data, experiments);
    fflush(stdin);
    while(b==0){
        int input=0;
        printf("\n\n");
        printf("Data set analysis\n");
        printf("1. Show all the data\n");
        printf("2. Calculate the average for an experiment\n");
        printf("3. Calculate the average across all experiments\n");
        printf("4. QUIT\n");
        printf("Selection: ");
    fflush(stdin);
freopen("/dev/tty", "r", stdin);
        scanf("%d", &input);

        switch (input) {
            case 1:

                    displayAll(data, experiments);

                break;
            case 2:
 fflush(stdin);               
                    printf("\n");                  
 printf("Enter the name of the experiment: ");
                    scanf("%s", name);
                    individualAverage(name, experiments, data);




                break;
            case 3:

                    allAverage(experiments, data);

                break;
            case 4:b=1;;
                break;

            default:
                printf("Oops, something went wrong!\n");
                break;
        }
    }
    return 0;

}

此外,个人平均功能给我一个核心转储。谁能明白为什么?

dislayAll的输出是:

Experiment One
3 10 8 7 3 2 9 7 5 6 
Experiement Two
0 3 2090266759 1011 -1216787076 -1218461544 -1074571312 -1216447140 -1218499016 1074266160 
Control Group
78514226 1815496840 0 0 1 2342 -1216565784 -1216567208 -1216559671 -1218461352

1 个答案:

答案 0 :(得分:0)

好的,我不完全确定程序中发生了什么。对我来说有点有趣的是你重新开放的标准。 Bash将处理管道。

#include <stdio.h>
#include <string.h>

/* Remove newline characters from end of string */
void strip(char* str)
{
  int len;
  if (str == NULL)
    {
      return;
    }
  len = strlen(str);
  if (str[len - 1] == '\n')
    {
      str[len - 1] = '\0';
    }
  len = strlen(str);
  if (str[len - 1] == '\r')
    {
      str[len - 1] = '\0';
    }
}

int main()
{
    char* line = NULL;
    size_t len = 0;
    int line_num = 0;
    while (getline(&line, &len, stdin) != -1)
    {
        strip(line);
        if (line_num % 2 == 0)
            printf("[%d]: %s\n", line_num, line);
        line_num++;
    }
    return 0;
}

上面的程序用bash读取文件中输入的文字。 我使用以下文件来测试它。你只会看到&#34; print line&#34;在输出上我称之为words.txt

print line
hello world
print line
not this one
print line
secret

然后执行,只需使用     cat words.txt | ./a.out

所有数据都将通过stdin直接流入程序。我希望这有帮助。