首页 > 解决方案 > C 编程:从文件中读取数字行并将其存储在数组中

问题描述

我对编程有点陌生,很抱歉提出一个愚蠢的问题。我的任务是读取前两行(数字)并将它们存储为变量 n,另一行存储在 m 中。需要读取这两行下面的其他数字并将其存储在动态数组中(确切地说是 malloc)。每行只有一个数字。我很难做到这一点。

到目前为止,这是我的代码:

#include <stdio.h>
#define MAX_LINE 5

int main() {

    FILE* in;
    FILE* out;
    int n, m, list = (int*)malloc(9 * sizeof(int));

    in = fopen("ulazna.txt", "r");
    out = fopen("izlazna.txt","w");

    if ((in && out) == NULL)
    {
        printf("Error opening the file...");
        return -1;
    }

    while (fscanf(in, "%d\n", &m) != EOF)
    {
        fscanf(in, "%d\n", &n);
        fscanf(in, "%d\n", &m);
        printf("First two: %d %d", n, m);
    }

    //free(list);
    fclose(in);
    fclose(out);
    return 0;
}

文件 ulazna.txt 有:

3
3

这按预期工作,但是当我写时:

3
3
1
2
3
4
5
6
7
8
9

我的程序从此文件中打印随机数。

所以,我需要将 3 和 3 读入 n 和 m。然后,从 1 到 9 进入数组列表。

提前感谢您的帮助:)

标签: c

解决方案


动态数组大小

如果要为列表动态分配内存,则必须先计算文件中的行数。

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

int main(void)
{
    int n, m;
    /* open the file for reading text using "r" */
    FILE *file_in = fopen("ulazna.txt", "r");

    if (!file_in) { /* same as `file_in == NULL` */
        printf("Failed to open file\n");
        return EXIT_FAILURE;
    }

    int lines = 0; /* number of lines */
    int c; /* return value of `fgetc` is of type `int` */

    /* go through each character in the file */
    while ((c = fgetc(file_in)) != EOF) {
       /* increment the number of lines after every line break */
       if(c == '\n')
           ++lines;
    }

    /* reset pointer to start of file */
    rewind(file_in);

    /* check the return value of `fscanf` */
    if (fscanf(file_in, "%d\n", &n) != 1 ||
            fscanf(file_in, "%d\n", &m) != 1) {
        printf("File contains invalid line\n");
        return EXIT_FAILURE;
    }
    printf("First two: %d %d\n", n, m);

    /* no need to cast `malloc`, because it returns `void *` */
    int *list = malloc(lines * sizeof(int));
    for (int i = 0; i < lines - 2; ++i) {
        /* check the return value of `fscanf` */
        if (fscanf(file_in, "%d\n", &list[i]) != 1) {
            printf("File contains invalid line\n");
            return EXIT_FAILURE;
        }
        printf("%d\n", list[i]);
    }

    free(list); /* always free memory allocated by malloc */
    fclose(file_in);
    return EXIT_SUCCESS;
}

固定数组大小

如果您已经知道文件中的行数将是 11,则数组的长度为 9,无需动态分配内存。但是因为你的任务需要它,所以我已经实现了malloc.

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

int main(void)
{
    int n, m;
    /* open the file for reading text using "r" */
    FILE *file_in = fopen("ulazna.txt", "r");

    if (!file_in) { /* same as `file_in == NULL` */
        printf("Failed to open file\n");
        return EXIT_FAILURE;
    }

    /* check the return value of `fscanf` */
    if (fscanf(file_in, "%d\n", &n) != 1 ||
            fscanf(file_in, "%d\n", &m) != 1) {
        printf("File contains invalid line\n");
        return EXIT_FAILURE;
    }
    printf("First two: %d %d\n", n, m);

    /* the array has a fixed size of 9 */
    const int size = 9;
    int *list = malloc(size * sizeof(int));
    /* 
     * If you do not have to use malloc, remove the
     * above line and uncomment the following line:
     *
     * int list[size];
     */
    for (int i = 0; i < size; ++i) {
        /* check the return value of `fscanf` */
        if (fscanf(file_in, "%d\n", &list[i]) != 1) {
            printf("File contains invalid line or has to few lines\n");
            return EXIT_FAILURE;
        }
        printf("%d\n", list[i]);
    }

    fclose(file_in);
    return EXIT_SUCCESS;
}

推荐阅读