Guys before i post my problem , lemme say that, i HAVE gone through the previous blogs/forums on daniweb, so please dont get annoyed with this topic again.
But none of them answer my question

My question is, "How do you write a structure to a file and retrieve data from it.
1)I think fwrite is the best option. Assuming s is the structure instance, you could use a code like

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

But the problem is, this doesnt work if the structure contains int.

Why ?????
Because, all though the 'int' is saved in the file(in disk), a text doc wont be able to display because it only displays ascii characters.

2)So the way out would be to convery the number to a string , and then write into the file.So even the text doc will be able to display it char-by-char

3)This sound logically flawless. But after converting the number to a string, how do you make that string also a part of the structure so that i can still do my

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

4)OK so i tried doing this. I am storing the number 0 as roll no. Notice the small tweak i did, 0 + '0'. So it displays even in the text doc cuz its actuallt printing ascii of (0+48). But clearly this doesnt work if the number is greater than 0.

#include<stdio.h>
#include<conio.h>

struct mystruct
{
  int i;
  char ch;
};

int main(void)
{
   FILE *fp;
   char* data;
   struct mystruct s;
   clrscr();

   if((fp= fopen("e:\\tc1\\bin\\tceg.txt", "wb")) == NULL) /* open file TEST.$$$ */
   {
      fprintf(stderr, "Cannot open output file.\n");
      return 1;
   }
   s.i = 0+'0';
   s.ch = 'A';
   fwrite(&s, sizeof(s), 1, fp); /* write struct s to file */
   fclose(fp); /* close file */

   rewind(fp);
   fp=fopen("e:\\tc1\\bin\\tceg.txt", "rb");
   fgets(data,100,fp);
   printf("\nThe data read from the file is : %s",data);

   getch();
   return 0;
}

5)Guys , so basically can you help me with a code that will take any integer (PART OF A STRUCTURE) and display it on the text doc.

You could have fixed length structs (records), which are normally written out in binary mode, and read in as binary data, using fwrite(). For structs of varying length, (where any member of the struct might have a varying length), the advantages of binary mode just fade away, and they should be written out as text, (using fprintf), and read in as text as well.

Here, you should not be rewinding your fp after you close the file, and fgets() is used for text, not binary, data.

Either use fread() and fwrite(), (binary mode) or use fgets() and sscanf(), along with fprintf(), in text mode. Don't mix the two.

Why ?????
Because, all though the 'int' is saved in the file(in disk), a text doc wont be able to display because it only displays ascii characters.

That has nothing to do with fwrite not working and everything to do with your misunderstanding of how fwrite works. fwrite directly writes the bytes of an int to the file, not the string representation of an int that you would get with something like printf.

2)So the way out would be to convery the number to a string , and then write into the file.So even the text doc will be able to display it char-by-char

Yep. Or you could use fprintf, which does the conversion for you.

3)This sound logically flawless. But after converting the number to a string, how do you make that string also a part of the structure so that i can still do my [..fwrite]

You don't. What you do is write a function that textually serializes your structure to the file:

void serialize(FILE *out, struct mystruct obj)
{
    fprintf("%d %c\n", obj.i, obj.c);
}

fwrite is better suited for when you want to dump the bytes of an object to a file and don't give a hoot about readability in a text editor.

5)Guys , so basically can you help me with a code that will take any integer (PART OF A STRUCTURE) and display it on the text doc.

The same way you would print it to stdout.

thanks a lot guys, just 1 last doubt,
Suppose s is my structure instance,

struct abc
{
 char* name;
 int id;
}s;

And i use this to write to my file.

fwrite(&s,sizeof(s),1,fp)

Then can i use format specifiers to specify the format/spacing between the variable inside the structure.(while using FWRITE AND NOT FPRINTF)

Edited 5 Years Ago by tubby123: n/a

Dude, just stop using fwrite until you understand it. Your code is just getting worse.

To answer your question, no, you can't use format specifiers with fwrite because fwrite doesn't do any formatting. But your more immediate problem is the char* member of the structure. Using fwrite you'll write an address rather than the contents of the string. When you read the structure back from the file (presumably with fread), the address will be meaningless. This is a variation of the shallow copy problem.

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