简体   繁体   中英

C programming: Read lines of numbers from a file and store it in an array

I'm kinda new to programming so sorry for a stupid question. I've been tasked to read first two lines (numbers) and store them as variable n, and the other one in m. Other numbers bellow those two lines need to be read and store it in a dynamic array (malloc to be exact). Each line has exactly one number. I'm having trouble trying to accomplish this.

Here's my code so far:

#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;
}

File ulazna.txt has:

3
3

This works as intended but when I write:

3
3
1
2
3
4
5
6
7
8
9

My program prints random numbers from this file.

So, I need to read 3 and 3 into n and m. Then, from 1 to 9 into the array list.

Thanks for help in advance:)

Dynamic array size

If you want to dynamically allocate memory for the list, you would have to count the number of lines in the file first.

#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;
}

Fixed array size

If you already know that the number of lines in the file is going to be 11, the array has a length of 9 and there is no need to dynamically allocate memory. But because your assignment requires it, I have implemented 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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