【问题标题】:Store data from columns of a text file to an array. in C将文本文件列中的数据存储到数组中。在 C 中
【发布时间】:2020-11-30 02:07:53
【问题描述】:

我有一个如下所示的文本文件:

Process Priority    Burst   Arrival
1   8   15  0
2   3   20  0
3   4   20  20
4   4   20  25
5   5   5   45
6   5   15  55
7   9   10  70
8   6   15  100
9   5   15  105
10  5   15  115

各列由制表符分隔。 我需要忽略第一行,然后一列中的所有条目将它们存储到一个数组中,例如“int process[10]”。

我找到了这个帖子:Reading in a specific column of data from a text file in C,但是完成的方式非常具体到操作问题中数据的设置方式。

谢谢。

【问题讨论】:

    标签: c ansi-c


    【解决方案1】:

    试试这个。这会从文件中抓取您拥有的所有列,并且符合 ansi

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void parseLine(char *line, int *num1, int *num2, int *num3, int *num4 ){
    
        char* temp;
    
        temp = strchr(line,'\t');
        temp[0] = '\0';
        *num1 = atoi(line);
        line = temp + 1;
    
        temp = strchr(line, '\t');
        temp[0] = '\0';
        *num2 = atoi(line);
        line = temp + 1;
    
        temp = strchr(line, '\t');
        temp[0] = '\0';
        *num3 = atoi(line);
        line = temp + 1;
    
        *num4 = atoi(line);
    }
    
    int main(void)
    {
        FILE* fp;
        char line[100];
        
        int process[10];
        int priority[10];
        int burst[10];
        int arrival[10];
        int i;
        int j;
    
        i = 0;
    
        if ((fp = fopen("./yourfile.txt", "r")) == NULL)
        {
            printf("Error Opening File\n");
            exit(1);
        }
    
        fgets(line, sizeof(line), fp);
    
    
        while (fgets(line, sizeof(line), fp))
        {
            parseLine(line, &process[i],&priority[i],&burst[i],&arrival[i]);
            i++;
        }
    
        for (j = 0; j < 10; j++){
            printf("Process: %d, Priority: %d, Burst: %d, Arrival: %d\n", process[j], priority[j], burst[j], arrival[j]);
        }
    
        fclose(fp);
        return 0;
    }
    

    【讨论】:

    • 我的错,是速度编码没有意识到那里的细微错误,感谢您指出。
    • 对不起,我没有看到这个,谢谢,这绝对是按照我的其余代码。
    【解决方案2】:

    首先对此进行说明,这不是一个完整的解决方案,但我认为它会帮助您解析文件。也代替

    将它们存储到一个数组中,例如“int process[10]”

    我认为最好将它们存储在动态数组中,否则您需要事先知道要提取的列的行数。此代码仅根据您的格式解析文件并打印第一列的值,我认为在您实现动态数组后可以适应您的情况,所以这里是:

    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    /*scan_file: read each of the lines of the file.*/
    int scan_file(char *filename)
    {
        char *line_buf = NULL;
        size_t line_buf_size = 0;
        ssize_t line_size;
        char *next = NULL;
    
        FILE *fp = fopen(filename, "r");
        if (!fp) {
            fprintf(stderr, "Error opening file '%s'\n", filename);
            return 1;
        } 
        /*Get the first line of the file and do nothing with it*/
        line_size = getline(&line_buf, &line_buf_size, fp);
        while (line_size > 0)
        {
            //second and subsecuent lines
            line_size = getline(&line_buf, &line_buf_size,fp);
            if (line_size <= 0)
                break;
            line_buf[line_size-1] = '\0'; /*removing '\n' */
            char *part = strtok_r(line_buf, "    ", &next);
            if (part)
                printf("NUMBER: %s\n", part);
        }
    
        // don't forget to free the line_buf used by getline
        free(line_buf);
        line_buf = NULL;
        fclose(fp);
    
        return 0;
    }
    
    
    int main(void)
    {
        scan_file("file.txt");
        return 0;
    }
    

    注意:这里我假设文件名为file.txt,您应该将其更改为文件名。

    输出:

    NUMBER: 1
    NUMBER: 2
    NUMBER: 3
    NUMBER: 4
    NUMBER: 5
    NUMBER: 6
    NUMBER: 7
    NUMBER: 8
    NUMBER: 9
    NUMBER: 10
    

    我这样说是为了让你可以识别printf("NUMBER: %s\n", part);这一行,在这里你可以将其更改为将这个值推入动态数组的东西。

    【讨论】:

    • 哦,谢谢,是的,我知道限制,因为它实际上是我发布的所有内容(很短),谢谢你,我会实现这个。
    • 一行可能不以'\n' 结尾(可能是输入中的最后一行)导致line_buf[line_size-1] = '\0'; 不正确。
    猜你喜欢
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-11
    • 1970-01-01
    • 2021-03-04
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多