?
Solved

Structures & files

Posted on 2003-02-20
11
Medium Priority
?
183 Views
Last Modified: 2010-04-15
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
Comment
Question by:ssrg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 8

Expert Comment

by:Exceter
ID: 7987531
>> 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
 
LVL 5

Expert Comment

by:Kocil
ID: 7987677
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
 
LVL 2

Expert Comment

by:ellesd
ID: 7988088
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
Technology Partners: 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!

 
LVL 8

Accepted Solution

by:
Exceter earned 200 total points
ID: 7988368
>> However, it cannot be assumed that the program wrote integers.

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

Exceter
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 7991062
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
 

Expert Comment

by:cybervagrant
ID: 7996510
What are you tring to store and how are you tring to store it?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8117562
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8544813
How about rating it now?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8544818
>> var.str = "abcd"

God! I'm sorry.

strcpy ( var.str, "abcd" ) ;
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10010854
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question