Question

Filling binary (byte) integers values into array from a file and returning a pointer pointing at...

Filling binary (byte) integers values into array from a file and returning a pointer pointing at array, I'm using C programming language

Here is my code https://pastebin.com/xp7wSZgj

My output: https://pastebin.com/VU6JGWcX

My issue is my buffer array isn't being allocated, that's why I commented out free. fread() isn't reading in all the values! How do I fix that? Detailed explanation and how I can go about fixing it. Only the first value is taken in by the array also  I'm student no advanced concepts

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#######################################
           input.txt
#######################################
4 5 6 7 7 8



#######################################
              main.c
#######################################
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>


int *read_int_deltas(char *fname, int *len){

    int maxLen = 100;
    int count = 0;

    FILE* fpointer;
    int* buffer;
    fpointer = fopen(fname, "r");

    if(fpointer == NULL){
        *len=-1;
        return NULL;
    }

    buffer = (int *)malloc((maxLen)*sizeof(int));

    while(fscanf(fpointer, "%d", &buffer[count]) != EOF) {
        count++;
    }   
    fclose(fpointer);

    *len = count;

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

    //free(buffer);
    return buffer;
}

int main(void) {
    int l;
    read_int_deltas("input.txt", &l);
    return 0;
}

================

Try this code.. It works

Why your code doesn't work=>
You are reading the file size in bytes, and then dividing it by 4 to get how many integers should be present.. but that is not correct..

Because file holds everything in form of characters only, and one character is 1 byte only.. So if file contains "4321", then its size is 4 bytes.. If you divide it by size of integer, you get value 1, but that is not correct.. because you need 4 integers instead of 1 integer..
hence i am taking a sufficiently large array, and then putting required number of values into that. Let me know if any issues.

Add a comment
Know the answer?
Add Answer to:
Filling binary (byte) integers values into array from a file and returning a pointer pointing at...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Assignment 5 will be way different. It will be more like what you will receive in...

    Assignment 5 will be way different. It will be more like what you will receive in a programming shop. By that I mean that some things are built for you and others you will need to create or finish. P.S. part of your grade will include: Did you add in the required files? Did you rename your project? does your linear searches work? Did you put your code in the correct modules? did you change modules that you weren't supposed...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT