|
buffer[0] = 'X'; This sentence owerwrites the previous value of buffer[0] , which was the terminating null character from strcpy(buffer, ""); As you haven't added any additional terminating character, the remains of the first string appears, hence the output. To get the result you're after, write:
buffer[0] = 'X';
buffer[1]=0; Unless you're doing this for fun or learning purposes, it is strongly advisable that you leave this char manipulation stuff in favor of more robust alternatives like std::string .
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
ChiYung wrote:
strcpy(buffer, "");
buffer[0] = 'X';
cout << buffer << endl;
strcpy does not delete or null any of the characters inside of the buffer. Therefore passing in an empty string copies zero (0) characters and leaves the string untouched. If you want the string to be only 'X' you need to do something like:
buffer[0] = 'X';
buffer[1] = '\0';
The second character is the null character (the slash means it is an escape character); which in C means the end of the string. Character strings in C are always string length + 1. Example:
The string literal, "Hello World!", is actually 13 characters long, the 12 visible characters plus 1 for the null.
Hope this helps,
Nathan
---------------------------
Hmmm... what's a signature?
|
|
|
|
|
I have a quite enerving way to initialize a strin array, i always do:
char buffer[10];
memset(buffer , 0 , 10 );
PS: u can use : sizeof(buffer)
each time i want to initialize
Papa
Murex Co.
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|
ChiYung wrote:
char buffer[10];
strcpy(buffer, "ABCDEFGHIJ");
You have a more serious problem in that this code overruns the array. "ABCDEFGHIJ" is an 11 character string, 10 letters and the terminating 0 character.
--Mike--
Just released - RightClick-Encrypt v1.3 - Adds fast & easy file encryption to Explorer
My really out-of-date homepage
Sonork-100.19012 Acid_Helm
|
|
|
|
|
One other point - your char array is 10 elements - you need an extra element to hold the terminating null.
So far when you've run there has been a null anyway but at some point you could end up with a string several MB long !
We've all done it when starting out.
Elaine (Trollslayer)
|
|
|
|
|
I'm beginning work on an NT service (only in the early design phase right now) that will need to respond to network requests. I've done work with networking before, but this will be my first service. What I'm finding already is that there doesn't seem to be an easy way to handle networking. At least not as intuitive as through CSocket with MFC. What type of system does everyone else use for a service? I know I can setup a thread that continually tries to receive, and that might work. I've also looked into using the MFC service classes out there (PJ Naughter's in particular) so that I can use CSocket again. Both seem to have there up's and down's. Is there an easy way that I'm not seeing?
Also, as a second question. How do you control a service? I understand how to start, stop, and pause them. But how do I grab it and call a function? Or can I? Basically, if I write a control panel applet for example, how do I adjust the service in realtime? I could always write adjustments to the registry, but then I would also have to have a thread watching for changes and that doesn't seem very logical either. Again, is there an easy way that I'm just not seeing?
"Go Confidently in the direction of your dreams. Live the life you've imagined." - Thoreau
|
|
|
|
|
To write your very first service is always a bit of hard work. However, it is not that difficult as it looks like:
1) To really work with a service you need some kind of inter-process-communication (IPC). IPC is the way to communicate with your service. Sockets are one example for IPC, I usually prefer NamedPipes or RPC (because they integrate well into the NT security modell), but one may also use shared memory, sockets, NetBIOS and so on.
2) Even if possible I recommend not using MFC in a service. For things like CString or CArray it is you can use the ATL/WTL classes that provide a compatible interface. Or use STL. Especially CSocket is a no-no. It is integreated to much into "windowing applications".
3) To control a service a CPL or MMC snap-in is a really good solution. You can monitor the registry as you suggested, but you could also inform the service about changes via a Win32 event object, IPC or a user defined service control code. The last one is elegant because you don't need an own watcher thread.
4) Debugging a service is a bit hard, because it is not possible to run it directly under the debugger. However, you could connect your debugger later on to the service (via task manager). To debug service startup code a hardcoded breakpoint works best.
// some code
_asm int 3 // Program will break here
// some more code
If the code reaches the hardcoded breakpoint, the usual "unhandled exception" dialog box will appear and give you the chance to attach the debugger.
Hope that helps for the moment
--
Daniel Lohmann
http://www.losoft.de/
|
|
|
|
|
Daniel Lohmann wrote:
or a user defined service control code. The last one is elegant because you don't need an own watcher thread.
What exactly do you mean by this? What is a user defined service control code?
Also, if I do use normal socket code, is it common to have a looping thread that just sits there waiting to receive something?
Programming in binary is as easy as 01 10 11.
|
|
|
|
|
Greven wrote:
What exactly do you mean by this? What is a user defined service control code?
Using the ControlService() API you can send control codes to a services' handler function. Besides the predefined control codes (SERVICE_CONTROL_STOP, SERVICE_CONTROL_PAUSE, etc.) the range 128-255 is reserbed for user-defined control codes.
Greven wrote:
Also, if I do use normal socket code, is it common to have a looping thread that just sits there waiting to receive something?
Yes, there must be some thead that waits for recieving something. However, you could use overlapped IO so the thread is not blocked and can do also something else. You could even use the services' main thread for that purpose (the thread that starts in your ServiceMain).
--
Daniel Lohmann
http://www.losoft.de
|
|
|
|
|
My web project allows a user to navigate to a word document. My task is, not to allow the user to modifify the document. I can have the word document read only. But, user is still allowed to type-in the keys from keyboard, though he can not save it.
I do not want the user to press any key that modifies the text on the word document while it is being displayed in IE. Is it possible?
|
|
|
|
|
Srini Kella wrote:
I can have the word document read only. But, user is still allowed to type-in the keys from keyboard, though he can not save it.
I do not want the user to press any key that modifies the text on the word document while it is being displayed in IE. Is it possible?
What is the problem with the read only setting? I'm not sure what purpose you would be solving as the user would not be able to modify the source file at all.
Nick Parker
|
|
|
|
|
From MSDN I got
// Look for "Open" menu item from the File menu. Insert a new
// menu item called "Close" right after the "Open" menu item.
// ID_CLOSEFILE is the command id for the "Close" menu item.
pos = FindMenuItem(submenu, "&Open...\tCtrl+O");
if (pos > -1)
submenu->InsertMenu(pos + 1, MF_BYPOSITION, ID_CLOSEFILE, "&Close");
But the compiler says ID_CLOSEFILE is undefined. I dont know what I need to do in this case....I thought that hereafter that item would be identified with ID_CLOSEFILE, since I didnt make it in the resource editor....
Thanks
ns
|
|
|
|
|
Its ID_FILE_CLOSE, Generally they follow the format:
ID_ is the lead characters
FILE the name of the menu the item resides in
CLOSE the name of the menu item
So for example ID_VIEW_TOOLBAR would allow toggling of the toolbar in view of an app.
Roger Allen
Sonork 100.10016
If I had a quote, it would be a very good one.
|
|
|
|
|
Thank you very much. I guess MSDN messed up! Works Fine.
However, if its a custom menu item I want to add, which has no ID defined in the class wizard (since I didnt put in into the design resource since I'm adding it programmatically), how would I make that work? I found ID_VIEW_TOOLBAR in the class wizard so its been predefined, but in
pos = FindMenuItem(submenu, "&Open...\tCtrl+O");
if (pos > -1)
submenu->InsertMenu(pos + 1, MF_BYPOSITION, ID_FILE_ARBITRARY, "&Arb");
whats ID_FILE_ARBITRARY? I didnt actually create a menu item in the resource editor else it would show up anyways at runtime. I dont see a way to make it invisible so that I only see it where I insert it and not where I positioned it in the resource editor menu layout.
Appreciate your insight,
ns
|
|
|
|
|
ID_FILE_ARBITRARY would be the ID of the WM_COMMAND message you woule need to map. Once in the menu, you would have to write the handlers for them manually.
afx_msg void OnArbitrary() ;
ON_COMMAND(ID_FILE_ARBITRARY, OnArbitrary)
void CMyClass:OnArbitrary()
{
}
You will have to make sure that ID_FILE_ARBITRARY is #defined in resource.h and doesn't clash with an existing menu ID.
Roger Allen
Sonork 100.10016
If I had a quote, it would be a very good one.
|
|
|
|
|
I hadn't ever looked into resource.h (thats how new and splotchy my MFC experience is). So all I need to do is assign my arb ID a number thats not in there and I'm good to go, it seems. Theres no mention of ID_FILE_CLOSE etc so I guess they are taken care of somewhere else.
Okay if I use numbers from:
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_3D_CONTROLS 1
#define _APS_NEXT_RESOURCE_VALUE 131
#define _APS_NEXT_COMMAND_VALUE 32783
#define _APS_NEXT_CONTROL_VALUE 1007
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
or avoid these as well? eg. #define ID_FILE_ARB 131?
Thanks so much.
ns
|
|
|
|
|
ns wrote:
Okay if I use numbers from:
These are ... housekeeping definitions used by the resource editor when adding new resources. The values are the ones used by the next resource of a given type added.
A good way to get a new, unique symbol defined is to use the View->Resource Symbols menu option in Visual Studio; this brings up a dialog letting you view existing symbols and add new ones, without having to manually edit resource.h (which could be troublesome if you're not careful and then later use the resource editor to add new resources).
--------
PMGRE --Shog9 --
|
|
|
|
|
ID_FILE_CLOSE and other standard MFC menu commands etc are defined inside the MFC headers. These shouldn't be changes otherwise you stop the MFC from working so that why thier not in your resource.h.
When adding your own ID's manually (unlike what Shog9 said), if you were to use the next available ID specified by
#define _APS_NEXT_RESOURCE_VALUE 131
You would have to increment this #define in the file so that the next autmatically added one would not clash with the one you just added manually.
#define ID_FILE_MYCOMMAND 131
#define _APS_NEXT_RESOURCE_VALUE 132
Roger Allen
Sonork 100.10016
If I had a quote, it would be a very good one.
|
|
|
|
|
My thanks to both of you. I appreciate your input tremendously,
ns
|
|
|
|
|
Hi
Is there anyway that i can make my application have the looks of Outlook?? If yes, then where will i get that class?
Thanks alot!!!
|
|
|
|
|
how about searching this site for articles that have "outlook" in the title?
--
Help me! I'm turning into a grapefruit!
|
|
|
|
|
sorry, i should've done this before!!!
Thanks for the advise
|
|
|
|
|
I checked all the doc/view classses but didnt find a handler for the pre-existing VC ID_FILE_OPEN menu item. Which class handles the COMMAND for this ID? If it were the view class, and I wanted to remotely trigger this menu, could I say:
AFxGetMAinWnd()->SendMessage(WM_COMMAND, ID_FILE_OPEN)?
Does it matter who is handling this command? CAn this work even if any class were to be handling it?
Thanks,
ns
|
|
|
|
|
CWinApp does, or your CWinApp derived class does.
You should find these entries in your CWinApp derived class's message map
// Standard file based document commands
ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
Roger Allen
Sonork 100.10016
If I had a quote, it would be a very good one.
|
|
|
|
|
Thank you. So why do we send a message to the AfxGetMainWnd() object rather than AfxGetApp()? THe former works. Havent tried the latter.
One odd thing I found is that if my menu item is handled locally by the class thats sending the message, I dont have to Afx -> sendmessage it, just a direct sendmessage (since its to this->). So looks like the message has to be sent to the class that handles the command. In retrospect thats obvious....
So why AfxGetMainWnd (which returns the mainframe right? )and not AfxGetApp who is handling the ID?
Thanks,
ns
|
|
|
|