free2cric said:
CString fname(argv [1]);
LPTSTR p1 = fname.GetBuffer(fname.GetLength());
ok, others have pointed out that this code is C++ and not C. For that,
please use tools that C++ offers you (in particular string types), and
don't try to do it yourself. It's not necessary and it's error prone.
Further discussion on that topic belong to a C++ designated newsgroup
though, like alt.comp.lang.learn.c-c++ or comp.lang.c++.moderated.
Now, on the rest of the code, which is in fact valid C:
char teststr[1000];
sprintf(teststr,"%s",p1); //or
strcpy(teststr,p1);
Problems I see here are:
1. you copy argv[1] into a CString and then copy its content to a static
array. Why? I'd understand one copy, but why two?
2. 'LPTSTR' is a typedef for 'TCHAR*', but a TCHAR is not a char! Depending
on the environment, it could be a wchar_t or a char, so you definitely
have to convert, an operation that might fail. This also has nothing
directly to do with the C language, it is rather a feature of the win32
API. Note: it's also not a C++ problem, but I can't tell you a good group
where to get more info on that.
3. Your buffer is too small or too big. The only right size it could have
is 'strlen(argv[1])+1', so a static buffer wont work. Use malloc() if you
plan to stay with C or use string types in C++.
4. The way to copy is strcpy(). In this case, sprintf() would work just as
good, but probably not as fast. strcpy() is simply the more suitable tool
for the job, sprintf() has other cases where it is the right tool.
BTW: get a good book, look at the reviews at
http://accu.org
Uli