如何将输入从文件和命令行参数重定向到我的C程序中,并将其重定向到图形节点?

问题描述 投票:0回答:2

图节点如下

typedef struct node{
int x_position, y_position;
int max_rate, min_rate;
char *name;
struct node * prev;
}Node;

输入文本文件如下

2 2 200 300 name
1 5 240 499 name2
3 5 400 500 name3
...

此程序使用以下命令行参数运行

./program arg1 arg2 arg3 arg4 arg5 < input.txt

我想将它们全部保留在单独的变量中,命令行参数assingned_val1 = arg1 ,assigned_val2 = arg2 ...,

input.txt的内容如下分配我的Node图形变量

1 1 100 300 Gerald --> node( x_position = 1; y_position =1
                           min_rate = 100 ; max_rate = 300
                           name = "Gerald" 
2 3 200 450  Yennefer --> node( x_position = 2; y_position =3
                           min_rate = 200 ; max_rate = 450
                           name = "Yennefer"

...如何使用C编程解决?

c algorithm data-structures user-input graph-algorithm
2个回答
0
投票

输入文件,例如您所显示的文件。

1 1100 300杰拉尔德2 3 200 450 Yennefer...

可以在fopen循环中使用fgetswhile进行读取,然后使用strtok进行解析,并使用strtol转换整数变量。执行读取和解析部分(带有最少错误检查)的代码如下:

typedef struct node{
    int x_position;
    int y_position;
    int max_rate;
    int min_rate;
    char *name;
    struct node * prev;
}Node;


int main(void)
{
    char line[80];
    char *tok = NULL;
    int iVal = -1.1;
    char *temp;
    errno = 0;


    Node n = {0};

    n.name = calloc(80, 1);//this line would not be required if name
                           //was defined as char name[80]; instead
                           //of using a pointer.
    if(!n.name) 
    {
        //handle error 
    } 

    FILE *fp = fopen("C:\\inputfile.txt", "r");
    if(fp)
    {
        while(fgets(line, 80, fp))
        {
            tok = strtok(line, " ");//parse x_position
            if(tok)
            {
                n.x_position = strtol(tok, &temp, 0);
                tok = strtok(NULL, " ");//parse y_position
                if(tok)
                {
                    n.y_position = strtol(tok, &temp, 0);
                    tok = strtok(NULL, " ");//parse max_rate
                    if(tok)
                    {
                        n.max_rate = strtol(tok, &temp, 0); 
                        tok = strtok(NULL, " ");//parse min_rate
                        if(tok)
                        {
                            n.min_rate = strtol(tok, &temp, 0);
                            tok = strtok(NULL, " ");//parse name
                            if(tok)
                            {
                                strcpy(n.name, tok);
                            }
                        }

                    }

                }
            }
        }
        fclose(fp);
    }
    return 0;

}

0
投票

您可以使用atoi将字符串转换为int,并使用strcpy将字符串复制到字符串:

    int arg1, arg2, arg3, arg4;
    char arg5[256];

    arg1 = atoi(argv[1]);
    arg2 = atoi(argv[2]);
    arg3 = atoi(argv[3]);
    arg4 = atoi(argv[4]);

要从文件中获取信息,可以使用fgetssscanf。下面的示例仅适用于count节点的数组(不适用于链接列表,但是您可以将此想法用于链接列表)。 fgets用于读取文件,然后将文件的值存储到该行中。您可以使用sscanf将此行中的信息分配给struct的每个值。

    int count = 1;
    while (fgets(line, sizeof(line), file)) {
        n = realloc(n, count * sizeof(Node));
        n[count-1].name = malloc(sizeof(char) * 16);
        if(!n)
            return - 1;
        sscanf(line, "%d %d %d %d %s\n", &n[count-1].x_position, &n[count-1].y_position, &n[count-1].min_rate, &n[count-1].max_rate, n[count-1].name);
        count++;
    }

完整的测试代码:

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

typedef struct node{
    int x_position, y_position;
    int max_rate, min_rate;
    char *name;
    //struct node * prev;
}Node;

int main(int argc, char const *argv[]) {
    char line[256];
    int arg1, arg2, arg3, arg4;
    char arg5[256];
    Node * n = malloc(sizeof(Node));
    if(!n)
        return - 1;
    FILE * file = fopen(argv[6], "r");
    if (!file)
        return -1;
    arg1 = atoi(argv[1]);
    arg2 = atoi(argv[2]);
    arg3 = atoi(argv[3]);
    arg4 = atoi(argv[4]);
    strcpy(arg5, argv[5]);

    int count = 1;
    while (fgets(line, sizeof(line), file)) {
        n = realloc(n, count * sizeof(Node));
        n[count-1].name = malloc(sizeof(char) * 16);
        if(!n)
            return - 1;
        //printf("%s", line); 
        sscanf(line, "%d %d %d %d %s\n", &n[count-1].x_position, &n[count-1].y_position, &n[count-1].min_rate, &n[count-1].max_rate, n[count-1].name);
        count++;
    }

    printf("\narg1= %d, arg2 = %d, arg3 = %d, arg4 = %d, arg5 = %s\n", arg1, arg2, arg3, arg4, arg5);

    for(int i = 0; i < count-1; i++) {
        printf(" %d  %d  %d  %d  %s\n",
        n[i].x_position, n[i].y_position, n[i].min_rate, n[i].max_rate, n[i].name);
    }

}

结果:

#cat text.txt

2 2 200 300 name
1 5 240 499 name2
3 5 400 500 name3
./test 1 2 3 4 abc text.txt

arg1= 1, arg2 = 2, arg3 = 3, arg4 = 4, arg5 = abc
 2  2  200  300  name
 1  5  240  499  name2
 3  5  400  500  name3
© www.soinside.com 2019 - 2024. All rights reserved.