|
LiYS wrote: I create another process from the main process and then hide the main process window, but the newly created process window doesn't have the focus(not on the top). I've tried the SetForegroundWindow and AttachThreadInput but to no avail. Is there any other way? I work on XP
If you have the handle to the window, then you can use SetFocus ..
|
|
|
|
|
I've been racking my brain all day with this... Just can't get things going..
I need to be able to read and write variables to a file..
I've got one example to work.. But the reading I get back are box characters...
This prolly has to do with the fact that my progam is in UNICODE mode.
I'm just trying to read and write in a CSV file. (comma seperated values).
But anything that is capable of reading and writing variables to a file would be great..
Example:
// Open the text file we want
CFile cfFile (L"C:\\TextFile.txt", CFile::modeNoTruncate | CFile::modeRead);
CArchive ar (&cfFile, CArchive::load); // Load its contents into a CArchive
// Initialise the variable which holds each line's contents
CString strLine = L"";
if(!ar.ReadString(strLine))
// Read the first line of the CArchive into the variable
return; // Failed, so quit out
do // Repeat while there are lines in the file left to process
{
if(strLine.GetLength() == 0) // If the line is empty, skip it
continue;
CString strText = strLine; // A line of the file
// Initialise the variables that will hold the values
CString strItemName = L"";
CString strPicPath = L"";
CString strSoundPath = L"";
// Extract the first value, and place it in the strItemName variable
AfxExtractSubString(strItemName, strText, 0, ',');
// Extract the second value, and place it in the strPicPath variable
AfxExtractSubString(strPicPath, strText, 1, ',');
// Extract the third value, and place it in the strSoundPath variable
AfxExtractSubString(strSoundPath, strText, 2, ',');
// Do something with these values in the variables
}while(ar.ReadString(strLine));
If I display strItemName, strPicPath or strSoundPath after reading the file... I get blocks..
any ideas?
-- modified at 22:34 Tuesday 31st October, 2006
|
|
|
|
|
After this line
CString strText = strLine; // A line of the file
does strText have a line of comma separated strings?
Don't know if it's necessary but try making your comma constants L','
AfxExtractSubString(strItemName, strText, 0, L',');
|
|
|
|
|
hmmmm.. still dosn't display the character correctly..
Their must be a simple way to read and write int variables to a file.. I don't even really need strings.
|
|
|
|
|
Did you write the ints as ints or as string representations of the ints??
If you don't need to read the file in notepad or the file is not imported to other applications
then you can write binary ints instead of comma-separated strings.
|
|
|
|
|
ok... sounds good..
Just as long as the program can write to the file and read it back to itself.
What should I look up for binary reading and writing?
|
|
|
|
|
For int values use CArchive::Read() and CArchive::Write() or the overloaded extraction/insertion
operators, something like
// to write an int
int IntValue = ...;
ar.Write(&IntValue, sizeof(int)); // or "ar << IntValue;"
// to read an int
int IntValue;
ar.Read(&IntValue, sizeof(int)); // or "ar >> IntValue;"
|
|
|
|
|
Ok... I know i'm real close.. lol
CFile cfFile (L"C:\\TextFile.txt", CFile::modeNoTruncate | CFile::modeReadWrite );
char buf[512];
int variable = 12;
int i;
CArchive ar( &cfFile, CArchive::store, 512, buf );
ar.Write(&variable, sizeof(int));
ar.Close();
CArchive ar2( &cfFile, CArchive::load, 512, buf );
i = ar2.Read(&variable, sizeof(int));
ar2.Close();
temp.Format((L"%d"), i);
AfxMessageBox(temp); //dispalys 0... need 12
|
|
|
|
|
Nevermind... I got it
Thank you very very much Mark... very much appreciated
CFile cfFile (L"C:\\TextFile.txt", CFile::modeNoTruncate | CFile::modeReadWrite );
char buf[512];
Learning = 12;
cfFile.SeekToBegin();
CArchive ar( &cfFile, CArchive::store, 512, buf );
ar.Write(&Learning, sizeof(int));
ar.Close();
cfFile.SeekToBegin();
CArchive ar2( &cfFile, CArchive::load, 512, buf );
ar2.Read(&i, sizeof(int));
ar2.Close();
temp.Format((L"%d"), i);
AfxMessageBox(temp);
|
|
|
|
|
The file pointer, yes
Take care,
Mark
|
|
|
|
|
So now i come to implement a bit of the File I/O but there's a problem, the program just dies.
Im trying to open a file dflt1, or dflt2, dflt3 dflt4, or maybe tech1, tech2, tech3, tech4... i think you get the idea
Anyways, like i said, it just doesn't work. So i changed the filename to 'reallylongword' and it works fine.
Is there a problem with opening files with short names or something?
|
|
|
|
|
The Undefeated wrote: Is there a problem with opening files with short names...
No.
The Undefeated wrote: ...or something?
Possibly.
Have you considered showing the code snippet that does not work as expected?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Err, k.
ifstream ReadFile ("M:\\dflt1");<br />
char word[4];<br />
ReadFile.getline(word, 5);<br />
<br />
cout << "Mmm" << word << endl;<br />
ReadFile.close();
That doesn't work
ifstream ReadFile ("M:\\biglongword");<br />
char word[4];<br />
ReadFile.getline(word, 5);<br />
<br />
cout << "Mmm" << word << endl;<br />
ReadFile.close();
That does
Note that both files have exactly the same content
And you said does not work as expected, it just don't work.
|
|
|
|
|
The Undefeated wrote: char word[4];
ReadFile.getline(word, 5);
Are you expecting this to work (i.e., cramming 5 bytes into a 4-byte spot)?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
WTF? Its an access violation at ReadFile.close()
Am i missing something here? My brain perhaps? Or is this as wierd as i think it is?
Man, i like C# much better - it works
|
|
|
|
|
The Undefeated wrote: Am i missing something here?
I don't know, but I can tell you that it works better to use std::string to read text, rather than a buffer. Asking for more chars than will fit in the buffer is just asking for trouble.
The Undefeated wrote: Man, i like C# much better - it works
Yes, C# is a lot less complex than C++. What C# hides from you cannot be hidden by a language as powerful as C++.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
The Undefeated wrote: Man, i like C# much better - it works
Yeah, C++ does not work, it is well known in the industry for that. You can find all sorts of articles on the internet about how C++ is broken. None of the software that is written with it actually works either of course. Microsoft just runs all the C++ applications through the C++ to C# converter to get them to run. Sun runs them through the C++ to Java converter and all the Linux stuff is run through the C++ to Linux converter. There is talk of dropping the C++ standard in 2008 for adoption of the "Plain English" compiler.
led mike
|
|
|
|
|
Uh huh...
Anyways yeah, apart from the slight mistake that i put a 5 instead of a 4, it still doesn't work.
Well, if i try not to close the file it works but, it still doesn't make sense.
And aslo, i have a char array with 9 entire bytes in it, mmm, when i try write it, it so very kindly writes 16 bytes, err... huh?
Im so unhappy
|
|
|
|
|
How are you writing it ? Is it null terminated ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
It is now... but i din't have to do that 4 years ago when a made a lame text-based game with c++
All it was was a load of cin cout, oh yeah.
|
|
|
|
|
You need to study up on how null terminated strings work, it really is that simple... there is no magic. Alternatively you could go back to languages like C# where you don't have to know how null terminated strings work.
led mike
|
|
|
|
|
I already fixed it, y'know, how i said in the previous post how i've now added a null char to the end.
And we still havn't solved the mistery problem of why readfile.close() crashes the app
|
|
|
|
|
The Undefeated wrote: why readfile.close() crashes the app
Must be more in your app than we know about because this works perfect for me:
ifstream ReadFile("C:\\Research\\HeadersTest.txt");
char line[256];
memset(line, 0, sizeof(char)*256);
ReadFile.getline( line, sizeof(char)*255);
cout << "Line: " << line << endl;
ReadFile.close();
led mike
|
|
|
|
|
Hmmm, no not really, i took out all the other code to test this, must just be one o' thems things, like this one time, i tried to open a file, but the contents was always empty, so i copied the file and opened it, and it worked fine ??? figure that out! (Oh and i tried opening it in various apps, as well as moving the file to a different location, restarting the computer etc.)
|
|
|
|
|
The Undefeated wrote: figure that out!
You were running a "fat" system. I have not seen behavior like that since NT4 workstation and NTFS. I WIN!!
The Undefeated wrote: i took out all the other code to test this
Not what I would do. I would start a "new" project and put "only" that code in it by typing it into the Text Wizard, not by copy-paste.
led mike
|
|
|
|
|