Click here to Skip to main content
16,016,580 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi everyone,
I am doing a command line application, which will do two simple things.
1.Write file
2.Read file
I have array of structure like this,
struct MenuDesign
{
	int MenuId;
	char IdofBit[10];
};
struct MenuDesign objMenu[1000];


Here i am trying to write my structure into text file,using following piece of code.

FILE *MenuFile;
char BufferText[sizeof(objMenu)];
memset(BufferText,0,sizeof(BufferText));
memcpy(BufferText,&objMenu,sizeof(objMenu));
fopen_s(&MenuFile,"D:\\mlkcfg.txt", "w");
fwrite(BufferText,sizeof(struct MenuDesign),1000,MenuFile);
(or)
fwrite(BufferText,sizeof(objMenu),1,MenuFile);
fclose(MenuFile);


I am able to write my structure into file,but while reading i am facing a problem.
Here say i have written 30 records into my text file.I am reading text file with following code.

    errno_t error;
char TempBuff[sizeof(objMenu)];
memset(&objMenu,0,sizeof(objMenu));
memset(TempBuff,0,sizeof(TempBuff));
if((error  = fopen_s( &MenuDispFile, "D:\\mlkcfg.txt", "r")) ==0)
{
ReadSize = fread(&objMenu,sizeof(struct MenuDesign),1000,MenuDispFile);
    (or)
    ReadSize = fread(&objMenu,sizeof(objMenu),1,MenuDispFile);
fclose(MenuDispFile);
}

If i put breakpoint and see my structure(after reading file),till 25th record there is no problem,it is reading perfectly.
But from 26th record, it started reading wrong values.
I dont understand, what i am missing here.

Any help is greatly appreciated,

Shiva.

[edit]Code block added, "Ignore HTML..." option disabled - OriginalGriff[/edit]
Posted
Updated 29-Apr-11 22:32pm
v3
Comments
Niklas L 30-Apr-11 4:25am    
Just out of curiosity: Why do you read and write binary data to a file opened in text mode?
Albert Holguin 30-Apr-11 14:32pm    
that's probably the problem, good catch :)

1 solution

fread() will translate carriage return / line-feed pair into just a line-feed character. Use a binary stream instead for reading and writing. Add ‘b‘ to fopen_s open mode.

Update: Tried to answer CPallinis comment on my android, but hit delete instead. Thick fingers. :/
His comment was about always checking return values of fwrite and fread, which is a good advice.
 
Share this answer
 
Comments
Albert Holguin 30-Apr-11 14:31pm    
this is likely the issue, use binary mode for both writing and reading... my 5
Shiva S.S 2-May-11 0:16am    
Thanks Niklas,

You are absolutely right, i have to use binary mode for both reading and writing.

For this i need to add a single line before fopen_s().

_set_fmode(_O_BINARY);

After this it works very fine.Once again thank everyone helped me in this.
Niklas L 2-May-11 2:44am    
You're welcome.
fopen_s(&MenuFile,"D:\\mlkcfg.txt", "wb");
would also work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900