Can anyone tell me whats wrong with the code. I get two errors. I dont know how to fix it

#include<stdio.h>

typedef struct
{
	int i;
	float f;
} STR;

void fun(FILE*, int);

void main()
{
    STR* sp;
    int x;
    printf("Enter number of record to be printed: ");
    scanf("%d", &x);

    sp = fopen("SAMPLE.DAT", "rb");
    fun(sp, x);
}

void fun(FILE* sp, int x)
{
    int data;
    int i;

    if(ferror(sp))
        printf("Error Occured");
    else
    {
		while(!feof(sp))
		{
			fread(&data,sizeof(int),x,sp);
			printf("%d %f",data->i, data->f);
		}
    }
    return;
}
mvmalderen commented: In reply to the title of your thread, there are numerous things: first you did not use code tags, second you used void main(), and maybe some other things as well... -4
FILE *sp;
//STR* sp;

sp = fopen("SAMPLE.DAT", "rb");

don't forget when you return from fun()

if (NULL != sp)
{
   fun( sp, x );
    fclose(sp);
}

Also you did an ferror() when there was no error! Do a NULL check before calling fun().

while(x-- && !feof(sp))     // loop for (X) records or end of file
{
STR data;    <--    record is a STR not an int!
//fread(&data,sizeof(int),x,sp);   // You have one buffer not (x) ints!
fread( &data, sizeof(data), 1, sp );  // read only one record
//printf("%d %f",data->i, data->f);   // Wrong   not a pointer
  printf( "%d %f", data.i, data.f );     // a flat record
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.