reset a string. empty it totally.

S

spike

My prygram goes through a string to find names between '\'-characters
The problem is, parts of the name in sTemp remains if the new name is
shorter than the old name.

code
---------------------------------------------------
char sTemp[50];
char sText[] = "THELONGESTNAME\\samantha\\gregor\\spike\\..."; // and
so on...

....
some code
....

int i=0;
while((sText != '\\'))
{
sTemp = sText;
i++;
}
printf("Name: %s\n",sNameBuff);
// Here i want to reset the string, totally empty it.
// this dosent do it.
sNameBuff == "";
// Start over again...
....
-------------------------

example output:
Name: THELONGESTNAME
Name: samanthaSTNAME
Name: gregorhaSTNAME
Name: spikerhaSTNAME

How can i make the string completely empty?
 
M

Mike Wahler

spike said:
My prygram goes through a string to find names between '\'-characters
The problem is, parts of the name in sTemp remains if the new name is
shorter than the old name.

code
---------------------------------------------------
char sTemp[50];
char sText[] = "THELONGESTNAME\\samantha\\gregor\\spike\\..."; // and
so on...

...
some code
...

int i=0;
while((sText != '\\'))
{
sTemp = sText;
i++;
}
printf("Name: %s\n",sNameBuff);
// Here i want to reset the string, totally empty it.
// this dosent do it.
sNameBuff == "";


This statement compares the value of 'sNameBuff'
(which I guess is a pointer), to the value of the
address where the string literal "" is stored, then
discards the result of the comparison. You probably
meant to write:

sNameBuff = "";

But that doesn't help either. That assigns the address
of the first character of the string literal "" to
'sNameBuff'. If 'sNameBuff' is an adddress obtained
e.g. from 'malloc()' (and a copy of that address is
not saved elsewhere), you have a 'memory leak'.

// Start over again...

You don't show its definition or usage, but assuming
that 'sNameBuff' is a pointer to your string (or the
name of an array containing it), write:

*sNameBuff = 0;

or

sNameBuff[0] = 0;

There may indeed still exist characters in subsequent
memory locations, but because the first character
is now zero, subsequent characters are no longer part
of your string, it's "empty".

-Mike
 
R

Russell Hanneken

Mike said:
You don't show its definition or usage, but assuming
that 'sNameBuff' is a pointer to your string (or the
name of an array containing it), write:

*sNameBuff = 0;

or

sNameBuff[0] = 0;

There may indeed still exist characters in subsequent
memory locations, but because the first character
is now zero, subsequent characters are no longer part
of your string, it's "empty".

This is speculation, but I think Spike's problem might be that he's not
terminating each new sequence of characters with a '\0'. That would
explain why he sees the last part of the old string when he displays the
new string. If this is so, assigning 0 to the first element won't help
him. Every time he writes new characters to the buffer, he would be
overwriting the '\0' in the first position, and not replacing it with a
new end-of-string boundary marker.

Regards,

Russell Hanneken
(e-mail address removed)
Remove the 'g' from my address to send me mail.
 
P

Peter Hille

Hi,

Am Fri, 05 Mar 2004 13:58:28 -0800 schrieb spike:
int i=0;
while((sText != '\\'))
{
sTemp = sText;
i++;
}


sTemp = '\0';

Inserting this would NULL terminate the string after you have copied your
new stuff into it so that the following printf() will only display what
you have copied.
printf("Name: %s\n",sNameBuff);
// Here i want to reset the string, totally empty it.
// this dosent do it.
sNameBuff == "";
// Start over again...
...

The sTemp i mentioned above does not clear the string completely, it
just places the terminating 0 directly after your newly copied string.
If you want to completely clear it you could use

bzero(sTemp, strlen(sTemp));

which would overwrite the whole string with 0's, but i would prefer the
first one as it's a bit faster ;-)

Hope that helps

Peter
 
C

Christopher Benson-Manica

spike <[email protected]> spoke thus:

Building on earlier replies...

// style comments are frowned on in this newsgroup, because few (if
any) compliant C99 compilers exist. Be safe, be smart, use /* */
comments.
int i=0;
while((sText != '\\'))
{
sTemp = sText;
i++;
}
printf("Name: %s\n",sNameBuff);


