|
Need to create a function that would write some text message (that I send via argument) to a file. It has to be appended, and it has to appear on the next line. (More like how Print command works for vb file handling).
For the present I have been working with this code:
<br />
#include <windows.h><br />
<br />
<br />
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);<br />
<br />
<br />
void debug(LPSTR t);<br />
<br />
int WINAPI WinMain (HINSTANCE hThisInstance,<br />
HINSTANCE hPrevInstance,<br />
LPSTR lpszArgument,<br />
int nFunsterStil)<br />
<br />
{ <br />
debug("This is a test\n"); <br />
debug("End Program");<br />
return messages.wParam;<br />
<br />
}<br />
<br />
<br />
<br />
<br />
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)<br />
{<br />
return 0;<br />
}<br />
<br />
void debug(LPSTR t)<br />
{<br />
DWORD d; <br />
HANDLE h;<br />
LONG k=0;<br />
if ( (h = CreateFile("log.txt", GENERIC_WRITE, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,0) ) ) <br />
{<br />
SetFilePointer(h,0,&k,FILE_END);<br />
if ( ! WriteFile(h, t, strlen(t), &d, NULL) )<br />
MessageBox(0,"Write failure","Status", MB_OK | MB_ICONERROR); <br />
} <br />
else<br />
MessageBox(0, "Opening failure", "Status", MB_OK | MB_ICONERROR);<br />
<br />
CloseHandle(h); <br />
}<br />
-- modified at 15:41 Monday 6th August, 2007
The only problem is that I get my text file outputted as below:
<br />
This is a test¤End Program<br />
Instead I would want it as
<br />
This is a test<br/>End Program<br />
<br />
-- modified at 15:47 Monday 6th August, 2007
|
|
|
|
|
deostroll wrote: This is a test\n
Have you tried "This is a test\n\r"?
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
The people in the lounge said I should google for the answer to a programming question but I do not know what search engine to use
|
|
|
|
|
Wes Aday wrote: Have you tried "This is a test\n\r"?
You mean "\r\n"
|
|
|
|
|
But why is the value of k zero? LONG k = 0;
|
|
|
|
|
I have a problem which I am finding myself unable to figure out.
I have a fairly simple CSocket-derived class, which does not much beyond overriding OnReceive so that it saves all the received buffer, and then calling a function of a Pointer to a thread class of mine, which does what it will with the buffer.
At the moment I have a server and a client, both of which have a thread for receiving and a thread for sending through these sockets. The general communication type is thus - client sends a request, server processes and sends some sort of a reply. The messages to be sent in-process are delivered through inter-thread messaging.
The problems occurs mainly while debugging, but it can happen while just running the applications - at some point, if a number of request are "fired" rapidly, the connection get messed up in a rather odd way:
Clients sends successfully, then Server receives, does it's processing, goes on to send a reply, the send operation goes without errors, and yet the other side never receives the message. I've traced all the receive calls on my socket class, and not once there was an error.
So basically, everything indicates that the connection is alive, and yet it never goes through in one direction.
The messages are of varied sizes, but when in debug, 2 140-bytes requests and 160-bytes replies kill the communication.
|
|
|
|
|
Does the error occur for any of the unit tests?
What sort of coverage do your unit tests supply?
|
|
|
|
|
Are you receiving just the number of bytes you're expecting or receiving all the bytes sent?
If the latter, are you parsing the bytes into individual "messages" or assuming it's one message and losing the rest?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I am receiving the number bytes I'm expecting, first by receiving a buffer size parameter,and then the rest of the message.
|
|
|
|
|
You mentioned another thread.
OnReceive() occurs on the thread that creates the socket (through a hidden HWND).
Is this your UI thread?
Seems like something's causing you to lose FD_READ notifications from the socket.
I can only guess without seeing the code.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
CSocket is also notorious for being a bug-riddled piece of code. You may want to switch to CAsyncSocket or roll your own.
|
|
|
|
|
How do you add text to the title bar of an application using Microsoft Visual Studio 2003 .NET Visual C++?
|
|
|
|
|
SetWindowText() or WM_SETTEXT.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
|
This application has a mainframe window and like an older version of Microsoft Word documents, it can have multiple files open (MDI). If you tile the windows, the active window's file name title shows in the mainframe's. The format the header or title bar has is as follows:
[Application Name] - [Filename.extension]
I want to modify it like this:
[Application Name] - [Filename.extension] - [My Text Added Here]
The problem is I do not have a handle to this section such as SetTitle() or SetName() and I am thinking I need to add a section to have such a handle. However, this is dealing with MFC.
|
|
|
|
|
GetWindowTextLength()/GetWindowText() or W<_GETTEXT/WM_GETTEXTLENGTH can be used to get
the current title.
Append your new text and set the new text.
For MFC you can use something like
CString titlestr;
MainWnd->GetWindowText(titlestr);
titlestr += _T(" - [My Text Added Here]");
MainWnd->SetWindowText(titlestr);
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Again[^]
----------------------------
286? WOWW!
|
|
|
|
|
Are you simply wanting a 2D array?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Thanks atlast got a reply.
No I'm not simply wanting a 2D Array.
An array is a group of memory locations that we reserve, Ok in the below statement, I've reserved. 6 Memory locations that has the capability to store 6 integers. And I've got a pointer pointing to that.
int* pInt = new int[2*3];
The dimension. It's how we look at the reserved memory location.
The memory as of now, looks like :
[] [] [] [] [] []
^
Incrementing the pInt's value would make it go like :
[] [] [] [] [] []
-+^
[] [] [] [] [] []
---++^
This is linear. We can do the same with pInt[0],[1]..etc. Ok this about looking at the memory locations in 1D.
Now I want to look at it in 2D.
The same memory location.
I would want to initialize a 2D pointer to the same memory location.
Where I should be able to mention 2 indices. You may take it as 2x3 or 3x2
when you say 2x3,
That says, 2 rows and 3 elements in each row.
[] [] []
[] [] []
Now tell me how should declare a pointer to the memory location that it would access them as 2D.
int* pInt = new int[2*3];
int(** ptr2D)[2][3] = new int*[2]; <--something like this. NOT SURE.
If we are gonna look at the same memory locations as 3x2,
then it becomes.
[] []
[] []
[] []
Here ptr2D[2][0].. would make sense. Because, The number of rows, 0th row, 1th row, 2nd row. Has 3 rows. But in the former case, it would crash because we were looking at it as 2 rows with 3 columns.
My question makes sense?
----------------------------
286? WOWW!
|
|
|
|
|
_8086 wrote: Now tell me how should declare a pointer to the memory location that it would access them as 2D.
int **p = new int*[FirstDimension];
for(int i = 0; i < FirstDimension; i++)
{
p[i] = new int[SecondDimension];
for(int j = 0; j < SecondDimension; j++)
{
p[i][j] = 0;
}
}
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Let me see if this satisfies my requirement. Anyway thanks for your reply.
----------------------------
286? WOWW!
|
|
|
|
|
It does not matter. pInt[2][3] is the same as pInt[3][2].
|
|
|
|
|
Yes, but the question is different.
----------------------------
286? WOWW!
|
|
|
|
|
Rage wrote: It does not matter. pInt[2][3] is the same as pInt[3][2].
Oh, you've said like this?
Ok, take up the first one., pInt[2][3];
Try Printing the element pInt[3][0]."Boom" !??!. How can this be eqivalent to pIntp[3][2].
The dimension is different and that's what the question is all about. Looking the same block of memory with different dimensions.
----------------------------
286? WOWW!
|
|
|
|
|
No, you are reserving 6 memory locations. How you read them does not matter.
|
|
|
|
|
I don't think you can do what you're trying to do.
If you can, I'd like to see the syntax.
For a 2 dimension array like this:
int myarray[2][3];
myarray is not a pointer. You can't cast an int pointer to the type of myarray (AFAIK).
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|