|
While it (probably) has nothing to do with your problem, I'd offer:
1) Don't post commented-out code. It just makes that much more for us to have to read/ignore.
2) Since you are using MFC, why not take advantage of CStdioFile , AfxMessagBox() , and CFileFind ?
That said, do the first 2-4 "columns" in your input file contain more than 14 characters? If so, buffer will not hold them all.
If there are more than 2000 files in the folder pointed to by DirSpec , you'll have obvious trouble.
Your very last for() loop is using ii and II . Is that intentional?
mrby123 wrote: DirSpec = (LPTSTR) malloc (BUFSIZE);
DirSpec=TEXT("*.txt");
The address assigned to DirSpec (from malloc() ) has been changed, and a subsequent call to free() would fail.
Consider:
CStringArray fileNames;
CFileFind fileFind;
BOOL bFound = fileFind.FindFile("*.txt");
while (bFound)
{
bFound = fileFind.FindNextFile();
fileNames.Add(fileFind.GetFilePath());
}
fileFind.Close();
for (int ii = 0; ii < fileNames.GetSize(); ii++)
{
CString fileName = fileNames.GetAt(ii);
CStdioFile fileIn;
if (fileIn.Open(fileName, CFile::modeRead))
{
CString line;
fileIn.ReadString(line);
fileIn.Close();
}
}
CStdioFile fileOut;
if (fileOut.Open(m_outputFileName, CFile::modeWrite))
{
CString str;
str.Format("%d\n", fileNames.GetSize());
fileOut.WriteString(str);
fileOut.WriteString(str);
fileOut.WriteString("fineName Easting(m) Northing(m) elv(m)\n");
for (int ii = 0; ii < fileNames.GetSize(); ii++)
{
CString fileName = fileNames.GetAt(ii);
str.Format("%s %f %f %f\n", fileName, east[ii], north[ii], elv[ii]);
fileOut.WriteString(str);
}
fileOut.Close();
}
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
DavidCrow wrote: If so, buffer will not hold them all.
And worse, the data may spill and overwrite whatever is adjacent to buffer, causing
unpredictable errors. Overwriting a pointer is likely to result in "Access violation".
The present code is unsafe.
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
David,
You solve my problem. You are right that I have a test which is longer than 15 characters in the data file.
Thanks alots.
Ruilin
|
|
|
|
|
You could have possibly found the problem sooner by using the debugger to step over each of the calls to fscanf() and watch the value of f_ptr2 . I suspect it was changed by the time fclose() was called.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
DavidCrow wrote: You could have possibly found the problem sooner by using the debugger
Surely YOU jest
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
No really, I'm dead serious. Science has proven that the debugger really does add years to your life, make you look taller, help you find problems quicker.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I think this statement might have the problem.
fscanf( f_ptr2,"%s %s %s\n",buffer,buffer,fname[ii].GetBuffer(MAX_PATH));
Here one of the pointer is taken using GetBuffer of CString. I think it is not the correct way of geting the pointer of CString memory and copying value in it like character array. Taking the value in character array and then assigning it to CString might be better option. Something like this.
char sTemp[500];
fscanf( f_ptr2,"%s %s %s\n",buffer,buffer, sTemp);
fname[ii] = sTemp;
|
|
|
|
|
Sunil Shindekar wrote: I think this statement might have the problem.
fscanf( f_ptr2,"%s %s %s\n",buffer,buffer,fname[ii].GetBuffer(MAX_PATH));
While it's awkward looking, there is nothing wrong with it.
Sunil Shindekar wrote: Taking the value in character array and then assigning it to CString might be better option.
Different? Yes. Better? No.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
GetBuffer returns the pointer to the memory which is enough to store the current string assigned to the CString object. You are using the same pointer to read the data from the file. If the data is too large to store in the currently allocated memory for the pointer by the CString, then there will be memory overrun. It can cause overwriting the values of other memory locations which may or may not include file pointer also or FILE structure also.
|
|
|
|
|
Sunil Shindekar wrote: If the data is too large to store in the currently allocated memory for the pointer by the CString, then there will be memory overrun. It can cause overwriting the values of other memory locations which may or may not include file pointer also or FILE structure also.
Aand how is your suggestion of using char sTemp[500] any better?
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I have the following code to read data from a list of files. The code read the first file and read the data correctly. I used break point debug and found at "fclose(f_ptr2)" giving error: "Access Violation or memery can not be read"
//
FILE *f_ptr2;
//
//
for (int ii=0;ii {
//
f_ptr2 = fopen(fileName[ii],"r");
//
fscanf( f_ptr2,"%s %s %s\n",buffer,buffer,fname[ii].GetBuffer(MAX_PATH)); fname[ii].ReleaseBuffer();
fscanf( f_ptr2,"%s %s %f\n",buffer,buffer, &east[ii]); //easting
fscanf( f_ptr2,"%s %s %f\n",buffer,buffer,&north[ii]); //northing
fclose(f_ptr2);
}
//
Please help and thanks
|
|
|
|
|
You asked this yesterday, and you still haven't fixed your code so we can read it all.
Is f_ptr2 valid? I don't see any check for NULL.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
In fact, I have:
if((f_ptr2 = fopen(fileName[ii],"r")) == NULL) {
MessageBox("output file open problems !");
}
It seems that the fopen has no problem.
Thanks
|
|
|
|
|
What are buffer , fname , east , and north ?
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
They are:
const int MAX=15;
float north[2000];
float east[2000];
float elv[2000];
CString fname[2000];
char buffer[MAX];
buffer is used to read data as dumy. The data never exceeds 15 characters. Thanks
In fact, the following code works fine and print all files of *.txt without an error message.
----------------------------------------------------
void CBlast_vib_procDlg::Next()
{
FILE *f_ptr1;
FILE *f_ptr2;
const int MAX=16;
CBlast_vib_procDlg rr;
//
float north[2000];
float east[2000];
float elv[2000];
CString fname[2000];
//
char buffer[MAX];
// TODO: Add your control notification handler code here
UpdateData();
if(m_outputFileName == "" )
{
MessageBox("All file names have to be typed in !");
rr.m_outputFileName = m_outputFileName;
rr.DoModal();
UpdateData(false);
}
#define BUFSIZE MAX_PATH
WIN32_FIND_DATA FindFileData;
LPTSTR DirSpec;
//
HANDLE hFind = INVALID_HANDLE_VALUE;
CString fileName[2000];
int i=0;
DirSpec = (LPTSTR) malloc (BUFSIZE);
DirSpec=TEXT("*.txt");
//
hFind = FindFirstFile(DirSpec, &FindFileData);
fileName[0]=FindFileData.cFileName;
//
while (FindNextFile(hFind, &FindFileData) != 0)
{
i+=1;
fileName[i]=FindFileData.cFileName;
}
int nfile=i;
FindClose(hFind);
// open output data file
if((f_ptr1 = fopen(m_outputFileName,"w")) == NULL) {
MessageBox("output file open problems !");
rr.m_outputFileName = m_outputFileName;
rr.DoModal();
UpdateData(false);
}
//
fprintf (f_ptr1, "%i\n", nfile);
for (int ii=0;ii
|
|
|
|
|
Greetings!
I am looking for a Gantt chart control that can be used inside an ActiveX control developed in VB6 or VC++ 6. The control must allow me to draw tasks myself, and it must support drag-and-drop operations.
Here's the background: My company supplies annealing furnaces. A user stacks steel coils and drops a cover on them. Then they drop a furnace over the cover and heat the coils for the better part of the day. Then they take the furnace off and put a cooler on and cool the coils for several more hours. An annealing plant has several bases, furnaces and cooling covers, but fewer furnaces and covers than bases. Each base will be a resource on the chart. Each charge (stack of coils) will be a task. One color will be used for heating, and another for cooling. The caption for each task will include the charge number, furnace number and cooler number.
I have been working with the ActiveGanttVC control from SourceCodeStore.com, which is based in Bogota, Colombia. This control has a very nice API, including a set of events that I can handle to do my own drawing. The only problem is that despite several E-mails, I have never received a reply from this company. The US 800 number given on its "Contact Us" page belongs to a cruise line, completely mystifying some poor operator who wondered why I called them asking about an ActiveX control. I am really uncomfortable using this company's product. Can somebody recommend one where I might have some prayer of reaching a live person when I need to?
Thanks very much!
Rob Richardson
|
|
|
|
|
Hello everyone,
Here is the code, and if I change line from
static wchar_t* p[1] = {PREFIX((wchar_t*)_TEXT("FOO"))};
to
static wchar_t* p[1] = {PREFIX(_TEXT("FOO"))};
then code will be ok. Why?
<br />
#define UNICODE<br />
#define _UNICODE<br />
<br />
<br />
#define PREFIX(x) (wchar_t*)_TEXT("GOO_") x<br />
<br />
int main (int argc, char** argv)<br />
{<br />
static wchar_t* p[1] = {PREFIX((wchar_t*)_TEXT("FOO"))};<br />
<br />
return 0;<br />
}<br />
<br />
<br />
<br />
main.c(10) : error C2064: term does not evaluate to a function taking -22 arguments<br />
main.c(10) : error C2143: syntax error : missing ')' before 'type'<br />
main.c(10) : error C2059: syntax error : ')'<br />
thanks in advance,
George
|
|
|
|
|
because, in the first case the expression expands to
static wchar_t* p[1] = {(wchar_t*)L"GOO_" (wchar_t*)L"FOO"};
i.e. error.
while in the second one to:
static wchar_t* p[1] = {(wchar_t*)L"GOO_" L"FOO"};
that turns out to be correct.
A suggestion:
Use the compiler /E option to see the preprocessor output.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Thanks CPallini,
I have also used /E option to generate the preprocessor file and it looks the same as you mentioned below.
But from C/C++ grammar point of view, I think the two statements should be the same, right?
regards,
George
|
|
|
|
|
Let's recall the second one (the correct one):
static wchar_t* p[1] = {(wchar_t*)L"GOO_" L"FOO"};
int the statement above, you have two wide character literals separated off by a blank. The compiler removes the blank and merges the literals, i.e.
static wchar_t* p[1] = {(wchar_t*)L"GOO_FOO"};
that is a valid statement.
On the other hand, in the expression:
static wchar_t* p[1] = {(wchar_t*)L"GOO_" (wchar_t*)L"FOO"};
the cast operator (wchar_t*) precludes such merging of literals and the resulting statement is invalid.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Thanks CPallini,
I have tried the statement does not work, but what makes me confused is, even if I add wchar_t* type conversion to string literal L"FOO", it is still a string literal type and in this statement, I simply use space character to separate two string literals, and the expected result should be the concatenation of them.
Could you provide more information about why this statement does not work in C/C++ compiler please?
static wchar_t* p[1] = {(wchar_t*)L"GOO_" (wchar_t*)L"FOO"};
regards,
George
|
|
|
|
|
George_George wrote: (wchar_t*)L"FOO"
Because the above is a cast operator plus a string literal , not a string literal alone.
BTW you don't need the cast operator at all:
#define PREFIX(x) _TEXT("GOO_") x
int main (int argc, char** argv)
{
static wchar_t* p[1] = {PREFIX(_TEXT("FOO"))};
return 0;
}
compiles fine.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Thanks CPallini,
My question is answered.
regards,
George
|
|
|
|
|
Then please put [solved] in the start message So future people looking for something similiar can quickly know that there is a solution in this thread.
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
|
|
|
|
|
Hi Experts,
I have written a function that display the Html Content in HtmlView, this code is working fine on all the OS except Vista .This is working for some strings but does not work for some other string.And gives a unhandled exception.
I describe with code where the function throw exception.
void CDisplayBody::NavigateText(CString pszText)<br />
{<br />
try<br />
{<br />
if (!m_bHasDocument)
{<br />
Navigate(_T("about:blank"));
return;<br />
}<br />
<br />
<br />
if (GetBusy())
Stop(); <br />
<br />
IDispatch * pDisp = GetHtmlDocument();<br />
if (!pDisp)<br />
return;<br />
<br />
IHTMLDocument2* pDoc;<br />
if (SUCCEEDED(pDisp->QueryInterface( IID_IHTMLDocument2, (void**)&pDoc )))<br />
{<br />
CComBSTR bstrURL;<br />
CComVariant varDummy;<br />
pDoc->open(bstrURL, varDummy, varDummy, varDummy, NULL);<br />
<br />
SAFEARRAY *pSA;<br />
SAFEARRAYBOUND saBound = {1, 0};<br />
pSA = SafeArrayCreate(VT_VARIANT, 1, &saBound);<br />
<br />
VARIANT *pVar;<br />
CComBSTR bstrHTML = pszText;
varDummy = bstrHTML;
<br />
SafeArrayAccessData(pSA, (void**)&pVar);
pVar[0] = varDummy;
SafeArrayUnaccessData(pSA);
<br />
pDoc->write(pSA);
pDoc->close();
<br />
SafeArrayDestroy(pSA);
pDoc->Release();
}<br />
<br />
<br />
pDisp->Release();<br />
pDisp=NULL;<br />
}<br />
catch (...)<br />
{<br />
AfxMessageBox(_T("CDisplayBody::NavigateText"));<br />
}<br />
}
As the fuction comes out the last if statement scope , it throw a exception.
Could you help me to resolve it or handle this error.
|
|
|
|
|