-1

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;
}
Votes + Comments
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...
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by wildgoose
0
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
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.