-3

I'm writing a practice program to read integers from a file and sort them. I'm a little confused about file IO in C. What i have so far is below, I was hoping someone could take a look at it and offer any corrections/suggestions if they have any...

            // TODO: Open input file and do same as above
            char *mode = "r";
            FILE *fp = fopen(inputFile, mode);

            if(fp == NULL){
                    fprintf(stderr, "Can't open input file!");
                    exit(1);
            }

            // Load the numbers into a buffer and get a count
            int buffer[100];
            int count = 0;
            while(fscanf(fp, "%d", &buffer[count]) == 1) {
                    count++;
            }


            // Initialize the array with the proper size
            integers = (int*)malloc(sizeof(count*sizeof(int)));


            // Load the integers into the array
            rewind(fp);
            for(int i = 0; i < count; i++){
                    if(fscanf(fp, "%d", &integers[count] != 1)){
                            fprintf(stderr, "Error loading integers into array");
                            exit(1);
                    }

            }
JayB
  • 397
  • 6
  • 21
  • where do you decleare integers array? – PeerNet Feb 05 '15 at 03:24
  • @PeerNet it's a global int pointer. I've only included a function of my program – JayB Feb 05 '15 at 03:25
  • @JayB You know what `n` does in the `fscanf()` right? Number of characters read so what is your input what if you have 10 in the file? – Gopi Feb 05 '15 at 03:35
  • @Gopi just for reference, see [this answer](http://stackoverflow.com/questions/28334394/need-to-read-an-undetermined-amount-of-integers-from-stdin-into-an-arra/28335093#28335093). That's where the code came from, but OP has misapplied the answer. – user3386109 Feb 05 '15 at 03:37
  • @user3386109 I did exactly as you had written, but I'm trying to reapply it for reading from a file... – JayB Feb 05 '15 at 03:39
  • @user3386109 Sorry about that, forgot to accept your answer. As you said %n returns the number of characters read, I'm rushing and figured it could be applied the same way for fscanf... thanks for the help – JayB Feb 05 '15 at 03:46
  • @JayB The thing with `fscanf` is that it keeps track of where it is in the file automatically, so the `%n` isn't needed like it is with `sscanf`. Instead, use `fscanf` to count the numbers in the file, then `rewind` the file, and use `fscanf` to read the file again. – user3386109 Feb 05 '15 at 03:49
  • @user3386109 I think if you just want to read integers from file and memory allocation should be done dynamically then I would go for `malloc()` followed by consecutive `realloc()` instead of parsing the same file twice – Gopi Feb 05 '15 at 03:53
  • If all you're doing is loading a dynamic array from a file of textual `int`, then [**just do this**](http://ideone.com/CvR7Hh). Lookup *every* function call in the linked code [**here**](http://en.cppreference.com/w/c) and work the algorithm carefully to understand what it does, and see how it fits in to the program. Best of luck. – WhozCraig Feb 05 '15 at 03:55
  • @JayB Yup, that's general the idea. One thing, instead of `&buffer[count]`, you'll want `&temp`. As is, the code could overflow buffer if there's more than 100 numbers in the file. Also, consider Gopi's solution that allows you to get all the numbers while only reading the file once. – user3386109 Feb 05 '15 at 04:04
  • @JayB Is it really needed to parse the file twice why not just parse it once and allocate required memory as shown in the below code? Anyways the edits code looks good. Take care of `count` use some other variable while parsing the file second time it can't be the index to your integers array – Gopi Feb 05 '15 at 04:07

1 Answers1

1

fscanf() returns number of elements successfully read so check against the required number of elements to be read and in your case you can just read values to your array and increment index. Later use the value of index to allocate memory.

int *temp;
integers = malloc(sizeof(int)));
while(fscanf(fp, "%d", &integers[index]) == 1)
{
    index++;
    temp = realloc(integers,sizeof(int) * (index+1));
    if(temp != NULL)
    integers = temp; 
}
Gopi
  • 19,784
  • 4
  • 24
  • 36