C编程,char指针数组。

时间:2022-07-31 21:16:11

I am trying to create a program which takes out strings from a text file, and stores them into an array.

我正在尝试创建一个程序,它从文本文件中取出字符串,并将它们存储到一个数组中。

Suppose from the text file I get delims: "one", "dfs", "w342"

假设从文本文件中,我得到了delims:“1”、“dfs”、“w342”

After I get these delims, I want to create an array with the same size as number of tokens found, and elements in the array have to be char pointers to "one", "dfs", "w342".

在我得到这些delims之后,我想创建一个与所找到的令牌数量相同的数组,数组中的元素必须是char指向“一个”、“dfs”、“w342”。

int lexer(FILE* file){

char line[50];
char* delim;    
char* tokenArray[100];
int i = 0;

while(fgets(line,sizeof(line),file)){
    printf("%s\n", line);

    if(is_empty(line) == 1)
        continue;

    delim = strtok(line," ");


    if(delim == NULL)
        printf("%s\n", "ERROR");

    while(delim != NULL){

        if(delim[0] == '\n'){
        //rintf("%s\n", "olala");
            break;
        }
        tokenArray[i] = strdup(delim);
        printf("Token IN array: %s\n", tokenArray[i]);
        i= i+ 1;
        delim = strtok(NULL, " ");

    }   
}

printf("%s\n", tokenArray[6]);

fclose(file);
return 0;

Now I am getting the delims and I'm storing them into an array, but I don't think I'm doing it right because I'm not storing the pointers. I also want to send this tokenArray into different functions. What should I do?

现在我得到了delims,我将它们存储到一个数组中,但是我不认为我做对了因为我没有存储指针。我还想把这个tokenArray发送到不同的函数中。我应该做什么?

1 个解决方案

#1


0  

I tested your code with very small modification and you can pass the tokenArray to a function:

我用很小的修改测试了您的代码,您可以将tokenArray传递给函数:

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

void test(char *tokenArray[]) {
    printf("Test %s\n", tokenArray[2]);
}

int main(void) {

    char line[50];
    char *delim;
    char *tokenArray[100];
    int i = 0;
    FILE* file;

    file = fopen("data.txt","r"); // read mode

    while (fgets(line, sizeof(line), file)) {
        printf("%s\n", line);

        delim = strtok(line, " ");


        if (delim == NULL)
            printf("%s\n", "ERROR");

        while (delim != NULL) {

            if (delim[0] == '\n') {
                //rintf("%s\n", "olala");
                break;
            }
            tokenArray[i] = strdup(delim);
            printf("Token IN array: %s\n", tokenArray[i]);
            i = i + 1;
            delim = strtok(NULL, " ");

        }
    }

    printf("%s\n", tokenArray[2]);
    test(tokenArray);
    fclose(file);
    return 0;
}

File data.txt

文件data.txt

one dfs w342

Output

输出

one dfs w342
Token IN array: one
Token IN array: dfs
Token IN array: w342
w342
Test w342

#1


0  

I tested your code with very small modification and you can pass the tokenArray to a function:

我用很小的修改测试了您的代码,您可以将tokenArray传递给函数:

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

void test(char *tokenArray[]) {
    printf("Test %s\n", tokenArray[2]);
}

int main(void) {

    char line[50];
    char *delim;
    char *tokenArray[100];
    int i = 0;
    FILE* file;

    file = fopen("data.txt","r"); // read mode

    while (fgets(line, sizeof(line), file)) {
        printf("%s\n", line);

        delim = strtok(line, " ");


        if (delim == NULL)
            printf("%s\n", "ERROR");

        while (delim != NULL) {

            if (delim[0] == '\n') {
                //rintf("%s\n", "olala");
                break;
            }
            tokenArray[i] = strdup(delim);
            printf("Token IN array: %s\n", tokenArray[i]);
            i = i + 1;
            delim = strtok(NULL, " ");

        }
    }

    printf("%s\n", tokenArray[2]);
    test(tokenArray);
    fclose(file);
    return 0;
}

File data.txt

文件data.txt

one dfs w342

Output

输出

one dfs w342
Token IN array: one
Token IN array: dfs
Token IN array: w342
w342
Test w342