Searching for "fread()"

Q:

If the following structure is written to a file using fwrite(), can fread() read it back successfully?

struct emp

{

    char *n;

    int age;

};

struct emp e = { "Sujay",15};

FILE *fp;

fwrite (&e, sizeof (e), 1, fp);

Answer

No, since the structure contains a char pointer while writing the structure to the disk using fwrite() only the value stored in the pointer n would get written. When this structure is read back the address would be read back but it is quite unlikely that the desired string would be present at this address in memory

Report Error

View answer Workspace Report Error Discuss

Subject: Programming