簡體   English   中英

如何將輸入從文件和命令行 arguments 重定向到我的 C 程序到圖形節點?

[英]How to redirecting input into my C program from a file and command line arguments to graph node?

圖節點如下

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
...

該程序使用命令行 arguments 運行如下

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

我想將它們全部保存在單獨的變量命令行中 arguments 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 編程解決?

您可以使用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]);
    strcpy(arg5, argv[5]);

要從文件中獲取信息,您可以使用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

您還可以使用輸入文件,例如包含您顯示的數據的文件,例如。

1 1 100 300 傑拉德
2 3 200 450 葉奈法
...

然后只傳遞一個命令行參數,文件的路徑和名稱,例如:
executable.exe "C:\\filename.txt"
這將使程序更容易在一個步驟中容納盡可能多的玩家,並消除容易出現語法不一致的用戶輸入,從而使解析數據變得困難或不可能。

可以使用以下函數讀取和解析該文件,如下所示:
fopen (和fclose
fgets
斯特克,
斯特羅爾

執行讀取和解析部分的代碼(具有最少的錯誤檢查)如下:

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

int linecount(const char *file);

int main(int argc, char *argv[])
{
    char line[80];
    char *tok = NULL;
    char *temp;
    errno = 0;
    int count = 0;

    if(argv != 2) 
    { 
        //exit with message
    }

    count = linecount(argv[1]);
    Node *n = calloc(count, sizeof(*n));

    FILE *fp = fopen(argv[1], "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;

}

int linecount(const char *file)
{
    char line[80];
    int i = 0;

    FILE *fp = fopen(file, "r");
    if(fp)
    {
        while(fgets(line, 80, fp))
        {
            i++;
        }
        fclose(fp);
    }
    return i;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM