views:

88

answers:

3

I am reading and writting a structure into a text file which is not readable. I have to write readable data into the file from the structure object.

Here is little more detail of my code:

I am having the code which reads and writes a list of itemname and code into a file (file.txt). The code uses linked list concept to read and write data. The data are stored into a structure object and then writen into a file using fwrite.

The code works fine. But I need to write a readable data into the text file.

Now the file.txt looks like bellow,

㵅㡸䍏䥔䥆㘸䘠㵅㩃䠀\䵏㵈䑜㵅㡸䍏䥔䥆㘸䘠\㵅㩃䠀䵏㵈䑜㵅㡸䍏䥔䥆㘸䘠㵅㩃䠀䵏㵈\䑜㵅㡸䍏䥔䥆㘸䘠㵅㩃䠀䵏㵈䑜㵅㡸䍏䥔\䥆㘸䘠㵅㩃䠀䵏㵈

I am expecting the file should be like this,

pencil aaaa
Table bbbb
pen cccc
notebook nnnn

Here is the snippet:

struct Item
{
char itemname[255];
char dspidc[255];
struct Item *ptrnext;
};

  // Writing into the file
printf("\nEnter Itemname: ");
gets(ptrthis->itemname);
printf("\nEnter Code: ");
gets(ptrthis->dspidc);
fwrite(ptrthis, sizeof(*ptrthis), 1, fp);

  // Reading from the file
while(fread(ptrthis, sizeof(*ptrthis), 1, fp) ==1)
{
  printf("\n%s %s", ptrthis->itemname,ptrthis->dspidc);
  ptrthis = ptrthis->ptrnext;
}
+1  A: 

The code works fine

not really:

a) you are dumping the raw contents of the struct to a file, including the pointer to another instance if "Item". you can not expect to read back in a pointer from disc and use it as you do with ptrthis = ptrthis->ptrnext (i mean, this works as you "use" it in the given snippet, but just because that snippet does nothing meaningful at all).

b) you are writing 2 * 255 bytes of potential crap to the file. the reason why you see this strange looking "blocks" in your file is, that you write all 255 bytes of itemname and 255 bytes of dspidc to the disc .. including terminating \0 (which are the blocks, depending on your editor). the real "string" is something meaningful at the beginning of either itemname or dspidc, followed by a \0, followed by whatever is was in memory before.

the term you need to lookup and read about is called serialization, there are some libraries out there already which solve the task of dumping data structures to disc (or network or anything else) and reading it back in, eg tpl.

akira
Thank you. I understood the problem form your solution. Now I thought to restructure the program as below.1. Writing Itemname and Itemcode as a string into a file from the structure member ( using fprintf() ).2. Reading Itemname and Itemcode as a string to structure member( using fscanf() ).Please let me know whether this will work?
Guru
it will work if you implement it correctly :)
akira
A: 

Writing the size of an array that is 255 bytes will write 255 bytes to file (regardless of what you have stuffed into that array). If you want only the 'textual' portion of that array you need to use a facility that handles null terminators (i.e. printf, fprintf, ...).

Reading is then more complicated as you need to set up the idea of a sentinel value that represents the end of a string.

This speaks nothing of the fact that you are writing the value of a pointer (initialized or not) that will have no context or validity on the next read. Pointers (i.e. memory locations) have application only within the currently executing process. Trying to use one process' memory address in another is definitely a bad idea.

ezpz
Thank you. I will try your solution.
Guru
A: 

First of all, I would only serialize the data, not the pointers.

Then, in my opinion, you have 2 choices:

  1. write a parser for your syntax (with yacc for instance)
  2. use a data dumping format such as rmi serialization mechanism.

Sorry I can't find online docs, but I know I have the grammar on paper. Both of those solution will be platform independent, be them big endian or little endian.

Aif