Problem with read in CFile class
-
In addition to Richard's suggestion, narrow the problem down to just a small handful of lines. Upwards of 95% of the code you've shown is not part of the problem scope (i.e., is irrelevant).
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
I did other example to prove somthing, I did :
CStdioFile f;
// Se abre el fichero para escritura CString path= GetUserHomeDir() + \_T("\\\\temporal.txt"); f.Open(path, CFile::modeCreate | CFile::modeWrite | CFile::typeText); double a=9.8; CString b=\_T("hello"); f.WriteString(b); f.Write(&a,sizeof(double)); f.Close();
I see the content from the file, and only wrote hello. This is the content from the file: hello™™™™™#@
-
I did other example to prove somthing, I did :
CStdioFile f;
// Se abre el fichero para escritura CString path= GetUserHomeDir() + \_T("\\\\temporal.txt"); f.Open(path, CFile::modeCreate | CFile::modeWrite | CFile::typeText); double a=9.8; CString b=\_T("hello"); f.WriteString(b); f.Write(&a,sizeof(double)); f.Close();
I see the content from the file, and only wrote hello. This is the content from the file: hello™™™™™#@
antonio343 wrote:
I see the content from the file, and only wrote hello.
The above code shows you follow the word "hello" with a
double
value, so the file content is correct. You also do not show any code to read the file so I am still not sure what your problem is.Unrequited desire is character building. OriginalGriff
-
antonio343 wrote:
I see the content from the file, and only wrote hello.
The above code shows you follow the word "hello" with a
double
value, so the file content is correct. You also do not show any code to read the file so I am still not sure what your problem is.Unrequited desire is character building. OriginalGriff
The content from the file isn't correct!!!!! I wrote hello and 9.8 and it isn't the content of the file. In adittion, I made a method to read the file
CStdioFile f;
CString b;
double a;// Se abre el fichero para escritura CString path= GetUserHomeDir() + \_T("\\\\temporal.txt"); f.Open(path, CFile::modeRead | CFile::typeText); f.ReadString(b); f.Read(&a,sizeof(double)); f.Close();
And it dont read well. It read hello#@ and in the other variable, read rubbish
-
The content from the file isn't correct!!!!! I wrote hello and 9.8 and it isn't the content of the file. In adittion, I made a method to read the file
CStdioFile f;
CString b;
double a;// Se abre el fichero para escritura CString path= GetUserHomeDir() + \_T("\\\\temporal.txt"); f.Open(path, CFile::modeRead | CFile::typeText); f.ReadString(b); f.Read(&a,sizeof(double)); f.Close();
And it dont read well. It read hello#@ and in the other variable, read rubbish
antonio343 wrote:
The content from the file isn't correct!
Yes it is, you do not understand what you are writing. The line:
f.Write(&a,sizeof(double));
writes the double value exactly as it is held in memory as a binary value 8 bytes wide. If you wish to convert it to a textual representation then you need to format it into a text string with
sprintf()
orCString
, using the appropriate format codes.Unrequited desire is character building. OriginalGriff
-
antonio343 wrote:
The content from the file isn't correct!
Yes it is, you do not understand what you are writing. The line:
f.Write(&a,sizeof(double));
writes the double value exactly as it is held in memory as a binary value 8 bytes wide. If you wish to convert it to a textual representation then you need to format it into a text string with
sprintf()
orCString
, using the appropriate format codes.Unrequited desire is character building. OriginalGriff
ok.. But I don't need to show. In the first function that I post here, I need to copy the value of variable in a file, and after load the value in variable, I dont know how to do it, becouse I try with fread/fwrite, CFile class, CStdio class, and I don't get something. The problem is the variable are several kind of dates, CString, double, int... but I think that the main problem is save and load CString kind.
-
ok.. But I don't need to show. In the first function that I post here, I need to copy the value of variable in a file, and after load the value in variable, I dont know how to do it, becouse I try with fread/fwrite, CFile class, CStdio class, and I don't get something. The problem is the variable are several kind of dates, CString, double, int... but I think that the main problem is save and load CString kind.
There is no problem with what you suggest, you merely need to ensure that what you try to read back exactly mirrors what you write. This means that you must read back the exact number of bytes that you have written in the first place. With elementary items (
int
,double
,char
etc.) this is not difficult, but with strings you need to add some extra information to know how many characters to read in; you could do this by adding an integer in front of the string that specifies its length. Alternatively, use one of the formatting functions to convert all your data to string items and use some form of field separation (e.g. CSV, XML) to write in lines of text which you can then reconvert when reading back.Unrequited desire is character building. OriginalGriff
-
There is no problem with what you suggest, you merely need to ensure that what you try to read back exactly mirrors what you write. This means that you must read back the exact number of bytes that you have written in the first place. With elementary items (
int
,double
,char
etc.) this is not difficult, but with strings you need to add some extra information to know how many characters to read in; you could do this by adding an integer in front of the string that specifies its length. Alternatively, use one of the formatting functions to convert all your data to string items and use some form of field separation (e.g. CSV, XML) to write in lines of text which you can then reconvert when reading back.Unrequited desire is character building. OriginalGriff
I tried to do this:
int nLength = b.GetLength() + 1; // string lenght in characters
int nBytes = nLength * sizeof(TCHAR); // buffer size in bytes
f.Write(&nLength, sizeof(int)); // write string length
f.Write(b, nBytes); // write string
f.Write(&a,sizeof(double));And the problem is the same, this is the content of the file:
h o l a C a r a c o l a š™™™™™#@
-
I tried to do this:
int nLength = b.GetLength() + 1; // string lenght in characters
int nBytes = nLength * sizeof(TCHAR); // buffer size in bytes
f.Write(&nLength, sizeof(int)); // write string length
f.Write(b, nBytes); // write string
f.Write(&a,sizeof(double));And the problem is the same, this is the content of the file:
h o l a C a r a c o l a š™™™™™#@
"Here is the content of the file"... Just how are you getting that output to show us? Is that from some editor or file dump utility? If you write a file containing some binary data (and you do, both 'nLength' and 'a' are written to the file as plain binary data), and then you ask an editor to display the content of the file, of course you are going to get garbage. Editors make the reasonable assumption that the entire data file is 'char' and will attempt to interpret the file in that way, rendering all the characters into some printable form, including trash characters as you observe. For binary data, editors and other display programs are inappropriate for looking at the contents to see if it is correct. Raw binary file dumpers are the best for that, then you can see every byte.
-
I tried to do this:
int nLength = b.GetLength() + 1; // string lenght in characters
int nBytes = nLength * sizeof(TCHAR); // buffer size in bytes
f.Write(&nLength, sizeof(int)); // write string length
f.Write(b, nBytes); // write string
f.Write(&a,sizeof(double));And the problem is the same, this is the content of the file:
h o l a C a r a c o l a š™™™™™#@
So what is the problem? Do you think your binary values are going to somehow magically transform themselves to textual representations? As I said before this content is correct and if you read it back using the information that you used to write it out you will have no problems. Or, you could adopt my other suggestion of converting everything to strings and using CSV, XML or some other system to manage your data.
Unrequited desire is character building. OriginalGriff
-
So what is the problem? Do you think your binary values are going to somehow magically transform themselves to textual representations? As I said before this content is correct and if you read it back using the information that you used to write it out you will have no problems. Or, you could adopt my other suggestion of converting everything to strings and using CSV, XML or some other system to manage your data.
Unrequited desire is character building. OriginalGriff
But the file isn't binary, it is text file, and dont show well the content I'm sorry, you are right, the content is read well Thank you so much, finally I get to do run well my function