RE: sscanf problem
RE: sscanf problem
- Subject: RE: sscanf problem
- From: "Chris Reed" <email@hidden>
- Date: Fri, 30 Jan 2004 13:51:56 -0600
- Thread-topic: sscanf problem
I realize this is a little old, but I wanted to make sure no one was
left with the wrong impression.
(further comments in-line below)
>
-----Original Message-----
>
From: email@hidden [mailto:cocoa-dev-
>
email@hidden] On Behalf Of lbland
>
Sent: Wednesday, January 28, 2004 8:13 AM
>
To: David Dauer
>
Cc: Cocoa-Dev
>
Subject: Re: sscanf problem
>
>
On Jan 28, 2004, at 8:10 AM, David Dauer wrote:
>
>
> I've got a strange problem using sscanf:
>
>
>
> const char *s;
>
> char theString[ 256 ];
>
>
>
> s = "123 456 \"blah\"";
>
>
>
> int theInt1;
>
> int theInt2;
>
>
>
> sscanf( s, "%d %d \"%s\"", &theInt1, &theInt2, theString );
>
>
>
> NSLog(@"%@",[NSString stringWithCString:theString]);
>
>
>
> This returns a string 'blah"' - and i've no idea why i get a quote
>
> sign at
>
> the end of the string.
>
>
>
> Anyone knows why this happens and how to fix that?
>
>
You have maybe 4 problems:
>
>
s is a "constant" string and may be in a constant mach-o header and
>
should not be overwritten.
>
>
use something like:
>
>
s[50];
>
strcpy(s, "123 456 \"blah\"");
This is incorrect.
The const char* type means that the characters pointed to by variable
cannot be modified, but the pointer itself can. So you can assign a C
string (C string constants are of type const char[], or const char* if
you prefer) to s as many times as you like.
You were thinking of something like this:
const char* const s = "a constant string";
Here, the s pointer itself is constant cannot be modified after being
set with an initializer.
Another point to make sure is clear, is that you do not need to use
strcpy() as long as you do not intend to modify the strings you are
working with. This is perfectly valid:
const char* s = "string A";
printf(s);
s = "string B";
printf(s);
s = "string C";
printf(s);
(Replacing the printf with whatever operation you want to use that
doesn't modify the characters pointed to by s.)
-chris
>
>
next you need to assign theString:
>
>
strcpy(theString, "");
>
>
3, '\"' is an escape character for a quote, if you want a backslash
use:
>
>
"123 456 \\\"blah\\\""
>
>
and 4: you write into s, not theString.
>
>
maybe you want something like:
>
>
theString = [NSString stringWithFormat:@"%d %d %s", theInt1, theInt2,
>
"blah"];
>
>
NSLog(theString);
>
>
>
-lance
>
>
Lance Bland
>
mailto:email@hidden
>
VVI
>
888-VVI-PLOT
>
http://www.vvi.com
>
_______________________________________________
>
cocoa-dev mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.