|
From: Jason H. <jas...@bt...> - 2001-10-18 22:12:55
|
I think the strcat(szDatei, " \0") was to insert a space between the comand
line arguments when concatenating them. Any string literal will be
automatically NULL terminated anyway, but putting in the extra NULL wouldn't
cause any harm as strcat would just ignore it. A C++ string would be better,
but perhaps he was intentionally writing in pure C?
I've been haveing a look at the C99 standard of late and some of the
developments in there would make it useful to use C instead of C++. C is no
longer a subset of C++, although maybe in future they will extend C++ to
include at least some of the inovations? Of course I don't think that the
compiler used in Dev-C++ supports it yet.
Jason.
----- Original Message -----
From: "Ioannis Vranos" <no...@ya...>
To: "Dev-C++" <dev...@li...>
Sent: Thursday, October 18, 2001 10:33 PM
Subject: RE: [Dev-C++] Errorhandling
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Christian Wallukat
> Sent: Thursday, October 18, 2001 2:40 PM
> To: CPP-Maillinglist (E-Mail)
> Subject: [Dev-C++] Errorhandling
>
>
> Hi list,
>
>
> I try to write a little Dos-tool.
> If I want to get the parameters and the user has no
> parameters specified I get an exception.
>
> I think the error comes if i call strlen() with an empty string...
>
> How can i handle this error, has anyone an idea ????
>
>
>
> ----------------Schnipp---------------------
> //Get commandline
> strcpy(szDatei, argv[1]); // *** access violation
// *** argv[1] doesn't exist with no commands passed!
> for (nCount = 2; argc > nCount; ++nCount)
> {
> strcat(szDatei, " ");
> strcat(szDatei, argv[nCount]);
> };
>
> //Check Commandline
> if (strlen(szDatei) <= 5)
> {
> printf("Wrong parameter !");
> };
> -------------------Schnapp------------------
>
>
> Thanx for help
>
>
> Chris
The errors may be many. For example szDatei may not have the necessary
size to hold all these strings... Except of that, strcat(szDatei, "
\0"); is a mistake. So wwe do not know if you get an exception, the most
possible is that you get memory access error.
For more bullet proof code use C++ higher level constructs like string.
E.g.
#include <iostream>
#include <string>
...
//somewhere in a function
using namespace std;
...
if(argc>1)
string szDatei=argv[1];
for (int nCount = 2; nCount<argc; ++nCount)
szDatei+= " " + argv[nCount]); // ***added a space
//Check Commandline
if (szDatei.size() <= 5)
cout<<"Wrong parameter !";
This is the equivallent higher level C++ code with your mistakes
corrected (the first if statement, and int nCount in the for loop).
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.f2s.com
* Alternative URL: http://run.to/noicys
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|