How does fread() work.

This is a discussion on How does fread() work. within the C Programming forums, part of the General Programming Boards category; Hello, Ive got an assignment to do, that requires I take a .dat file, with data points sorted like this: ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    5

    How does fread() work.

    Hello, Ive got an assignment to do, that requires I take a .dat file, with data points sorted like this:

    x1 y1 x2 y2 etc.

    Unfortunately its arranged for bigendian pcs and one of the assignment takes is to change it to littlendian so the data actually means something,

    This is the code I have so far. Basically I know that I have to read in the data, put them into a union as an integer, do bitwise operations on them to rearrange into littleend, and then return that union as a wonderful new float that means something.

    I just don't know how fread() works. I've read up on it online, but...its not very clear.
    If I have 10 numbers to read, how do i tell it to put them into 10 unions? Is this how it does things normally?

    I kind of want teaching more than the answer, if thats k.

    Thanks alot.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    
    
    int status;
    FILE *data;
    int no_of_floats;
    int no_of_datapoints;
    
    typedef struct{
    	double x_value;
    	double y_value;
    }data_point;
    
    typedef union{
    	int data_dummy;
    }converter;
    
    int main(int argc, char** argv)
    {
    	struct stat file_status;
    	data_point* data_value;
    	converter* data_converter;
    	int datacheck;
    	
    	data = fopen("px390_2_bin.dat", "r");
    	if (data == NULL) /*Test for error*/
    		{ 
    		printf("Error opening file.\n");
    		exit(8); 
    		}
    		
    	if (stat("px390_2_bin.dat", &file_status) != 0)
    	{
    	perror("Could not find the size of the file 'px390_2_bin.dat'\n");
    	}
    	else
    	{
    	printf("The size of the file is: &#37;ld\n", file_status.st_size);
    	}
    	no_of_floats = file_status.st_size/sizeof(float);
    	printf("The number of data values in the file is: %d\n", no_of_floats);
    	no_of_datapoints = no_of_floats / 2;
    	
    	data_value = (data_point*)malloc(sizeof(data_point) * no_of_datapoints);
    	
    	data_converter = (converter*)malloc(sizeof(int) * no_of_floats);
    
    	datacheck = fread((char *)&data_converter, sizeof(int), no_of_floats, data);
    	if (datacheck != no_of_floats)
        {
        	printf ("Error writing data to file.\n");
        }
        else
        {
        	printf ("Successfully wrote data to file.\n");
        }
        printf("datacheck = %d\n", datacheck);
        printf("no_of_floats = %d\n", no_of_floats);
        printf("The first int is: %d\n", data_converter[1].data_dummy);
    	
    	free(data_value);
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    5
    I've kinda figured it out through plain desperation.

    My fread line should have been this:

    Code:
    datacheck = fread(data_converter, sizeof(int), no_of_floats, data);
    As in it didn't need a pointer to memory, I think thats because I'd already assigned data_converter its memory using malloc previously, so I guess it just needed to know what array of unions I wanted to put everything into.

    Feel free to confirm this though, one of you experienced folk

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using fread on stdin
    By nadroj in forum C Programming
    Replies: 29
    Last Post: 10-23-2008, 02:03 PM
  2. Another link from Microsoft about bug in fread
    By vart in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-06-2008, 11:56 AM
  3. How to load pixels of BMP into an array
    By brconner in forum Windows Programming
    Replies: 10
    Last Post: 06-02-2007, 04:30 AM
  4. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  5. fread doesnt work
    By condorx in forum C Programming
    Replies: 2
    Last Post: 06-05-2002, 02:36 PM

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