Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VC8.0
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);
file_object.Write(reinterpret_cast(&myfile),sizeof(config));
 
Is there anyother way to write integer value in structure properly..Pls guide me,
 
Shiva..
Posted 19-Apr-10 0:43am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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).
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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(myfile.name);
  file_object.WriteString(","); // field separator
  sValue.Format(_T("%d"), myfile.n_tcp);
  file_object.WriteString("\n"); // record separator
  ...
Smile | :)
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 19 Apr 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100