Click here to Skip to main content
15,877,376 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hello everyone,

I am doing a mfc dialog based application.In my dialog box, i have combo boxes,edit controls.Now i have to read the text in those boxes and write it into a file.For this i use a structure will be like this,

struct config
float version_number;
char name[20];
int n_tcp;
struct config myfile;

There is no problem with writing Char value.But i am getting some unknown symbols '' like this when writing integer into text file.I am using the following code to write into text file.

CFile file_object(filepath,CFile::modeCreate|CFile::modeWrite);

Is there anyother way to write integer value in structure properly..Pls guide me,


Since you are writing whole structure in file, the structure goes into file as sequence of bytes, not as string/text. For example if you write an int (4-byte) into file, you will NOT see the actual value of integer, but some bizarre value. Dont worry, and you need not to worry. When you read the integer, in sequence-of-bytes form, you get your integer properly.
In short, you write 4 bytes, irrespective of the value of integer. Then you read 4-bytes, and you get value in int.

Similarly, you are writing the whole structure (which is 28 bytes), you'd definitely see those characters. When you read the same structure are sequence of 28-bytes, and typecast into same structure, you'd get the same values!

You should set the structure to null,with memset/ZeroMemory to get the clear picture. Open file in Visual Studio (in BINARY EDITOR).
Share this answer
If you need to write the numeric values as human-readable text than you may use use CString and CStdioFile classes, for instance:
CStdioFile file_object(filepath,CFile::modeCreate|CFile::modeWrite);
CString sValue;
sValue.Format(_T("%f"), myfile.version_number);
file_object.WriteString(","); // field separator
file_object.WriteString(","); // field separator
sValue.Format(_T("%d"), myfile.n_tcp);
file_object.WriteString("\n"); // record separator

Share this answer

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