#include <stdio.h>
#include <stdlib.h>
typedef struct filedata
{
char data[100];
}data_t;
data_t * fname=NULL;
//IS AN ARRAY OF the structure filedata REQUIRED HERE
void quit()
{
printf("\nPress enter to exit");
fflush(stdin);
getchar();
}
int main()
{
char ch;
fname=(data_t *)malloc(sizeof(data_t));
FILE *fptr=NULL;
atexit(quit);
printf("Please enter the file name to read : ");
fflush(stdin);
scanf("%s",fname->data);
fptr=fopen(fname->data,"rb");
if(fptr == NULL)
{
perror("Could not open the file ");
return;
}
printf("\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
printf("Contents of the file %s are : ",fname->data);
[B] while(fread(&fname, sizeof(data_t), 1,fptr) == 1)
{
// what do I put here?
}[/B]
fclose(fptr);
return 0;
}
I want to read any binary file contaning some text or numbers in it on my computer and display it on the stdout.
How do I do it?
Shall I declare an array of the structure file data like `data_t data[100]`?
What should I put in the while loop above to display the contents?
An example : of course if I know the attributes inside the some stucture like name,age etc then I can do something like
while ( fread ( &e, sizeof ( e ), 1, fp ) == 1 )
printf ( "\n%s %d %f", e.name, e.age, e.bs ) ;
But how do I read any text contents of any binary file and display it to stdout?
Can somebody look into it please... may be I could get an idea or something?