|
1/
new char [file.GetLength()];
2/
You are making a buffer exactly as long as the file - and as it's char buffer, you're likely to be expecting it to be NULL terminated. Which it won't be...
Your second example reads *upto* 100 chars, so will probably be less, and as that chunk of 100 bytes probably has a zero or two in it, you get away with it.
Make a text file 100 bytes long, and it would likely fail too.
3/
I'd put the size into a variable, if only for debugging purposes.
4/
You could also have some naughty person save a '\0' somewhere in the file, so your string would be messed up... Especially if this program ends up in the wild. The effect could be benign: "Ok, it will look like there was less text. big deal", or critical, depending on your code.
Iain.
Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.
|
|
|
|
|
as long as you're using MFC, why don't you use CString instead of char* ?
|
|
|
|
|
Use the rant icon.
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|
nope, i wasn't ranting, i was just advising, on an interrogative way...
|
|
|
|
|
Actually yours was a good suggestion and so, could be a rant.
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|
iayd wrote: char *pbuf = new char(file.GetLength())
file.Read( pbuf,file.GetLength());
In this way occured an error
What error? A missing semicolon, perhaps? Did new fail? Did GetLength() fail? Did Read() fail? Be specific.
"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
|
|
|
|
|
Hello all,
I have taken static text which work as hyperlink. It is working properly but I want to destroy dialog box on which i have put this static text. I am setting URL at run time when user click on it. so just I want to download specific file from URL and at the same time to unload dialog box. How couls i do this?
Any help is greatly appreciated.
Regards,
Hemang
|
|
|
|
|
You've got a bunch of ways to do this.
Have a look at InternetOpenUrl, InternetReadFile, etc for getting the actual file.
There are almost certainly articles in the internet section here on codeproject that do a better job too.
How you implement it is up to you, but getting files over an unreliable connection is always hard. What if the user gave you a bad URL? Malformed?
If you don't need immediate feedback, you could put the reading code into a thread, so you don't make the user wait a random time, and so on.
Lots of different things for you to think about, and no trivial solutions, as the answers will depend heavily on the details of your application.
Iain.
Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.
|
|
|
|
|
I think I hadnt explained my problem exactly. I got Internet stuff almost done. Actually i want to unload particular dialog box when user click on Link text as I normally do with OK button. so how could i do this?
Thanks,
Hemang
|
|
|
|
|
You'll need the SS_NOTIFY style on the static control to know when the use clicks on it.
Then, in a handler for that click message, you can call EndDialog (SOMECODENUMBER); to finish the dialog. The DoModal () function will return the SOMECODENUMBER to any variable paying attention.
In Win32, it's the same, excepy enddialog wants a window handle.
Iain.
Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.
|
|
|
|
|
Since the dialog class is likely derived from CDialog , why not just call EndDialog() like what happens when OK and Cancel are clicked?
"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 have referred to following article for my purpose.
http://www.codeproject.com/KB/miscctrl/hyperlink.aspx[^]
If you will have look at it then you can easily understand my problem. In this example there is one dialog named "HyperLink Sample". On this dialog there is static text named "mail me" and Ok button is there.
This static text is associated with CHyperLink class.
I want same functionality but just want to destroy "HyperLink Sample" dialog when someone clicks on it as well as it should work as HyperLink.
|
|
|
|
|
Hi,
I have a ListView control with Report style with 4 colomn. I want to show tooltip for different row as mouse moves over there. How can I do this?
|
|
|
|
|
Have a look at MSDN magazine June 2001, and the article by Paul DiLascia. He presented a friendly tooltip, which you can integrate and give text to, depending on the position of the mouse.
I use it in a window class I wrote to present the X,Y and value of various parts of the window.
Iain.
ps, You wouldn't also be called George George, would you?
Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.
|
|
|
|
|
Could you give the link please?
|
|
|
|
|
Nope! Far too much like hard work for me to do, when it's you who needs the answer.
goooooooooogle.
(I think www.msdnmag.com is rerouted to the appropriate place)
Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.
|
|
|
|
|
|
I've created function as follows
template <class t="">
void BubbleSort(int nArraySize)
{
T* pArray = new T[nArraySize];
int nIdx;
cout<<"Enter the items to be sorted"<<endl;
for ( nIdx = 0;nIdx < nArraySize;nIdx++)
{
cin>>pArray[nIdx];
}
....
}
</class>
and called this function as follows
int main()
{
int x;
cin >> x;
switch( x )
{
case 1:
BubbleSort<float>( 3 );
break;
case 2:
BubbleSort<int> ( 3 );
default:
break;
}
}</int></float>
This code works fine with Visual C++ 2008 and Dev C++ compiler.
cin>>Array[nIdx]; breaks when I compile and execute the code using Visual C++ 6.0
Is there any work around for this problem? or something missing with my usage?
-Sarath.
"Great hopes make everything great possible" - Benjamin Franklin
|
|
|
|
|
I can guess what you mean by this:
Sarath. wrote: template
void BubbleSort(int nArraySize)
but it would be nice if you took a little care to ensure your post is readable in its entirety.
Also, did you forget break here?
Sarath. wrote: case 2:
BubbleSort ( 3 );
default:
Sarath. wrote: cin>>Array[nIdx]; breaks when I compile and execute the code using Visual C++ 6.0
What do you mean by "breaks"? Does it compile? If not what's the error message? If it compiles in what way is it not working as you expect?
Steve
|
|
|
|
|
I'm really sorry for not formatting the code well.
Ok let me talk more about this issue
Let's take simple sample
template <class T>
void Foo(int nArraySize)
{
T* pArray = new T[nArraySize];
int nIdx;
cout<<"Enter the items"<<endl;
for ( nIdx = 0;nIdx < nArraySize;nIdx++)
{
cin>>pArray[nIdx];
}
cout<"The array contains"<<endl;
for ( nIdx = 0; nIdx < nArraySize;nIdx++)
{
cout<<pArray[nIdx]<<endl;
}
}
int main()
{
int x;
cin >> x;
switch( x )
{
case 1:
Foo<float >( 3 );
break;
case 2:
Foo<int> ( 3 );
break;
default:
break;
}
} ;
The program exits prematurely when we try to enter with float option
Foo<flo> has been executed
The output was as follows
1
Enter the items
2.3
The array contains
2
-842150451
-842150451
Just after entering the first value, the execution has break from the for loop. (the behavior we see when we try to read an int and entering invalid characters. I dont know the exact word for that).
In my understanding, cin deducing the type of data as the last type used to invoke this function. in this case Foo<int>()
For further analysis, I tried to analyze using char as template type
case 3:
Foo<char> ( 3 );
break;
and I executed float option (not the char version of template function)
the output was as follows
1<br />
Enter the items<br />
123.23<br />
The array contains<br />
1<br />
2<br />
3
The above are the ouput of VC6. (note that the for loop was not completely executed in this case. it has exited after entering the first element
Is it clear now?
-Sarath.
"Great hopes make everything great possible" - Benjamin Franklin
|
|
|
|
|
VC6 compiler is notorious for not being C++ standards complient. If you see it call _Myt& operator>>(int& _X) version instead of _Myt& operator>>(double& _X) version of function.
Which Microsoft fixed in later verions of compiler.
|
|
|
|
|
The following call stack shows what's going on:
std::basic_istream<char,std::char_traits<char> >::operator>>(int & 0xcdcdcdcd) line 103
Foo(int 0x00000003) line 17
main() line 34 + 7 bytes
mainCRTStartup() line 206 + 25 bytes
KERNEL32! 7c816fd7()
As you can see operator>>(int& _X) is being called instead of correct operator for a double . This results in the stream going "bad" when it encounters the period. The program works as expected in VS2008 but not in MSVC6.
Steve
|
|
|
|
|
I am trying to convert an int to string and get an error.
I have the following...
string str="1";
int val;
val=atoi(str.c_str());
val++;
itoa(val,str,10);
On doing this I get the following error. Were could I have gone wrong?
error C2664: 'itoa' : cannot convert parameter 2 from 'class std::basic_string<char,struct std::char_traits<char="">,class std::allocator<char> >' to 'char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
</char></char,struct>
THANKS.
|
|
|
|
|
string str="1";
int val;
val=atoi(str.c_str());
val++;
itoa(val,(char*)str.c_str(),10);
The code would serve your purpose but not be the optimized way of doing this.
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
_AnShUmAn_ wrote: (char*)str.c_str(
This is a bad idea. The return type is const char * and casting away the const is wrong and the compiler is correct in complaining.
Steve
|
|
|
|