|
I discovered the error(except for the errors you mentioned. "m_csFileName" is also a CString and CStdioFile requires a TCHAR evidently. When I changed it to TCHAR it wrote the file! Is there a way to copy a CString into a TCHAR? I really need to do that, because I build the path the User has chosen from a lot of Editboxes which I get using a CString; hence my need to be able to copy the gathered data in the CString into a TCHAR. Do you know how to do that?
A C++ programming language novice, but striving to learn
I made the changes you suggesed and I hardcoded the path and it created the file but it didn't write anything into the file even though "str" was loaded here:
void CSystemDB::FileSaveSys(CString str)
{
m_csFileName.Format(ID_CONVERTTOTCHAR,str);
// open a file
CStdioFile myFile;
CFileException ex;
if(!myFile.Open(pszFileName, CFile::modeNoTruncate | CFile::modeCreate | CFile::typeText, &ex))
{
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
AfxMessageBox(szError, MB_OK, 0);
return;
}
ULONGLONG len = myFile.GetLength();
DWORD dw = GetLastError();// error_path_not_found (3)
if(dw == 0 || dw == 183)
{
// File exists so setup to add to it with this batch:
//myFile.Seek( len +1, CFile::begin );
}
for(m_vSys.m_ITFileStr = m_vSys.m_vFileStr.begin() ; m_vSys.m_ITFileStr != vSys.m_vFileStr.end(); m_vSys.m_ITFileStr++)
{
// First get CSysData from vector:
str = *m_vSys.m_ITFileStr;
myFile.WriteString(str);// was loaded in Debug
}
myFile.Close();
the error returned is "Disk Full" yet nothing is written in the file! and I have over 65 GB of disk space. What gives?
<div class="ForumMod">modified on Sunday, May 4, 2008 10:12 AM</div>
|
|
|
|
|
A CString is a class which wraps an array of TCHARs, so no conversion is necessary.
You can pass a CString object anywhere a const TCHAR * (LPCTSTR) is required.
You should be able to debug this easily - it's not very many lines of code to step through.
I would guess your pathname was being built incorrectly the first time.
You still have a call to GetLastError() in there, which is wrong. GetLastError()
is only to be used after function calls that are documented to actually set the system
internal last error code. The way you are using GetLastError(), you have no idea what
function last set the error.
Step through this with the debugger ... you should easily be able to see what's
working and what's not
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Here's code that works:
// Open the file with the specified encoding pszFileName
FILE *fStream;
/* _T("w+") == _O_RDWR | _O_CREAT | _O_TRUNC*/
errno_t e = _tfopen_s(&fStream,pszFileName, _T("a+"));
if(e != 0)
{
AfxMessageBox((_T("Unable to open file")),MB_OK,0);
return;// failed
}
CStdioFile myFile(fStream); // open the file from this stream
ULONGLONG len = myFile.GetLength();
for(m_vSys.m_ITFileStr = m_vSys.m_vFileStr.begin() ; m_vSys.m_ITFileStr != m_vSys.m_vFileStr.end(); m_vSys.m_ITFileStr++)
{
// First get CSysData from vector:
str = *m_vSys.m_ITFileStr;
myFile.WriteString(str);
}
str = "";
myFile.Close();
}
Now I've tried your code and it fails on my machine. The above code works but I don't like it. When I try to use this code it doesn't work:
CStdioFile myFile;
CFileException ex;
if(!myFile.Open(pszFileName, CFile::modeNoTruncate | CFile::modeCreate | CFile::typeText, &ex))
{
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
AfxMessageBox(szError, MB_OK, 0);
return;
}
ULONGLONG len = myFile.GetLength();
for(m_vSys.m_ITFileStr = m_vSys.m_vFileStr.begin() ; m_vSys.m_ITFileStr != m_vSys.m_vFileStr.end(); m_vSys.m_ITFileStr++)
{
// First get CSysData from vector:
str = *m_vSys.m_ITFileStr;
myFile.WriteString(str);
}
str = "";
myFile.Close();
pszFileName is a TCHAR and m_csFileName is a CString. using m_csFileName I got an ASSESS violation error. Using pszFileName it created the file but didn't write anything in it. and gave an error of "Disk is Full error". both examples used the same path as Debug showed. The working example(1st one) also uses the same path with no errors. I have no idea why this is so.
A C++ programming language novice, but striving to learn
|
|
|
|
|
Larry Mills Sr wrote:
pszFileName is a TCHAR and m_csFileName is a CString
Again, there should be no difference here unless you initialize/set
the CString with a different string than your TCHAR pointer points to.
You're getting hung up on the string type when it's probably either the
pathname or the Open() flags that are bad.
What about starting simple. Create/open a file you know is going to succeed.
Use the CFile::Open() method flags properly, get the file to create and/or open
and then worry about writing to it.
Try this (works great on this end)....note that I've used one access permission (CFile::modeReadWrite)
and one share option (CFile::shareDenyWrite), as the docs state I should:
CString m_csFileName("e:\\testastdiofile.txt"); <code>
CStdioFile myFile;
CFileException ex;
if(!myFile.Open(m_csFileName, CFile::modeNoTruncate | CFile::modeCreate | CFile::modeReadWrite | CFile::shareDenyWrite | CFile::typeText, &ex))
{
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
AfxMessageBox(szError, MB_OK, 0);
return;
}
myFile.SeekToEnd(); <code>
myFile.WriteString(_T("A string\r\n"));
myFile.Close();
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Here's my code and the results:
void CSystemDB::FileSaveSys(CString str)
{
CString m_csFileName = ("c:\\Wells\\Program\\DataBases\\Operators\\WSF\\SystemDB.sdb"); //<--set this to an appropriate pathname!!
CStdioFile myFile;
CFileException ex;
if(!myFile.Open(m_csFileName, CFile::modeNoTruncate | CFile::modeCreate | CFile::modeReadWrite | CFile::shareDenyWrite | CFile::typeText, &ex))
{
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
AfxMessageBox(szError, MB_OK, 0);
return;
}
myFile.SeekToEnd(); //<-- do this only if you want to append to an existing file!
myFile.WriteString(_T("A string\r\n"));
myFile.Close();
This is the error I received:
CString m_csFileName = ("c:\\Wells\\Program\\DataBases\\Operators\\WSF\\SystemDB.sdb"); //<--set this to an appropriate pathname!!
error C2440: 'initializing' : cannot convert from 'const char [54]' to 'ATL::CStringT<BaseType,StringTraits>'
1> with
[
1> BaseType=wchar_t,
1> StringTraits=StrTraitMFC<wchar_t>
1> ]
1> Constructor for class 'ATL::CStringT<BaseType,StringTraits>' is declared 'explicit'
1> with
1> [
1> BaseType=wchar_t,
1> StringTraits=StrTraitMFC<wchar_t>
1> }
Do you think maybe the switches are set wrong on the compiler? I'm afraid to mess with the compiler settings; but obviously, there is something wrong somewhere.
Mark, I know you are a highly trained programmer, that's obvious from the code of yours I've seen. Something is wrong and I don't know how to fix it.
A C++ programming language novice, but striving to learn
If you want, you can email me directly at: larryamillssr@hughes.net
|
|
|
|
|
Larry Mills Sr wrote: This is the error I received:
CString m_csFileName = ("c:\\Wells\\Program\\DataBases\\Operators\\WSF\\SystemDB.sdb"); //<--set this to an appropriate pathname!!
error C2440: 'initializing' : cannot convert from 'const char [54]' to 'ATL::CStringT<BaseType,StringTraits>'
Shouldn't you be wrapping that string literal with _T() or prefixing it with L ?
"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
|
|
|
|
|
David, Let me explain how the path is arrived at. The User selects certain options in a dialog; and upns the path is predetermined, hence the usage of a CString. The path will only be the same is the User makes the same selections. How am I to know what the User wants before he makes an selection? I can't, so I must be prepared to use his selections for the path. Belie ve me it's complicated when you are building a path according to selections made by a User. As you can see I can't do the _T("%s") rutine everytime, in advance. The data being gathered is for different wells and their locations(paths). I no nothing about what you mentioned as "L" or how to do it.
According to Mark CStrings are suppose to be the same as TCHAR's, but my compiler doesn't know that yet!
the code Mark compiled and ran on his machine and he didn't use the _T("%s") or the "L" why won't it on mine? is the question and evidently the resolution.
Of course I further edited it with the _T("%s") and it compiled and ran correctly. But I can't hardcode every path in the program!
A C++ programming language novice, but striving to learn
|
|
|
|
|
Larry Mills Sr wrote: According to Mark CStrings are suppose to be the same as TCHAR's...
What he said was that any place a const TCHAR* is expected, a CString can be used because it has a LPCTSTR() operator.
Larry Mills Sr wrote: ...the code Mark compiled and ran on his machine and he didn't use the _T("%s") or the "L" why won't it on mine?
Maybe his (test) project did not have UNICODE defined.
In any case, if you'll get into the habit of creating Unicode-aware code (e.g., _T() , _tcscpy() , TCHAR ), your code will work the same whether UNICODE is defined or not.
"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
|
|
|
|
|
|
Larry Mills Sr wrote: the code Mark compiled and ran on his machine and he didn't use the _T("%s") or the "L" why won't it on mine?
Because you changed the code I used the CString constructor that takes a const char *
as an argument. In a Unicode build, the passed char string is converted to a TCHAR
string (which is wchar_t in a Unicode build).
You changed the code to use an assignment operator. There isn't an CString assignment
operator that takes a const char *, so the code failed to compile.
Larry Mills Sr wrote: But I can't hardcode every path in the program
Of course. But now that you know the file open works with a proper pathname string, you
can work on building a pathname string in a CString. It should be easy to debug - if the
file Open() fails, then you built a bad pathname string
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Larry Mills Sr wrote: CString m_csFileName = ("c:\\Wells\\Program\\DataBases\\Operators\\WSF\\SystemDB.sdb");
There shouldn't be an '=' sign there (at least there wasn't in my code sample).
If you want to use the assignment like that, then as David suggested, you'll
need to assign an appropriate string type, which is a TCHAR:
CString m_csFileName = _T("c:\\Wells\\Program\\DataBases\\Operators\\WSF\\SystemDB.sdb");
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Oh, it's always the little things. Sorry to have bothered you Mark. I'm so used to using the assignment operator that I failed to notice you didn't. Sorry, and thanks a lot!
A C++ programming language novice, but striving to learn
|
|
|
|
|
Larry Mills Sr wrote: Oh, it's always the little things
And there's so many little things!
Cheers,
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Another question Mark? Here's how I get the path:
CString CSystemDB::SetPath(CSysData cSysData)
{
CString str, str1, str2;
str1 = "c:\\Wells\\Program\\DataBases\\Operators\\";
// Make path for SystemDB.bku file for each Operator:
pszFileName = _T("c:\\Wells\\Program\\DataBases\\Operators\\WSF\\SystemDB.sdb");// hardcoded
pszFileName2 = _T("c:\\Wells\\Program\\DataBases\\SystemDB\\SystemDB.bku");//hardcoded
// Make path for SystemDB.sdb file for each Operator:
str2 = "c:\\Wells\\Program\\DataBases\\Operator\\";
str2 += cSysData.m_csOperatorName;
str2 += "\\";
str2 += "SystemDB.sdb";
m_csSysNameTMP = str2;
str = str1;
str += cSysData.m_csOperatorName;
str += "\\";
str += cSysData.m_csWellName;
str += "\\";
m_vSys.m_cSysData.m_csPath = str;// ============ Path for file lookup ===================
DoDirectory(m_vSys.m_cSysData);
// Now get the file filename:
cSysData.m_IsFileName = cSysData.m_csWellName;
cSysData.m_IsFileName += ".wdb";
m_csFileName = str;
FileSaveSys(str);// create file =================
return str;
}
You didn't use the assignment operator so how do I get "str" passed to FileSaveSys(str) into the global varable m_csFileName where it will work correctly?
A C++ programming language novice, but striving to learn
|
|
|
|
|
Mark, I don't know how to insert the string that holds the path into the main public CString m_csFileName(?); Here's how I get path CSysData cSysData is just a class for my vector that holds the data. Here's the path code:
Another question Mark? Here's how I get the path:
CString CSystemDB::SetPath(CSysData cSysData)
{
CString str, str1, str2;
str1 = "c:\\Wells\\Program\\DataBases\\Operators\\";
// Make path for SystemDB.bku file for each Operator:
pszFileName = _T("c:\\Wells\\Program\\DataBases\\Operators\\WSF\\SystemDB.sdb");// hardcoded
pszFileName2 = _T("c:\\Wells\\Program\\DataBases\\SystemDB\\SystemDB.bku");//hardcoded
// Make path for SystemDB.sdb file for each Operator:
str2 = "c:\\Wells\\Program\\DataBases\\Operator\\";
str2 += cSysData.m_csOperatorName;
str2 += "\\";
str2 += "SystemDB.sdb";
m_csSysNameTMP = str2;
str = str1;
str += cSysData.m_csOperatorName;
str += "\\";
str += cSysData.m_csWellName;
str += "\\";
m_vSys.m_cSysData.m_csPath = str;// ============ Path for file lookup ===================
DoDirectory(m_vSys.m_cSysData);
// Now get the file filename:
cSysData.m_IsFileName = cSysData.m_csWellName;
cSysData.m_IsFileName += ".wdb";
m_csFileName = str;
FileSaveSys(str);// create file =================
return str;
}
You didn't use the assignment operator so how do I get "str" passed to FileSaveSys(str) into the global varable m_csFileName where it will work correctly?
A C++ programming language novice, but striving to learn
|
|
|
|
|
I didn't use the assignment operator, but I didn't say there was
anything wrong with using it. You just need to assign a type that's
compatible with one of the assignment operators for the CString class.
You have a CString you're passing to FileSaveSys(), but you also already assign
it to a variable called m_csFileName in your SetPath() method.
I'm not sure why you pass it AND set a member variable...either way, you can use it
in FileSaveSys() however you want - use the passed string or the member variable string...
void CSystemDB::FileSaveSys(CString str)
{
CStdioFile myFile;
CFileException ex;
if(!myFile.Open(<code>m_csFileName</code>, CFile::modeNoTruncate | CFile::modeCreate | CFile::modeReadWrite | CFile::shareDenyWrite | CFile::typeText, &ex))
{
...
void CSystemDB::FileSaveSys(CString <code>str</code>)
{
CStdioFile myFile;
CFileException ex;
if(!myFile.Open(<code>str</code>, CFile::modeNoTruncate | CFile::modeCreate | CFile::modeReadWrite | CFile::shareDenyWrite | CFile::typeText, &ex))
{
...
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
This is why:
C:\Wells\Program\DataBases\Operator\WSF\SystemDB.sdb contains an incorrect path. (error message)
"C:\Wells\Program\DataBases\Operator\WSF\SystemDB.sdb" is what is in m_csFileName and the path is correct.
if I hardcode it,(exactly as above), in the constructor it does NOT produce this error. I've tried it so I know this is true.
A C++ programming language novice, but striving to learn
|
|
|
|
|
Mark, I want to appolize to you for taking up so much of your time. I finally found my mistake and it was as you said THE PATH WAS WRONG! It was writing all along but I diden't know it; because it was putting it somewhere else.
Sorry for using your time. I tried to look it up in debug but over looked it!
A C++ programming language novice, but striving to learn
|
|
|
|
|
Good deal! Glad you found it
Cheers,
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi all,
I want to know how can i use keyboards kyes like enter,move-down/up/left/right.
Please tell me how can use Enter key in Tree view.and how can select a tree item with the help of Enter key.
thanks in advance.
IN A DAY, WHEN YOU DON'T COME ACROSS ANY PROBLEMS - YOU CAN BE SURE THAT YOU ARE TRAVELLING IN A WRONG PATH
|
|
|
|
|
About the Keys, take a look into this message[^] and its answers. With the CTreeCtrl, sorry but I haven't used it yet. Have you checked in the articles? I remember seeing one about an extended tree control, maybe there is info you can use.
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
|
|
|
|
|
ho can replace a view on click of enter key?
IN A DAY, WHEN YOU DON'T COME ACROSS ANY PROBLEMS - YOU CAN BE SURE THAT YOU ARE TRAVELLING IN A WRONG PATH
|
|
|
|
|
I find that in my Visual C++ 2008 I can select between Debug and Release modes in two places:
A: at the top of the window just below the Help menu option.
B: reachable by clicking Build, Configuration Manager.
I have found that my C:\3d\makeobj_vc\... keeps two copies of its working: Debug and Release.
What happens with each of these four selections for A and B?:-
A and B both Debug
A and B both Release
A Debug, B Release
A Release, B Debug
modified on Saturday, May 3, 2008 7:23 AM
|
|
|
|
|
Didnt get you completly what you wanna say.
When i do changes from A than automatically B also changes.
It is not possible that if one changes than another is not change.
3) A Debug, B Release
4) A Release, B Debug
please check it again. I think you do changes in project context (configuration manager)not active solution configuration. If yes than when the both condition are different than project is not built and failure occur.
Yes U Can ...If U Can ,Dream it , U can do it ...ICAN
|
|
|
|
|
hi Everyone
i am doing project on GSM modem using AT commands in VC++ programming.
I am not getting how to send,read and receive a sms from GSM modem using AT commands.I am opening port and connecting modem.it is working.But i am not getting how the message shold me send to particular
persons in the list.whether i have to write a function for AT commands or wt.if it is so then tell me how can i do it.
Thanks,
Savitri
|
|
|
|
|