简体   繁体   中英

Reading integers from txt file in C

I`m making a file reader that reads integers numbers line by line from a file. The problem is that is not working. I think I am using fscanf in a wrong way. Can someone help me?

I had already look for answers in others questions but I can`t find anything that explain why I my code is not working.

int read_from_txt(){
    FILE *file;
    file = fopen("random_numbers.txt", "r");
    //Counting line numbers to allocate exact memory
    int i;
    unsigned int lines = 0;
    unsigned int *num;
    char ch;
    while(!feof(file)){
        ch = fgetc(file);
        if (ch == '\n'){
            lines++;
        }
    }
    //array size will be lines+1
    num = malloc(sizeof(int)*(lines+1));
    //storing random_numbers in num vector
    for(i=0;i<=lines;i++){
        fscanf(file, "%d", &num[i]);
        printf("%d", num[i]);
    }
    fclose(file);
}

The txt file is like:

12 
15
32
68
46
...

But the output of this code keeps giving "0000000000000000000..."

You forgot to "rewind" the file:

fseek(file, 0, SEEK_SET);

Your process of reading goes through the file twice - once to count lines, and once more to read the data. You need to go back to the beginning of the file before the second pass.

Note that you can do this in a single pass by using realloc as you go: read numbers in a loop into a temporary int , and for each successful read expand the num array by one by calling realloc . This will expand the buffer as needed, and you would not need to rewind.

Be careful to check the results of realloc before re-assigning to num to avoid memory leaks.

You could try to use the getline function from standard IO and add the parsed numbers into the array using only one loop. See the code below. Please check https://linux.die.net/man/3/getline

Also, you can use the atoi or strtoul functions to convert the read line to an integer. Feel free to check https://linux.die.net/man/3/atoi or https://linux.die.net/man/3/strtoul

The code below evaluate a file with a list of numbers and add those numbers to a C integer pointer

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

int main(int argc, char ** argv) {
    FILE * file;

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

    size_t read;
    char * line = NULL;
    size_t line_len = 0;

    size_t buffer_size = 10;
    int * buffer = (int *)malloc(sizeof(int) * buffer_size);

    int seek = 0;
    while((read = getline(&line, &line_len, file)) != -1) {
        buffer[seek++] = atoi(line);

        if (seek % 10 == 0) {
            buffer_size += 10;
            buffer = (int *)realloc(buffer, sizeof(int) * buffer_size);
        }
    }

    for (int i = 0; i < seek; i++) {
        printf("%d\n", buffer[i]);
    }

    free(buffer);
    fclose(file);
}

If you aren't sure which conversion function should you use. You can check the difference between atoi and sscanf at What is the difference between sscanf or atoi to convert a string to an integer?

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