user2407616
user2407616

Reputation: 31

Reading numbers from a file into an array in C

I am very new to C, and I am trying to write a program that reads in a file that contains a number of integers in binary and prints the integers back out in reverse order. The integers in the test file are: 200 300 -145629 67 11111111 -1 8000 0

The integers are written using binary output from another file, so you cannot normally read the file. This is the code I have so far:

#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h> #include <sys/types.h>

int main(int argc, char *argv[]){ 
    int *integers; 
    char *file; 
    int fd; 
    struct stat fileStat; 
    int value; 
    int i; 

    if(argc != 2){ 
        fprintf(stderr, "usage: intrev <input file> <output file>\n");
        exit(1); 
    } 

    file = argv[1]; 
    fd = open(file, O_RDONLY); 
    if(fd < 0){ 
        fprintf(stderr, "error opening file\n"); 
        exit(1); 
    }

    if(fstat(fd, &fileStat) < 0){ 
        fprintf(stderr, "error getting file size\n"); 
        exit(1); 
    } 

    integers = malloc(fileStat.st_size * sizeof(int)); 

    for(i = 0; i < fileStat.st_size; i++){ 
        value = read(fd, integers + i, sizeof(int)); 
        if(value == -1){ 
            fprintf(stderr, "error reading file\n"); 
            exit(1); 
        } 
    } 

    for(i = fileStat.st_size - 1; i >= 0; i--){ 
        printf("%d\n", *integers + i); 
    } 

    if(close(fd) != 0){ 
        fprintf(stderr, "error closing file -- quitting\n"); 
        exit(1); 
    } 

    return(0); 
}

I am required to use fstat and malloc() to set the array size. The program compiles without errors, but when run, I get the numbers 231 through 200 as follows: 231 230 229 . . . 201 200

What is it that I have incorrect? Any help is greatly appreciated.

Upvotes: 0

Views: 290

Answers (4)

Steen
Steen

Reputation: 207

You are allocating an array which is larger than the file in question. Your 'malloc' should allocate 'filestat.st_size' bytes, thus making room for 'filestat.st_size / sizeof( int )' elements.

Your error handling of the 'read()' operation is wrong quote: If the current file offset is at or past the end of file, no bytes are read, and 'read()' returns zero.

Try fixing these...

You should of course also fix error pointed out by R. Sahu

Upvotes: 1

Praveen Kumar
Praveen Kumar

Reputation: 1658

please try the below code

#include <stdio.h>
main()
{

    FILE *file;
    myFile = fopen("Somenumbers.txt", "r");

    //read file into array
    int numberArray[16];
    int i;

    for (i = 0; i < 16; i++)
    {
        fscanf(file, "%1d", &numberArray[i]);
    }

    for (i = 0; i < 16; i++)
    {
        printf("Number is: %d\n\n", numberArray[i]);
    }

}

Upvotes: 0

R Sahu
R Sahu

Reputation: 206567

This line

    printf("%d\n", *integers + i); 

is equivalent to

    printf("%d\n", integers[0] + i); 

due to operator precedence.

You can use:

    printf("%d\n", *(integers + i)); 

or

    printf("%d\n", integers[i]); 

Upvotes: 1

Zia ul Rehman
Zia ul Rehman

Reputation: 11

for(i = fileStat.st_size - 1; i >= 0; i--){ 
        printf("%d\n", *integers + i); 
    } 
in these lines i think you are dereffering integers and than adding value of i, it is actually reading from same location and adding i in it which is being decremented in each iteration. so correct this to read from integers+i and than de reffer. I am a c++ programmer so i am not giving any code but i have pointed out the logical error i think. Hope this will help. Rate this as answer if it helps.

Upvotes: 1

Related Questions