473,405 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

reset a string. empty it totally.

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[i] != '\\'))
{
sTemp[i] = sText[i];
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?
Nov 14 '05 #1
8 2150

"spike" <jo**@ljungh.se> wrote in message
news:66**************************@posting.google.c om...
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[i] != '\\'))
{
sTemp[i] = sText[i];
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
Nov 14 '05 #2
Mike Wahler wrote:
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
rg********@pobox.com
Remove the 'g' from my address to send me mail.

Nov 14 '05 #3
Hi,

Am Fri, 05 Mar 2004 13:58:28 -0800 schrieb spike:

int i=0;
while((sText[i] != '\\'))
{
sTemp[i] = sText[i];
i++;
}
sTemp[i] = '\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...
...
-------------------------
How can i make the string completely empty?


The sTemp[i] 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

Nov 14 '05 #4
spike <jo**@ljungh.se> spoke thus:

Building on earlier replies...
code
---------------------------------------------------
char sTemp[50];
char sText[] = "THELONGESTNAME\\samantha\\gregor\\spike\\..." ; // and
so on...
// 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[i] != '\\'))
{
sTemp[i] = sText[i];
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[i] != '\\'))
{
sTemp[i] = sText[i];
i++;
} sTemp[i]='\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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #5


spike wrote:

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[i] != '\\'))
{
sTemp[i] = sText[i];
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[i] != '\\'))
{
sTemp[i] = sText[i];
i++;
}
sTemp[i] = '\0';

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225
Nov 14 '05 #6
Peter Hille <pe*********@aix-user-group.de> writes:
sTemp[i] = '\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
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #7
"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:c2**********@chessie.cirr.com...
spike <jo**@ljungh.se> spoke thus:

Building on earlier replies...
code
---------------------------------------------------
char sTemp[50];
char sText[] = "THELONGESTNAME\\samantha\\gregor\\spike\\..." ; // and
so on...
// 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[i] != '\\'))
{
sTemp[i] = sText[i];
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;

--
Peter
Nov 14 '05 #8
Peter Nilsson wrote:
"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote
.... snip ...
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.


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

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ryan Stewart | last post by:
Is there a way to reset the style property of an HTML element to some default or to all empty values? I have a group of elements whose style settings may be changed arbitrarily at certain points in...
22
by: spike | last post by:
How do i reset a string? I just want to empty it som that it does not contain any characters Say it contains "hello world" at the time... I want it to contain "". Nothing that is.. Thanx
4
by: J. Oliver | last post by:
I am attempting to copy a portion of a string into another string. Say I have "abcdef" in string 'a' and want to copy only "bcd" into a new string 'b'. The following function came to mind: ...
1
by: NancyASAP | last post by:
Thought I'd share this since it took me a long time to get it working. Thanks to a bunch of contributers in Google Groups who shared javascript, etc. The question was: How can I put a reset...
3
by: Jensen bredal | last post by:
hmtl reset does not seem to work or i'm i doing something wrong? MT JB
4
by: Lee Chapman | last post by:
Hi, Can anyone tell me why in the code below, the call to ClearChildViewState() has no effect? To paraphrase the code: I'm using view state. I have a textbox and a submit button (and a label...
6
by: Matt | last post by:
I'm not entirely sure how to describe this issue. I have a number of ComboBoxes in my application which have their text properties bound to a field in a data set. The items loaded in the ComboBox...
7
by: Kermit Piper | last post by:
Hello, How can you clear session variables when a reset button is pressed? I thought I might be able to do something like: <% If request.form("Reset") = "Reset" then Session("variable") =...
3
by: radix | last post by:
Hello, I have a aspx page with ajax scriptmanger update panel etc. I also have public string variables on the aspx page. Whenever postback happens from Ajax update panel, at server side all...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.