|
The 32 in strncpy tells the function the number of chars to copy the the string. You don't want it to copy past the bounds of your array.
If you send the function a 40 char string like "0123456789012345678901234567890123456789", it should cut it off at 32.
Here's a little console app to test it:
#include <iostream.h>
#include <string.h>
char Names[21][32];
int NN = 0;
void vSetName(char* NName)
{
strncpy(Names[NN], NName, 32);
NN++;
}
void main()
{
char inp;
vSetName("testing");
vSetName("0123456789012345678901234567890123456789");
cout << Names[0];
cout << "\r\n";
cout << Names[1];
cin >> inp;
}
Like it or not, I'm right.
|
|
|
|
|
Oops!
The #includes didn't go through.
#include <istream.h>
#include <string.h>
Like it or not, I'm right.
|
|
|
|
|
<istream.h> is deprecated. Instead, you should be using <istream> , possibly followed by
using namespace std;
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
did you know joaquin is the name of my streat? your famous!!!!
Thanks for the help again and again, and again its always appreciated
~SilverShalkin
|
|
|
|
|
did you know joaquin is the name of my streat? your famous!!!!
Are you by chance from California? There's a lot of Spanish toponyms there. By the way, in Madrid (where I live) there's a street with my exact name "Joaquín María López" --it was some politician from the XIX century.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
hi all,
in one program i need to extract data from a txt file. the program extracts data line by line, but it doesn't catch the EOF.
i included the part of the code that gives me trouble below, if anybody knows how to fix this, let me know!
thanx
Fred
...
char c = 0, *buffer = 0;
int nc = 0;
while(c != EOF) // !! PROB
{
if(!ReadFile(hFile, &c, 1, &dwByteRead, NULL))
{
AfxMessageBox("Error in ReadFile() : %d", GetLastError());
return NULL;
}
if(dwByteRead == 1)
{
if(c == '\r' || c == '\n' || nc >= CHAR_LINE)
{
szSentence[nc] = 0;
return szSentence;
}
else
{
szSentence[nc++] = c;
}
}
}
|
|
|
|
|
From MSDN on ReadFile() .
the return value is FALSE and GetLastError returns ERROR_HANDLE_EOF
when the file pointer goes beyond the current end of file. So, every time you get to EOF, you're generating what you perceive to be an error. Check for both instead of just the return value.
Alternatively, you could use c to check for the value of 46 (0x2E), as that's the ASCII/ANSI EOF char.
Jeremy L. Falcon
"It's in the mail."
<nobr>
Homepage : Sonork = 100.16311 Surely some striving souls survive symptomatic stress?
|
|
|
|
|
ReadFile does not return any EOF . Instead, it returns TRUE with dwByteRead being 0.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
My team is given a task of developing a chat server. Ofcourse the server will be multithreaded.But the concern here is that we do not want to develop server using MFC. Reason is that we may want to deploy this server on LINUX platform in the future.
My question is that whether multithreading is possible using standard ANSI C++. Is there any method that we can develop the chat server in C++ using multithreading and can compile the same code in compilers like VC++ and on UNIX ???
|
|
|
|
|
If you want portability, you can use the
posix functions like the c-runtime library's
beginthread & endthread etc..
These functions will be portable across platforms.
|
|
|
|
|
Look up:-
_beginthreadex
Nish
Regards,
Nish
Native CPian.
Born and brought up on CP.
With the CP blood in him.
|
|
|
|
|
If you want threads and portability, then use the threads contained in BOOST.
www.boost.org
If you don't want to use BOOST, then at least look at the threads implementation in there so you can get an idea what is required to do threading using PTHREADS. Then you can design your own system. The important thing is that the OS specific calls such as CreateThread/_beginthreadex are abstracted using some type of class or library. Then you just have to adjust the class when porting to another platform.
Tim Smith
I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
|
|
|
|
|
With GetAsyncKeyState(), GetKeyboardState() and GetKeyState(), I cannot get the global keyboard state
under Windows 2000. How can I get it and how can I set it?
Can you help me?
Thanks.
|
|
|
|
|
i don't know why you can't get it, but I can:
bool ctrl = (GetKeyState(VK_CONTROL) & 0xf000) == 0xf000;
bool shift = (GetKeyState(VK_SHIFT) & 0xf000) == 0xf000;
works fine for me.
-c
"Should you not have a ramekin of sea salt beside the stove, you don’t deserve oatmeal."
Dean Allen
|
|
|
|
|
Thank you for your replay.
My scenario is:
When message WM_HOTKEY is received, I repeatedly checks keyboard state until no key is pressed. If my program is in the foreground, GetKeyState() cannot work well. Now I has to create a worker thread to check it.
Now I has another question:
GetKeyState() should return a SHORT value which is a 16-bit integer, and only the low byte is significant. If the specific key is pressed, it returns "FF81" or "FF80". Are you sure the above code segment can work well?
|
|
|
|
|
sorry, i can't help with your first question.
2nd Q:
from MSDN:
GetKeyState:
Return Values
The return value specifies the status of the specified virtual key, as follows:
* If the high-order bit is 1, the key is down; otherwise, it is up.
* If the low-order bit is 1, the key is toggled. A key, such as the CAPS LOCK key, is toggled if it is turned on. The key is off and untoggled if the low-order bit is 0. A toggle key's indicator light (if any) on the keyboard will be on when the key is toggled, and off when the key is untoggled.
-c
ABSURDITY:
A statement or belief manifestly inconsistent with one's own opinion.
|
|
|
|
|
i need to implement some search and filtering capabilites in my application.the problem is that i have to call dialog from my CRecordView class.that's OK but how to give informations for CListCtrl which is on dialog-from CRecordView
|
|
|
|
|
Remember that the list control is not created till the dialog is almost shown on screen. Most people try to access member control variables after they instantiate their dialog wrapper class. I suggest that you pass all the strings to a string array and then populate the list control in the initdialog function of the dialog using those strings in the array.
Regards
Nish
Regards,
Nish
Native CPian.
Born and brought up on CP.
With the CP blood in him.
|
|
|
|
|
thanks man.i would give almost anythyng to foun out how can You possible know all those things?can You tell me where can i download (for free by all means) material (ebooks,html files,...) about MCSD 70-016 certificate?do You think that i can learn something smart from them?Also i need to ask is there any chance to study online MFC programming (again-for free) and where?code on this site is sometimes just too complex for me and i want to follow some guide to get stuck in with it graduatelly.i want to learn how to develop windows applications with MFC so bad!!! thanks for everything
|
|
|
|
|
Hello
What IOCTL codes are available to access to parallel port from an application?
I am trying to read/write from the parallel port with the file access functions but I must be missing something and it doesn´t work.
I don´t have C++ nor the SDK nor the DDk. I only have access to MSDN online help.
Thanks
Good luck
http://www.geocities.com/nafiton
|
|
|
|
|
I remember something about using "COM1" as the path name for CreateFile when wanting to access the serial COM1 port.
I guess you just have to specify the name of the port then.
"It could have been worse, it could have been ME!"
|
|
|
|
|
Hi,
im trying to set the current working directory before going in to a file dialog and then reseting it to its original value after the dialog has closed:
TCHAR strCurrentDirectory[MAX_PATH];
GetCurrentDirectory(MAX_PATH, strCurrentDirectory);
CFileDialog fileDlg(TRUE, ".dot;.doc", NULL, OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR, "Microsoft Word Templates (*.dot; *.doc)|*.dot;*.doc|HTML Templates (*.htm; *.html)|*.htm; *.html|All files (*.*)|*.*||\0", NULL);
fileDlg.m_ofn.lpstrInitialDir = "c:\\projects\\temp";
if(fileDlg.DoModal() == IDOK)
{
// Do something
}
SetCurrentDirectory(strCurrentDirectory);
But this doesnt work as next time i go in to a different file dialog the current directory is set as c:\projects\temp.
Has anyone had to cope with a similar problem before and if so how did they get around it?
Cheers,
Andy.
|
|
|
|
|
Since you have used OFN_NOCHANGEDIR, you really don't need to use Get/SetCurrentDirectory at all.
Nish
Regards,
Nish
Native CPian.
Born and brought up on CP.
With the CP blood in him.
|
|
|
|
|
Hi Probably you are using Windows 2000.
In this OS the last opened directories thru the
application is set in the registry and the
File Dialog reads thru that.
|
|
|
|
|
hi, yes i am using windows 2000. do you know where the registry entry is stored so that i can modify that myself.
Cheers,
Andy.
|
|
|
|