hmm... Now why did this work?
-
I wonder if someone can help me understand why, when I made changes to the following code worked (more like, gave me the result that I wanted) and if I did something wrong which can come back to haunt me later on (don't bash me :~ ) Original Code:
CFileDialog fOpenDlg(true,NULL,NULL,NULL,_T("All Files (*.*)|*.*||"),this);
CString fName = NULL;
string name;
fName = fOpenDlg.GetFileName();
name = fName;Error: error C2679: binary '=' : no operator found which takes a right-hand operand of type 'CString' (or there is no acceptable conversion) now the Modified Code (not the final one):
CFileDialog fOpenDlg(true,NULL,NULL,NULL,_T("All Files (*.*)|*.*||"),this);
CString fName = NULL;
string name;
fName = fOpenDlg.GetFileName();
name = *fName;Observation: if I open a file named test.txt fName gets set to test, and name gets set to t Final code (giving me the result I wanted, discovered accidentally while applying it elsewhere):
CFileDialog fOpenDlg(true,NULL,NULL,NULL,_T("All Files (*.*)|*.*||"),this);
CString fName = NULL;
string name;
fName = fOpenDlg.GetFileName();
name = (fName+"");Observation: now if I open a file named test.txt, fName gets set to test, and name ALSO gets set to test. I've been wondering how that +"" changed the behavior Thanks
-
I wonder if someone can help me understand why, when I made changes to the following code worked (more like, gave me the result that I wanted) and if I did something wrong which can come back to haunt me later on (don't bash me :~ ) Original Code:
CFileDialog fOpenDlg(true,NULL,NULL,NULL,_T("All Files (*.*)|*.*||"),this);
CString fName = NULL;
string name;
fName = fOpenDlg.GetFileName();
name = fName;Error: error C2679: binary '=' : no operator found which takes a right-hand operand of type 'CString' (or there is no acceptable conversion) now the Modified Code (not the final one):
CFileDialog fOpenDlg(true,NULL,NULL,NULL,_T("All Files (*.*)|*.*||"),this);
CString fName = NULL;
string name;
fName = fOpenDlg.GetFileName();
name = *fName;Observation: if I open a file named test.txt fName gets set to test, and name gets set to t Final code (giving me the result I wanted, discovered accidentally while applying it elsewhere):
CFileDialog fOpenDlg(true,NULL,NULL,NULL,_T("All Files (*.*)|*.*||"),this);
CString fName = NULL;
string name;
fName = fOpenDlg.GetFileName();
name = (fName+"");Observation: now if I open a file named test.txt, fName gets set to test, and name ALSO gets set to test. I've been wondering how that +"" changed the behavior Thanks
Are you doing a Unicode or an ANSI build?! This works for me, and I am doing a Unicode build.
std::wstring cppStr;
CFileDialog cfd(true);
if(cfd.DoModal()==IDOK) //I never see a cfd.DoModal() in your code! But you've called cfd.GetFileName(), which is pointless.
{
cppStr = cfd.GetFileName();
wcout<
I hope you can figure out your mistakes.Also, I don't understand why do you need to use std::string if you're already using MFC. Why not use
CString
?! :confused:It is a crappy thing, but it's life -^ Carlo Pallini
-
Are you doing a Unicode or an ANSI build?! This works for me, and I am doing a Unicode build.
std::wstring cppStr;
CFileDialog cfd(true);
if(cfd.DoModal()==IDOK) //I never see a cfd.DoModal() in your code! But you've called cfd.GetFileName(), which is pointless.
{
cppStr = cfd.GetFileName();
wcout<
I hope you can figure out your mistakes.Also, I don't understand why do you need to use std::string if you're already using MFC. Why not use
CString
?! :confused:It is a crappy thing, but it's life -^ Carlo Pallini
oh, i do do a "DoModal()==OK", I thought it didn't concern with the reason the strings behave (which I guess is what I wanted to know). and you are saying I don't need to use std::string with MFC eh? hmm... interesting point, I was basing that part of my code on basic- opening files, loading the data into buffer, copying the data into another file sort of operations. btw, on a new note, how does working with MFC change the need to use different types of variables (in this case, work with CStrings and not with CStrings and std::strings)? thanks for the patience mate p.s. oh there's another part of my code which isn't working (based on the examples I read on CFileDialog examples. I wanted to set the default directory to my C: drive when I click the Open button. my code for it is:
fOpenDlg.m_pOFN->lpstrInitialDir = (LPCWSTR)("C:");
which doesn't seem to be working... again im guessing on the way strings behave? (boy my string concepts are weak :(( )
-
oh, i do do a "DoModal()==OK", I thought it didn't concern with the reason the strings behave (which I guess is what I wanted to know). and you are saying I don't need to use std::string with MFC eh? hmm... interesting point, I was basing that part of my code on basic- opening files, loading the data into buffer, copying the data into another file sort of operations. btw, on a new note, how does working with MFC change the need to use different types of variables (in this case, work with CStrings and not with CStrings and std::strings)? thanks for the patience mate p.s. oh there's another part of my code which isn't working (based on the examples I read on CFileDialog examples. I wanted to set the default directory to my C: drive when I click the Open button. my code for it is:
fOpenDlg.m_pOFN->lpstrInitialDir = (LPCWSTR)("C:");
which doesn't seem to be working... again im guessing on the way strings behave? (boy my string concepts are weak :(( )
You can even write inline assembly to manipulate everything. But the question is: Do you really need to do it, given that you're already using a framework which makes things much more simpler? I am saying that you should not be using std::string in an MFC program. But given that CString does everything that you need to, I simply fail to see any justification for using std::string. (OK, you're manipulating files? Then you could use CFile or CStdioFile or ...). And why do I use MFC when I can do everything with vanilla C or C++? Because I'd like to leave for home at 5. I may use something like an std::string within an MFC program very rarely (may be I'm using some third party code and a function expects a reference to an std::string, or whatever)...
sadman89 wrote:
drive when I click the Open button. my code for it is:
You are again providing code in bits and pieces. That works for me and I can't tell you why it didn't work in your case unless I can see the whole of your code (relevant code, please). See if this helps:
CFileDialog cfd(true);
CString szStr;
cfd.m_pOFN->lpstrInitialDir = _T("C:\\");
if(cfd.DoModal == IDOK)
{
szStr = cfd.GetFileName();
AfxMessageBox(szStr);
}It is a crappy thing, but it's life -^ Carlo Pallini
-
You can even write inline assembly to manipulate everything. But the question is: Do you really need to do it, given that you're already using a framework which makes things much more simpler? I am saying that you should not be using std::string in an MFC program. But given that CString does everything that you need to, I simply fail to see any justification for using std::string. (OK, you're manipulating files? Then you could use CFile or CStdioFile or ...). And why do I use MFC when I can do everything with vanilla C or C++? Because I'd like to leave for home at 5. I may use something like an std::string within an MFC program very rarely (may be I'm using some third party code and a function expects a reference to an std::string, or whatever)...
sadman89 wrote:
drive when I click the Open button. my code for it is:
You are again providing code in bits and pieces. That works for me and I can't tell you why it didn't work in your case unless I can see the whole of your code (relevant code, please). See if this helps:
CFileDialog cfd(true);
CString szStr;
cfd.m_pOFN->lpstrInitialDir = _T("C:\\");
if(cfd.DoModal == IDOK)
{
szStr = cfd.GetFileName();
AfxMessageBox(szStr);
}It is a crappy thing, but it's life -^ Carlo Pallini
ok so i get your point with using CString over std::string in MFCs, however I had to do some operations like name.append(".txt" which don't seem to be working if I declare name as a CString variable. and that m_pOFN->lpstrInitialDir = _T("C:\\"); also doesn't seem to be working. ok so here's ALL (I hope) relavent code related to that function:
::OnBnClickedBrowse() //When you click the open button
{
CFileDialog fOpenDlg(true,NULL,NULL,NULL,_T("All Files (*.*)|*.*||"),this);
fOpenDlg.m_pOFN->lpstrTitle= _T("Open File");
fOpenDlg.m_pOFN->lpstrInitialDir = _T("C:\\");
CString fPath = NULL;
CString fName = NULL;
if(fOpenDlg.DoModal() ==IDOK)
{
fPath = fOpenDlg.GetPathName();//path of current file
fName = fOpenDlg.GetFileName();
}thats basically all the code concerning lpstrInitialDir.
-
ok so i get your point with using CString over std::string in MFCs, however I had to do some operations like name.append(".txt" which don't seem to be working if I declare name as a CString variable. and that m_pOFN->lpstrInitialDir = _T("C:\\"); also doesn't seem to be working. ok so here's ALL (I hope) relavent code related to that function:
::OnBnClickedBrowse() //When you click the open button
{
CFileDialog fOpenDlg(true,NULL,NULL,NULL,_T("All Files (*.*)|*.*||"),this);
fOpenDlg.m_pOFN->lpstrTitle= _T("Open File");
fOpenDlg.m_pOFN->lpstrInitialDir = _T("C:\\");
CString fPath = NULL;
CString fName = NULL;
if(fOpenDlg.DoModal() ==IDOK)
{
fPath = fOpenDlg.GetPathName();//path of current file
fName = fOpenDlg.GetFileName();
}thats basically all the code concerning lpstrInitialDir.
sadman89 wrote:
however I had to do some operations like name.append(".txt" which don't seem to be working if I declare name as a CString variable.
Append works perfectly. You are most probably doing something wrong. (again, unless I see your code...) You might as well want to look at the
CString::operator+
, just in case you didn't know, you could do this:CString szStr(_T("Hello, ")); szStr += _T("world!");
sadman89 wrote:
CString fPath = NULL; CString fName = NULL;
This kind of initialization is not really needed. Your code works perfectly on my machine. Setting of the initial directory and the title of the open dialog seem to have no problem at all. I don't see what could have gone wrong. I'm assuming you have *do have* a C drive. :-D
It is a crappy thing, but it's life -^ Carlo Pallini
-
sadman89 wrote:
however I had to do some operations like name.append(".txt" which don't seem to be working if I declare name as a CString variable.
Append works perfectly. You are most probably doing something wrong. (again, unless I see your code...) You might as well want to look at the
CString::operator+
, just in case you didn't know, you could do this:CString szStr(_T("Hello, ")); szStr += _T("world!");
sadman89 wrote:
CString fPath = NULL; CString fName = NULL;
This kind of initialization is not really needed. Your code works perfectly on my machine. Setting of the initial directory and the title of the open dialog seem to have no problem at all. I don't see what could have gone wrong. I'm assuming you have *do have* a C drive. :-D
It is a crappy thing, but it's life -^ Carlo Pallini
weird :confused: yeah i googled the .append and also the C: drive thingy, works for other ppl. bah, so i won't be able to open the C drive default... noone's gonna die lol. thanks for the help... now on to figure out bigger bugs X|
-
I wonder if someone can help me understand why, when I made changes to the following code worked (more like, gave me the result that I wanted) and if I did something wrong which can come back to haunt me later on (don't bash me :~ ) Original Code:
CFileDialog fOpenDlg(true,NULL,NULL,NULL,_T("All Files (*.*)|*.*||"),this);
CString fName = NULL;
string name;
fName = fOpenDlg.GetFileName();
name = fName;Error: error C2679: binary '=' : no operator found which takes a right-hand operand of type 'CString' (or there is no acceptable conversion) now the Modified Code (not the final one):
CFileDialog fOpenDlg(true,NULL,NULL,NULL,_T("All Files (*.*)|*.*||"),this);
CString fName = NULL;
string name;
fName = fOpenDlg.GetFileName();
name = *fName;Observation: if I open a file named test.txt fName gets set to test, and name gets set to t Final code (giving me the result I wanted, discovered accidentally while applying it elsewhere):
CFileDialog fOpenDlg(true,NULL,NULL,NULL,_T("All Files (*.*)|*.*||"),this);
CString fName = NULL;
string name;
fName = fOpenDlg.GetFileName();
name = (fName+"");Observation: now if I open a file named test.txt, fName gets set to test, and name ALSO gets set to test. I've been wondering how that +"" changed the behavior Thanks
Until you call
fOpenDlg.DoModal()
, your code is pointless. That aside...sadman89 wrote:
CString fName = NULL;
Why are you assigning
NULL
to a non-pointer? Not sure how this even compiles.sadman89 wrote:
name = *fName;
This makes no sense.
sadman89 wrote:
name = (fName+"");
Nor does this.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"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