Link to home
Start Free TrialLog in
Avatar of ssrg
ssrg

asked on

Structures & files

When I write a record in a file, the details doesn't get stored properly.  I make use of fwrite() function.  The record when written into a file, they are some junk characters stores.  The records when asked to be displayed from the file, the details are correct.  What is the problem when writing into the file
Avatar of Exceter
Exceter
Flag of United States of America image

>> When I write a record in a file, the details doesn't get stored properly.  

Yes they are being stored properly.

The record when written into a file, they are some junk characters stores.  

fwrite() wrote the integer values to disk in binary. So naturally they will not show up as text in a text editor.

The records when asked to be displayed from the file, the details are correct.  What is the problem when writing into the file

There is nothing wrong with it. That is the way fwrite is supposed to work. The reason it writes the integers to disk in binary is to save space. Suppose you had the number 30000. This would take 5 bytes to store as ASCII text. Where as binary this can be represented with only 2 bytes.

Exceter
Avatar of Kocil
Kocil

Exceter answered it all.

If you want the file is not a junk of characters, you have to convert the binary data to text when you write it,
and convert the text back to binary when you read it.
For example.

typedef struct {
  int a;
  int b;
} MyRecord;


void Store(char *fname, MyRecord *r)
{
  FILE *f = fopen(fname, 't+w');
  fprintf(f, "%d %d\n", r->a, r->b);
  fclose(f);
}

void Read(char *fname, MyRecord *r)
{
  char buffer[100];
  FILE *f = fopen(fname, 't+r');
  /* fscanf is sometimes so strange */
  /* so we do this with gets and sscanf */
  fgets(f, buffer);
  sscanf(buffer, "%d %d", &r->a, &r->b);
  fclose(f);
}

It seems as though most of the solution has been answered.  However, it cannot be assumed that the program wrote integers.  Even if it wrote character data, there would be junk in the file, usually ^@ symbols.  These are pads to fill in the space occupied by each member of the struct.  For instance, if you have a char name[20] variable, and write the name Tom to the file, it would contain Tom and then 17 pad sybmols so that when you read it back it occupies 20 chars.
ASKER CERTIFIED SOLUTION
Avatar of Exceter
Exceter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
as its already been pointed..
 read the file . the way you wrote it ..  if u wrote using fwrite then read with fread .. if u wrote a struct .. then read a struct .. if u wrote an integer then read integer..

if u wrote text ( using formatted output routines like printf).. then read as text .. and only in this case .. u can read the text with some text editor..
What are you tring to store and how are you tring to store it?
Avatar of Mayank S
The junk characters will always be there because when you're writing an entire structure to a file, you'll be using fwrite (). Now, fwrite () will write a record of the whole size of the structure. Meaning that, even if a string member of the structure has been assigned a value smaller than its maximum length, the rest of the characters also have default values and are written to th file.

e.g., if you have:

struct xyz
{
 char str[80] ;
 int a ;

} var ;

var.str = "abcd" ;

The record written to the file will have "abcd" in the start, followed by a null character, followed by 75 dummy characters (initialized when the variable was declared) because fwrite () has to write as many characters as the maximum length states. That is how fread () will be able to read it from the file, because it will read exactly a record of the maximum size of the structure variable, and accordingly group the data members.

I faced the same problems when I was working on an address book, and I managed to solve it by writing a function (which should be called for every string, after reading the string), in which I assigned all the characters from the index of the current length of the string to its maximum length, as nulls ('\0'). That worked.

Mayank.
How about rating it now?
>> var.str = "abcd"

God! I'm sorry.

strcpy ( var.str, "abcd" ) ;
Nothing has happened on this question in over 10 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by Exceter.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer