简体   繁体   English

将char添加到数组中并返回

[英]adding char into an array and returning

Im new to c and am trying to understand pointers. 我是C语言的新手,正在尝试理解指针。 here I am opening a file and reading the lines given. 在这里,我正在打开文件并阅读给定的行。 Im trying to append these lines into an array and return it from the function. 我试图将这些行添加到数组中,然后从函数中返回它。 I dont seem to be appending or accessing the array correctly. 我似乎没有正确附加或访问数组。 output[count] = status; 输出[计数] =状态; gives an error with mismatched char and char *. 使用不匹配的char和char *给出错误。

Im essentially trying to get an array with a list of words given by a file where each element in the array is a word. 我本质上是试图获得一个包含文件给出的单词列表的数组,其中数组中的每个元素都是一个单词。

char *fileRead(char *command, char output[255]) {

    int count = 0;
    char input[255];
    char *status;

    FILE *file = fopen(command, "r");
    if (file == NULL) {
        printf("Cannot open file\n");
    } else {
        do {
            status = fgets(input, sizeof(input), file);
            if (status != NULL) {
                printf("%s", status);
                strtok(status, "\n");

                // add values into output array
                output[count] = status;
                ++count;
            }
        } while (status);
    }
    fclose(file);

    return output;
}

I access fileRead via: 我通过以下方式访问fileRead:

...
        char commandArray[255];
        char output[255];
        int y = 0;
        char *filename = "scriptin.txt";
        strcpy(commandArray, fileRead(filename, output));
        // read from array and pass into flag function
        while (commandArray[y] != NULL) {
            n = flagsfunction(flags, commandArray[y], sizeof(buf), flags.position, &desc, &parentrd, right, left, lconn);
            y++;
...

Example of Read from file Line by line then storing nonblank lines into an array (array of pointer to char (as char* )) 从文件中逐行读取然后将非空白行存储到数组(指向char的指针数组(如char* ))的示例

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

//for it does not exist because strdup is not a standard function. 
char *strdup(const char *str){
    char *ret = malloc(strlen(str)+1);
    if(ret)
        strcpy(ret, str);
    return ret;
}

//Read rows up to 255 rows
int fileRead(const char *filename, char *output[255]) {
    FILE *file = fopen(filename, "r");

    if (file == NULL) {
        perror("Cannot open file:");
        return 0;
    }
    int count = 0;
    char input[255];
    while(count < 255 && fgets(input, sizeof(input), file)) {
        char *line = strtok(input, "\n");
        if(line)//When it is not a blank line
            output[count++] = strdup(line);//Store replica
    }
    fclose(file);

    return count;
}

int main(void){
    char *output[255];//(`char *` x 255)
    int number_of_read_line = fileRead("data.txt", output);

    for(int i = 0; i < number_of_read_line; ++i){
        printf("%s\n", output[i]);
        free(output[i]);//Discard after being used
    }

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM