problem with fread into array

This is a discussion on problem with fread into array within the C Programming forums, part of the General Programming Boards category; I am trying to read a binary file: Code: FILE *fl = fopen("dta.bin", "rb"); if ( (fopen("data.bin", "rb" )) == ...

  1. #1
    carpe diem
    Join Date
    Jan 2010
    Posts
    46

    Unhappy problem with fread into array

    I am trying to read a binary file:
    Code:
    FILE *fl = fopen("dta.bin", "rb");
            
             if ( (fopen("data.bin", "rb" )) == NULL) 
                    { 
                   printf("input file not found");
                   getchar();
                   exit(1);
                   
                    } 
            
            fseek(fl, 0, SEEK_END);
           
            long len = ftell(fl);//get length of file
            char *ret = (char*)  malloc(len);
          
            
            if(ret == NULL) 
                    { 
                            printf("\nInsufficient memory to read file\n");
                            getchar(); 
                    } 
                    
            fseek(fl, 0, SEEK_SET);
    		//Read data from file into array
            fread(ret, 1, len, fl);
    
            fclose(fl);
        
    		
    	printf  ("size of array: %i", sizeof(ret));
    	printf  ("\nlength of file: %i", len);
    	getchar();
    I can't see what is wrong with the code but the char array "ret" is not getting the values from my input file.
    The output says:
    "size of array:4
    length of file: 102727488"
    when they should be the same length.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    "ret" is a pointer that is 4 bytes long. Therefore, sizeof(ret) will return 4.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    carpe diem
    Join Date
    Jan 2010
    Posts
    46
    ok... so how do I get my char array to be same size as len? and then populate with the contents of my input file?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The only way to know the length of something you allocated with malloc is to store that value in a variable.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,789
    Quote Originally Posted by doia View Post
    ok... so how do I get my char array to be same size as len?
    Your array's length IS the same as the value in len (assuming the malloc call was successful).

    Be aware that if you are attempting to use any of the basic string manipulation functions declared in the string.h header on this data you're reading then you'll want to allocate an additional byte and make sure you properly null-terminate the data.

    Quote Originally Posted by doia
    and then populate with the contents of my input file?
    What you've got should be good as far as reading the data into the array although if you are truly having difficulties then fseek and fread both return values to indicate success or failure, you can try seeing if they return any weird error code.

    [edit]Don't typecast the malloc call.[/edit]
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fread not playing nice; my array is full of zeros
    By voraciousveggie in forum C Programming
    Replies: 14
    Last Post: 03-16-2010, 10:36 PM
  2. Newbie Question: sizeof() problem in function
    By Xeyide in forum C Programming
    Replies: 3
    Last Post: 09-04-2009, 12:05 AM
  3. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM
  4. Problem Putting INTs Into a CHAR Array
    By cram in forum C++ Programming
    Replies: 13
    Last Post: 10-13-2004, 07:53 AM
  5. From stream/file to a string array problem
    By dradsws in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 06:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21