I am trying to read a structure from a .dat file...the first integer to be read is the no of readings, then there is the data in following structure format

struct Oven
                    {
                    int rec_no; 
                    int temp; 
                    int status;
                    char string[10];
                    };

below is the method through which I read the file

   int read_and_process_file()
        {
            int counter;
            FILE *ptr_myfile;
            FILE *ptr_myfile2;
            struct Oven my_record,my_record2; 
            ptr_myfile=fopen("data.dat","rb");
            ptr_myfile2=fopen("out.csv","w+");
            if (!ptr_myfile) 
            { 
                printf("Unable to open file!"); 
                return 1; 
            } 

            fread(&no_of_readings,sizeof(no_of_readings),1,ptr_myfile);
                int value=10;
                unsigned char x;
                for ( counter=1; counter <= 2570; counter++) 
                { 


                    int size = fread(&my_record,sizeof(struct Oven),1,ptr_myfile);
                       if (size != 1)
                       {
                          perror ("Error reading Line ");
                       }


                    name=my_record.string;

                    printf("%d : ",my_record.rec_no);
                    fprintf(ptr_myfile2,"%d,",my_record.rec_no);
                    printf("%d: ",my_record.temp);
                    fprintf(ptr_myfile2,"%d,",my_record.temp);
                    printf("%d : ",my_record.status);
                    fprintf(ptr_myfile2,"%d,",my_record.status);
                    printf("%s\n",name);
                    fprintf(ptr_myfile2,"%s\n",name);



                    if(counter==value)
                    {
                        printf("valuue %d\n",value );
                        value=value+256;
                        fread(&x,sizeof(x),1,ptr_myfile);
                    }



                } 



            fclose(ptr_myfile);
            fclose(ptr_myfile2);
           return 0; 
        }

i found an extra byte first at 10th position and then at every 256th after it so i skipped that byte by reading it int into an unsigned char which is equal to 1 byte (if counter==value in my code)

I have got 2559 records out of 3000. after 2559 i am unable to get others below is the hex dump of the file at 9ff (2559th) record.Please help me out to identify what is wrong with my .dat file

HEX dump of data.dat:
http://i.stack.imgur.com/TcT9z.jpg
My .dat file
https://docs.google.com/file/d/0B5T4nL_0pCoXVDlxc1FQc1ctSk0/edit?usp=sharing

please post the code that writes the file.

line 18: why is the loop hardcoded with that magic number instead of using the int that was previously read?

for(counter = 0; counter < no_of_readings; counter++)

Edited 3 Years Ago by Ancient Dragon

I dont have the code that writes the file... ijust have to read this as it is , it is provided by my professor,
I want to read that amount of records so the loop is hardcoded

Where did you get that structure? What compiler did your teacher use to write that file (the size of an int is not consistent across all compilers.)

Edited 3 Years Ago by Ancient Dragon

This article has been dead for over six months. Start a new discussion instead.