C++ help |
Author: |
Message: |
Millenium_edition
Veteran Member
![*](images/star.gif) ![*](images/star.gif) ![*](images/star.gif) ![*](images/star.gif) ![*](images/star.gif)
![Avatar](http://www.alcoholstift.be/faces_of_death/notrebel.gif)
Posts: 1787 Reputation: 57
Joined: Apr 2003
|
O.P. C++ help
Yesterday I realized my MSN servers app was widely used... So I decided to make a new version, with new features and stuff. But i wanted to do it in c++.
My problem: i get a bug, and I don't understand why
I wrote this piece of code to try to get the number of servers.
code: #include <windows.h>
#include <fstream.h>
int GetEntries(char* buffer, long int size);
int main(int argc, char* argv[])
{
fstream servers("msnservers.dat",ios::in | ios::binary | ios::ate);
long int fileSize;
fileSize = servers.tellg();
char* fileBuffer;
fileBuffer = new char[fileSize];
servers.seekg(0);
servers.read(fileBuffer,fileSize);
servers.close();
int entries=GetEntries(fileBuffer,fileSize);
MessageBox(0,reinterpret_cast<const char*>(entries),'\0',0);
delete[] fileBuffer;
return 0;
}
int GetEntries(char* buffer, long int Size) {
char tempchar;
int temp = 0;
for(int i = 0; i < Size; i++) {
tempchar=buffer[i];
MessageBox((HWND)0,reinterpret_cast<const char*>(tempchar),'\0',0);
}
return temp;
}
it gives me the ugly XP crash box. (I'm using ms vc++ 6 as a compiler, and I'm happy with it.)
Now, could someone please tell me what's wrong? ![:)](images/smilies/msn_happy.gif)
|
|
01-25-2005 01:55 PM |
|
![](images/pixel.gif) |
Concord Dawn
Veteran Member
![*](images/star.gif) ![*](images/star.gif) ![*](images/star.gif) ![*](images/star.gif) ![*](images/star.gif)
![Avatar](avatar.php?uid=29319-4682)
This is a loopy fruit.
Posts: 1203 Reputation: 16
34 / / –
Joined: Feb 2004
|
RE: C++ help
quote: Originally posted by Millenium_edition
Yesterday I realized my MSN servers app was widely used... So I decided to make a new version, with new features and stuff. But i wanted to do it in c++.
My problem: i get a bug, and I don't understand why
I wrote this piece of code to try to get the number of servers.
code: #include <windows.h>
#include <fstream.h>
int GetEntries(char* buffer, long int size);
int main(int argc, char* argv[])
{
fstream servers("msnservers.dat",ios::in | ios::binary | ios::ate);
long int fileSize;
fileSize = servers.tellg();
char* fileBuffer;
fileBuffer = new char[fileSize];
servers.seekg(0);
servers.read(fileBuffer,fileSize);
servers.close();
int entries=GetEntries(fileBuffer,fileSize);
MessageBox(0,reinterpret_cast<const char*>(entries),'\0',0);
delete[] fileBuffer;
return 0;
}
int GetEntries(char* buffer, long int Size) {
char tempchar;
int temp = 0;
for(int i = 0; i < Size; i++) {
tempchar=buffer[i];
MessageBox((HWND)0,reinterpret_cast<const char*>(tempchar),'\0',0);
}
return temp;
}
it gives me the ugly XP crash box. (I'm using ms vc++ 6 as a compiler, and I'm happy with it.)
Now, could someone please tell me what's wrong? ![:)](images/smilies/msn_happy.gif)
The compiler? I = total n00b in C++, but have you tried using a different compiler? I was doing a VB program in school once and I had to use a different compiler because VBStudio didn't work. Had to eventually get it reinstalled.
|
|
01-25-2005 01:59 PM |
|
![](images/pixel.gif) |
Millenium_edition
Veteran Member
![*](images/star.gif) ![*](images/star.gif) ![*](images/star.gif) ![*](images/star.gif) ![*](images/star.gif)
![Avatar](http://www.alcoholstift.be/faces_of_death/notrebel.gif)
Posts: 1787 Reputation: 57
Joined: Apr 2003
|
O.P. RE: RE: C++ help
tried the GNU c++ compiler, didn't work either.
nmv, found the bug. stupid messagebox call. >.<
This post was edited on 01-25-2005 at 03:48 PM by Millenium_edition.
|
|
01-25-2005 02:09 PM |
|
![](images/pixel.gif) |
RaceProUK
Elite Member
![*](images/star.gif) ![*](images/star.gif) ![*](images/star.gif) ![*](images/star.gif) ![*](images/star.gif)
![Avatar](http://rpsoftware.net/img/icon96.png)
Posts: 6073 Reputation: 57
39 / / ![United Kingdom Flag](images/flags/gb.png)
Joined: Oct 2003
|
RE: C++ help
quote: Originally posted by Millenium_edition
code: MessageBox((HWND)0,reinterpret_cast<const char*>(tempchar),'\0',0);
I'm surprised that compiled, sort of. Passing a char when you need a char* should be picked up by the compiler.
|
|
01-26-2005 09:38 AM |
|
![](images/pixel.gif) |
segosa
Community's Choice
![*](images/star.gif) ![*](images/star.gif) ![*](images/star.gif) ![*](images/star.gif) ![*](images/star.gif)
Posts: 1407 Reputation: 92
Joined: Feb 2003
|
RE: C++ help
code: char tmp[2];
tmp[0]=tempchar;
tmp[1]='\0';
MessageBox(NULL,tmp,NULL,NULL);
The previous sentence is false. The following sentence is true.
|
|
01-26-2005 06:35 PM |
|
![](images/pixel.gif) |
TheBlasphemer
Senior Member
![*](images/star.gif) ![*](images/star.gif) ![*](images/star.gif) ![*](images/star.gif)
![Avatar](images/avatars/removed/not_working.png)
Posts: 714 Reputation: 47
36 / – / –
Joined: Mar 2004
|
RE: C++ help
quote: Originally posted by raceprouk
quote: Originally posted by Millenium_edition
code: MessageBox((HWND)0,reinterpret_cast<const char*>(tempchar),'\0',0);
I'm surprised that compiled, sort of. Passing a char when you need a char* should be picked up by the compiler.
not if you tell it explicitly to reinterpret cast ![:P](images/smilies/msn_tongue.gif)
|
|
01-26-2005 06:59 PM |
|
![](images/pixel.gif) |
Millenium_edition
Veteran Member
![*](images/star.gif) ![*](images/star.gif) ![*](images/star.gif) ![*](images/star.gif) ![*](images/star.gif)
![Avatar](http://www.alcoholstift.be/faces_of_death/notrebel.gif)
Posts: 1787 Reputation: 57
Joined: Apr 2003
|
O.P. RE: RE: C++ help
quote: Originally posted by TheBlasphemer
quote: Originally posted by raceprouk
quote: Originally posted by Millenium_edition
code: MessageBox((HWND)0,reinterpret_cast<const char*>(tempchar),'\0',0);
I'm surprised that compiled, sort of. Passing a char when you need a char* should be picked up by the compiler.
not if you tell it explicitly to reinterpret cast ![:P](images/smilies/msn_tongue.gif)
I did that because it gave me an error if I didn't, but that's probably my fault
i realized that afterwards. but I'm a c++ noob atm
(thanks to everyone ![:)](images/smilies/msn_happy.gif) )
|
|
01-26-2005 07:39 PM |
|
![](images/pixel.gif) |
RaceProUK
Elite Member
![*](images/star.gif) ![*](images/star.gif) ![*](images/star.gif) ![*](images/star.gif) ![*](images/star.gif)
![Avatar](http://rpsoftware.net/img/icon96.png)
Posts: 6073 Reputation: 57
39 / / ![United Kingdom Flag](images/flags/gb.png)
Joined: Oct 2003
|
RE: C++ help
quote: Originally posted by TheBlasphemer
quote: Originally posted by raceprouk
quote: Originally posted by Millenium_edition
code: MessageBox((HWND)0,reinterpret_cast<const char*>(tempchar),'\0',0);
I'm surprised that compiled, sort of. Passing a char when you need a char* should be picked up by the compiler.
not if you tell it explicitly to reinterpret cast ![:P](images/smilies/msn_tongue.gif)
Wasn't looking at the cast: i was looking at the caption argument.
|
|
01-26-2005 09:06 PM |
|
![](images/pixel.gif) |
zaidgs
Full Member
![*](images/star.gif) ![*](images/star.gif) ![*](images/star.gif)
![Avatar](images/avatars/devil.gif)
Posts: 290
– / / –
Joined: Oct 2003
|
RE: C++ help
being a noob (i am an intermediate-level c++ programmer [or whtevr, i cannot find the correct expression, excuse my poor english ![:S](images/smilies/msn_confused.gif) ]) i suggest u keep away from reinterpret_cast as fire!!
use static_cast, or dynamic_cast instead
This post was edited on 01-28-2005 at 02:57 AM by zaidgs.
|
|
01-28-2005 02:50 AM |
|
![](images/pixel.gif) |
|