I predict that sNameBuff is declared something like

char *sNameBuff=sTemp;

If that isn't correct, well, you should have posted its declaration
yourself. If it is correct, an earlier reply likely nailed your
problem - you aren't adding a NUL character at the end of sTemp.
You're likely getting lucky the first time, since it isn't uncommon
for arrays to contain all 0's initially. If you alter your code thus,
while((sText != '\\'))
{
sTemp = sText;
i++;
} sTemp='\0';
printf("Name: %s\n",sNameBuff);


I predict your need and desire to "totally empty" the string will
disappear.

You have another potential problem in that you never check to make
sure you aren't writing beyond the end of sTemp. In your code
snippet, you were fine, but should a file name be longer than 50
characters, a calamity will likely result.
 
F

Fred L. Kleinschmidt

spike said:
My prygram goes through a string to find names between '\'-characters
The problem is, parts of the name in sTemp remains if the new name is
shorter than the old name.

code
---------------------------------------------------
char sTemp[50];
char sText[] = "THELONGESTNAME\\samantha\\gregor\\spike\\..."; // and
so on...

...
some code
...

int i=0;
while((sText != '\\'))
{
sTemp = sText;
i++;
}
printf("Name: %s\n",sNameBuff);
// Here i want to reset the string, totally empty it.
// this dosent do it.
sNameBuff == "";
// Start over again...
...
-------------------------

example output:
Name: THELONGESTNAME
Name: samanthaSTNAME
Name: gregorhaSTNAME
Name: spikerhaSTNAME

How can i make the string completely empty?


Assuming that you have determined that sTemp is long enough to hold the
string plus its trailing sero byte, just add the zero byte:

while((sText != '\\'))
{
sTemp = sText;
i++;
}
sTemp = '\0';
 
M

Martin Dickopp

Peter Hille said:
sTemp = '\0';

Inserting this would NULL terminate the string


`NULL' is a macro expanding to a null _pointer_ constant. ITYM "zero
terminate" or "terminte with a null character".

Martin
 
P

Peter Nilsson

Christopher Benson-Manica said:
spike <[email protected]> spoke thus:

Building on earlier replies...


// style comments are frowned on in this newsgroup, because few (if
any) compliant C99 compilers exist.

That's one issue, but you've missed the one that causes most angst. Take a
look at how the line has been wrapped. Even if a compiler supports //
comments, what is it to make of the wrapped text:

so on ...

Anyone copying the code portion of the post will have manually unwrap those
lines in order to get the code to compile. The _harder_ the OP makes it for
people to analyse the code, the less inclined people will be to help.
Be safe, be smart, use /* */ comments.
int i=0;
while((sText != '\\'))
{
sTemp = sText;
i++;
}
printf("Name: %s\n",sNameBuff);


I predict that sNameBuff is declared something like


My understanding of the word 'predict' is that you're fortelling a future
event, whereas your statement is actually 'guessing' an undisclosed event
which has (presumably) already occured.
char *sNameBuff=sTemp;

If that isn't correct, well, you should have posted its declaration
yourself. If it is correct, an earlier reply likely nailed your
problem - you aren't adding a NUL character at the end of sTemp.

It's a minor point, but a better term than NUL character is null character.
The standard defines the latter, the former is not mentioned in normative
text(even if it is intuitively obvious).

But if the purpose of the code is simply to print the path segments, as
delimited by '\\', then the code doesn't need to store a copy...

void breakup_path(const char *path)
{
const char *s = path, *e;

for (s = path; (e = strchr(s, '\\')) != NULL; s = e + 1)
{
int w = e - s;
printf("Name: \"%*.*s\"\n", w, w, s);
}

/* actual code might not require this line... */
if (*s) printf("Name: \"%s\"\n", s);
}

Even copying to a temporary space is relatively easy (although real code
would do some sanity checks)...

strncpy(temp, s, e-s)[e-s] = 0;
 
C

CBFalconer

Peter said:
My understanding of the word 'predict' is that you're fortelling
a future event, whereas your statement is actually 'guessing' an
undisclosed event which has (presumably) already occured.

It is common idiomatic usage. He is actually predicting that it
will be discovered that ... :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top