|
If the ThreadProc is declared as follows:
DWORD WINAPI ThreadProc(
LPVOID lpParameter // thread data
);
Why don't you just
return 0;
from your thread function instead of calling ExitThread at the every end of it anyway? Then your variables can go out of scope and destroy themselves automatically. Anything monitoring the 'exit code' of the thread will still see the 0.
FYI (From MSDN):
A thread that uses functions from the C run-time libraries should use the _beginthread and _endthread C run-time functions for thread management rather than CreateThread and ExitThread. Failure to do so results in small memory leaks when ExitThread is called.
|
|
|
|
|
fstream file;
char buffer[MAXBUFSIZE];
string line;
file.open(..., ios_base::in);
file.getline(buffer, MAXBUFSIZE-1);
//getline(file, line);
//both of the above fail to work
//and the vc.net2003 print such error info:
//error C2663: “std::basic_istream<_Elem,_Traits>::getline” : //2 //overloaded functions lack a legal conversion of pointer "this".
|
|
|
|
|
This works fine:
--------------------------------------
#include <iostream>
#include <fstream>
using namespace std;
void main()
{
fstream file;
const int MAXBUFSIZE = 256;
char buffer[MAXBUFSIZE];
file.open("a.txt", ios_base::in);
do
{
file.getline(buffer, MAXBUFSIZE-1);
//getline(file, line);
string s = buffer;
cout << s.c_str() << endl;
} while (! file.eof() );
}
<b>Maxwell Chen</b>
|
|
|
|
|
Might I suggest something more like this:
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::ifstream file("filename");
std::string line;
while (std::getline(file, line)) {
std::cout << line << '\n';
}
return 0;
}
Just a couple of points:
- C++ defines main as: "int main()" or "int main(int argc, char* argv[])". There's nothing in the code that limits us to Visual C++ and its non-standard "void main()" extension.
- Using std::getline (defined in "string") we avoid using a char* buffer as we have to with std::basic_istream<...>::getline.
- Invoking s.c_str() on something we write out using iostreams anyway might incur the following overhead: a char* buffer that can contain the contents is allocated, the contents of s is copied over into it, a terminating null character is added and this buffer is returned. This is because the contents of std::string isn't guaranteed to be null terminated.
Hope this helps.
--
Henrik Stuart (http://www.unprompted.com/hstuart/[^])
|
|
|
|
|
thank you for the response.
Finnally, I figured out the key problem.
the fstream object is the data member of my
class not declared mutable, but in the member
function declared const, the fstream.getline()
failed.
//ps: the error information vc.net2003 offers me
//just confuses me
|
|
|
|
|
Henrik, you got my 5!
Basically I agree with all of your view points, and they are quite true. It reminds me to pay efforts in studying the Standard Library.
Regarding to the prototype of main, you are right too, as we don't find any 'void main()' in Stroustrup's book. It's just my bad custom for simplicity, since in the MSDN Library it is stated:
Alternatively, the main and wmain functions can be declared as returning void (no return value). If you declare main or wmain as returning void, you cannot return an exit code to the parent process or operating system using a return statement; to return an exit code when main or wmain is declared as void, you must use the exit function.
Anyway, I like your attitude!
Maxwell Chen
|
|
|
|
|
thank you for the response.
Finnally, I figured out the key problem.
the fstream object is the data member of my
class not declared mutable, but in the member
function declared const, the fstream.getline()
failed.
//ps: the error information vc.net2003 offers me
//just confuses me
|
|
|
|
|
I have a class ( CPolyBtn)
This Function the PreSubclassWindow() allow . When the only one framework then the project is ok. But When I have two frameworks then the project is a error in this Function ASSERT(0). I don’t process. You help me.
void CPolyBtn::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
ModifyStyle(0, BS_OWNERDRAW);
if (m_edges.size()==0)
{
ASSERT(0);
}
else
{
m_rgn.DeleteObject();
SetWindowRgn(NULL, FALSE);
m_pPoints = new POINT[m_edges.size()];
for (int i=0;i<m_edges.size();i++)
{
="" m_ppoints[i]="m_edges.at(i).start;
"
="" if="" (i="">0)
{
ASSERT(m_edges.at(i-1).end == m_edges.at(i).start);
}
}
m_rgnWnd.CreateRectRgn(0,0,0,0);
// couldn't find a way to expand a region. so, we'll
// offset the region in 8 directions and combine the
// results. yes, this is a hack.
CRgn temp;
temp.CreatePolygonRgn(m_pPoints, m_edges.size(), ALTERNATE);
temp.OffsetRgn(-1,0);
m_rgnWnd.CombineRgn(&temp, &m_rgnWnd, SIMPLEREGION);
temp.OffsetRgn(0,1);
m_rgnWnd.CombineRgn(&temp, &m_rgnWnd, SIMPLEREGION);
temp.OffsetRgn(1,0);
m_rgnWnd.CombineRgn(&temp, &m_rgnWnd, SIMPLEREGION);
temp.OffsetRgn(1,0);
m_rgnWnd.CombineRgn(&temp, &m_rgnWnd, SIMPLEREGION);
temp.OffsetRgn(0,-1);
m_rgnWnd.CombineRgn(&temp, &m_rgnWnd, SIMPLEREGION);
temp.OffsetRgn(0,-1);
m_rgnWnd.CombineRgn(&temp, &m_rgnWnd, SIMPLEREGION);
temp.OffsetRgn(-1,0);
m_rgnWnd.CombineRgn(&temp, &m_rgnWnd, SIMPLEREGION);
temp.OffsetRgn(-1,0);
m_rgnWnd.CombineRgn(&temp, &m_rgnWnd, SIMPLEREGION);
// once you use a region in SetWindowRgn, you're not allowed to do *anything* else with it
// so, we'll make a copy.
m_rgn.CreateRectRgn(0,0,0,0);
m_rgn.CopyRgn(&m_rgnWnd);
SetWindowRgn(m_rgnWnd, TRUE);
if (m_bmpID!=0)
{
if (!m_bmp.LoadBitmap(m_bmpID))
{
ASSERT(0);
}
}
if (m_bmpIDD!=0)
{
if (!m_bmpD.LoadBitmap(m_bmpIDD))
{
ASSERT(0);
}
}
}
CButton::PreSubclassWindow();
}
Hello
|
|
|
|
|
i've got this code
FILE *tempf
std::string s;
arch=fopen(tempf,"r+");
cout <<"enter your name";
cin >>s;
fprintf(arch,s);
the last line doesn't compile. It says it can't convert from ...std::string.. to char *
any ideas?
thanks!
|
|
|
|
|
|
#include <iostream>
#include <fstream>
#include <string>
std::string s;
std::ofstream arch("filename");
std::cout << "enter your name: ";
std::getline(std::cin, s);
arch << s;
Reading the name by using "cin >> s;" will stop at the first whitespace character, so if you want to read the entire line std::getline is just perfect. std::getline strips the newline characters from the end (both \r and \n).
Personally find mixing C and C++ I/O a tad on the ugly side. I'm fully aware that it can be a necessity when incorporating C++ code in older apps, but for new code you might as well stick to the iostream library.
I haven't included any error checking in the above code (those are the things we love to leave as an exercise for the reader, no?). All it needs more, really, is a check on whether the file was actually opened and good for writing.
Hope this helps.
--
Henrik Stuart (http://www.unprompted.com/hstuart/[^])
|
|
|
|
|
kfaday wrote:
arch=fopen(tempf,"r+");
What is this? The first parameter to fopen() is a const char* filename, and the return value is a FILE* . Something like:
tempf = fopen("myfile.txt", "r+");
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
I am write a program that has two buttons. The first button would read an int value from a textbox and loads the value into an array. Both of the buttons should take the program execution from the button presses to a single method. The array holding the data should be declared locally in this method. The array is 50 elements long. When Calculate Average button is pressed,and pass the array and the count of the number of items in the array to a separate method that will perform the averaging and display the average value of the array in a label.
Wow, I hope you not a sleep after all that... Okay, I have the buttons,the textboxes, the only problem I am having and need help with is taking the input from the textbox, then assign it to array and the caculate the average.. Thank You for the Help....
|
|
|
|
|
You need to pass the array as a pointer, and then you're right, you need to pass a count as well. Or you can use a vector, which you can pass by reference.
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
I am stuck. I can not get the average if the inputs and I am not sure if the value are add up. Here is what I have so far... If you need to see my whole program just let me know.
void MyForm:: Btn1_Clicked(Object* sender, EventArgs* e)
{
int i,sum;
int* average = new int[50];// Create an array dynamically pg.234
String* average1 = VoltsTextBox->Text;
int val = Int32::Parse(average1);
for(i=0; i<50; ++i)
{
average[i];
}
for(i=0; i<50; ++i)
{
sum = sum + average[i];
}
int ave = sum/i;
String* ave2= ave.ToString("F3");
CurrentTextBox->Text= ave2;
}
|
|
|
|
|
I would expect something like,
class MyForm
{
private :
...
int values [ 50 ] ;
int value_count ;
public :
MyForm () : value_count ( 0 )
{
...
}
void Btn1_Clicked(Object* sender, EventArgs* e)
{
if ( value_count < 50 )
{
values [ value_count ] = GetValueFromBox () ;
++value_count ;
}
}
void Btn2_Clicked(Object* sender, EventArgs* e)
{
int sum = 0 ;
for ( int c = 0; c < value_count; ++c )
{
sum += value [ c ] ;
}
SetValueInBox ( sum / value_count ) ;
}
...
} ;
But if I were doing it I'd use a vector so as not to have any arbitrary limit on number of values.
The code above doesn't fit your requirements, but your requirements seem perverse. I don't understand why you are asked to make the array local to the method, this just seems to be requiring bad code. You would have to make the array and the count static, and then how does the 'average' button method know how to find the data?
Paul
|
|
|
|
|
This will help me finish the class project. There are few people loss in this class. Well, I have about three more project to finish up on, so I know will be posting more questions. Thank You...
|
|
|
|
|
The tab order of the controls in my dialog is wrong and i can't seem to change it. I tried pressing CTRL+D and putting them in the right order but it just doesn't seem to change anything. I also tried to edit the numeric values of the resources, but again to no effect And i really would like them to be in the right order. I also tried deleting them all and creating them again the right order, but i initially created them in the right order anyhow and still no effect, the tab order just stays the same.
Anyone know how to fix this?
Thanks.
Kuniva
--------------------------------------------
|
|
|
|
|
Strange - Layout | Tab Order (Ctrl+D) works for me. Is your .rc file read-only?
Kuniva wrote:
tried to edit the numeric values of the resources
That won't help. To manually fix the tab order, edit the .rc file outside VC6 and insert the controls in your dialog in the right (desired) order.
/ravi
My new year's resolution: 2048 x 1536
Home | Articles | Freeware | Music
ravib@ravib.com
|
|
|
|
|
Right now, I'm using
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
to keep the console window from being shown. However, later in the program, I want to show the window for text input and output.
I've tried CreateConsoleScreenBuffer() and all but cant figure out how to do this.
By using the pragma, am I preventing the window from ever being shown?
If not, how do I show the console window?
|
|
|
|
|
Can't you just use AllocConsole and FreeConsole ?
Best regards,
Dominik
_outp(0x64, 0xAD);
and
__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do??
(doesn't work on NT)
|
|
|
|
|
Thanks for the reply.
AllocConsole() creates a new console window, but I cant print anything to it using printf or cout.
As you can see, I'm not very good with win32 console programming.
Any suggestions?
Thanks!
|
|
|
|
|
I'm not that expert with console programming too but you can try these 2 ways:
1) use the Windows functions for console writing: get a handle to the current console using the GetStdHandle function. Then use the WriteConsole function to write to it.
2) there is a way to use the freopen function and redirect all printf commands (stdout/stdin/stderr) to your new console window. You must somehow get a FILE* for the console, but don't ask me how to do this...
Best regards,
Dominik
_outp(0x64, 0xAD);
and
__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do??
(doesn't work on NT)
|
|
|
|
|
|
I need help with a textbox, something simple because I am a study. I have a button when selected it will call this function that will show a text. The function is below. I would like when the button is selected to call a function and the function will display a textbox that a user can input text.
void Btn1_Clicked(Object*, EventArgs*)
{
MessageBox::Show(S"Lets see if we can switch this text");
}
Thanks for the help......
|
|
|
|
|