Cfile write problem [modified]
-
I am trying to write out an array and all I am getting is junk in the file. Here's the code:
short frameData\[3648\]; short \*p; file.Open( "C:\\\\test.txt", CFile::modeCreate|CFile:: modeReadWrite); p = (short \*)Frameptr; for ( int i=0; i<= 3647; i++) { frameData\[i\] = \*p; p++; } //write out data file.Write(frameData,sizeof(short)\*3647); file.Close();
modified on Monday, July 14, 2008 2:34 PM
-
I am trying to write out an array and all I am getting is junk in the file. Here's the code:
short frameData\[3648\]; short \*p; file.Open( "C:\\\\test.txt", CFile::modeCreate|CFile:: modeReadWrite); p = (short \*)Frameptr; for ( int i=0; i<= 3647; i++) { frameData\[i\] = \*p; p++; } //write out data file.Write(frameData,sizeof(short)\*3647); file.Close();
modified on Monday, July 14, 2008 2:34 PM
aei_totten wrote:
...all I am getting is junk in the file.
How are you verifying this? What should be in the file?
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
aei_totten wrote:
...all I am getting is junk in the file.
How are you verifying this? What should be in the file?
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
I open the file and there are a bunch of weird chars, boxes, greek letters, etc. and the data is an array of numbers in the 646-780 range. I step through with the debugger and the array is populated fine.
-
I open the file and there are a bunch of weird chars, boxes, greek letters, etc. and the data is an array of numbers in the 646-780 range. I step through with the debugger and the array is populated fine.
Well, you asked to write the array as a binary format into the file, so each short from your array will be writte as two bytes in the file (thus resulting in garbage characters). You first have to convert each short into a string and write that string into the file.
Cédric Moonen Software developer
Charting control [v1.4] OpenGL game tutorial in C++ -
I open the file and there are a bunch of weird chars, boxes, greek letters, etc. and the data is an array of numbers in the 646-780 range. I step through with the debugger and the array is populated fine.
and for some reason it isn't posting a less than or equal to in my loop, I even tried to go back and edit it with no luck.
-
Well, you asked to write the array as a binary format into the file, so each short from your array will be writte as two bytes in the file (thus resulting in garbage characters). You first have to convert each short into a string and write that string into the file.
Cédric Moonen Software developer
Charting control [v1.4] OpenGL game tutorial in C++Thanks that was it, duh.
-
Thanks that was it, duh.
-
I am trying to write out an array and all I am getting is junk in the file. Here's the code:
short frameData\[3648\]; short \*p; file.Open( "C:\\\\test.txt", CFile::modeCreate|CFile:: modeReadWrite); p = (short \*)Frameptr; for ( int i=0; i<= 3647; i++) { frameData\[i\] = \*p; p++; } //write out data file.Write(frameData,sizeof(short)\*3647); file.Close();
modified on Monday, July 14, 2008 2:34 PM
Okay new problem. I decided to write the file each element at a time. Here is the new bit of code...
for ( int i=0; i<= 3647; i++) { frameData\[i\] = \*p; temp.Format("%d ",frameData\[i\]); file.Write(temp,sizeof(temp)); p++; }
but instead of getting each number followed by a blank which is what I had intended. I get some of the numbers on top of each other (like four in a row) and some of them correct.
-
aei_totten wrote:
Thanks that was it, duh.
Yeah, imagine that! The file had in it what you told the computer to put in it! Wow, what will they think of next?
led mike
-
Okay new problem. I decided to write the file each element at a time. Here is the new bit of code...
for ( int i=0; i<= 3647; i++) { frameData\[i\] = \*p; temp.Format("%d ",frameData\[i\]); file.Write(temp,sizeof(temp)); p++; }
but instead of getting each number followed by a blank which is what I had intended. I get some of the numbers on top of each other (like four in a row) and some of them correct.
oh yeah temp is a CString
-
Okay new problem. I decided to write the file each element at a time. Here is the new bit of code...
for ( int i=0; i<= 3647; i++) { frameData\[i\] = \*p; temp.Format("%d ",frameData\[i\]); file.Write(temp,sizeof(temp)); p++; }
but instead of getting each number followed by a blank which is what I had intended. I get some of the numbers on top of each other (like four in a row) and some of them correct.
for ( int i=0; i <= 3647; i++ )
{
frameData[i] = *p;
// temp.Format("%d ",frameData[i]); <-- Wrong format specifier for short int
temp.Format("%hd ",frameData[i]);
// file.Write(temp,sizeof(temp)); <-- Wrong string length
file.Write(temp, temp.GetLength() * sizeof(TCHAR));
p++;
}If you're writing all text, maybe CStdioFile is a better class to use... Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
-
Sure but I like to pronounce that Shamoo ;) Have you tried one? Does it really work? Or is it a misinfomercial? :laugh:
led mike
led mike wrote:
Does it really work?
Works great on the commercial! I haven't tried one though. German technology - it HAS to be good!
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
for ( int i=0; i <= 3647; i++ )
{
frameData[i] = *p;
// temp.Format("%d ",frameData[i]); <-- Wrong format specifier for short int
temp.Format("%hd ",frameData[i]);
// file.Write(temp,sizeof(temp)); <-- Wrong string length
file.Write(temp, temp.GetLength() * sizeof(TCHAR));
p++;
}If you're writing all text, maybe CStdioFile is a better class to use... Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Thanks so much. I can't believe how much c++/mfc I had forgotten since switching to C sharp and Java. But somethings you just can't beat in c++. :-D
-
Thanks so much. I can't believe how much c++/mfc I had forgotten since switching to C sharp and Java. But somethings you just can't beat in c++. :-D
-
led mike wrote:
Does it really work?
Works great on the commercial! I haven't tried one though. German technology - it HAS to be good!
Mark Salsbery Microsoft MVP - Visual C++ :java:
Mark Salsbery wrote:
German technology - it HAS to be good!
Two and a half years ago, the bursting of the dot-com bubble erased most of the market value of Germany's technology sector. Now, the market itself is being scrapped.
Deutsche Börse, which runs the Frankfurt stock exchange, announced today that it would close the Neuer Markt -- the technology-related market that is Germany's answer to the Nasdaq -- by the end of 2003.
led mike