• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • Last Modified:

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
0
ssrg
Asked:
ssrg
1 Solution
 
ExceterCommented:
>> 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
0
 
KocilCommented:
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);
}

0
 
ellesdCommented:
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ExceterCommented:
>> However, it cannot be assumed that the program wrote integers.

I was using integers for examples. Sorry if that was not clear. :-)

Exceter
0
 
akshayxxCommented:
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..
0
 
cybervagrantCommented:
What are you tring to store and how are you tring to store it?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
How about rating it now?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> var.str = "abcd"

God! I'm sorry.

strcpy ( var.str, "abcd" ) ;
0
 
jmcgOwnerCommented:
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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now