473,796 Members | 2,658 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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\\gre gor\\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 2178

"spike" <jo**@ljungh.se > wrote in message
news:66******** *************** ***@posting.goo gle.com...
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\\gre gor\\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********@pobo x.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\\gre gor\\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=sTem p;

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)cybers pace.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\\gre gor\\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*********@ai x-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
"Christophe r Benson-Manica" <at***@nospam.c yberspace.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\\gre gor\\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=sTem p;

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(co nst 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:
"Christophe r Benson-Manica" <at***@nospam.c yberspace.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********@yah oo.com) (cb********@wor ldnet.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
9558
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 a script (maybe set the background color and font weight of one element and the text align of another), but at another point I want to undo all of these changes and go back to the original state (which is actually all empty styles). Is there a...
22
17205
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
11899
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: public string GetText(string a, int start, int end) { int i; string b;
1
14384
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 button on my ASP.NET web page, and have an HTML reset button click clear 1) all validator text display and 2) validation summary display. Problem was clearing them and yet still leaving them working and visible if the user immediately began...
3
1300
by: Jensen bredal | last post by:
hmtl reset does not seem to work or i'm i doing something wrong? MT JB
4
5334
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 that can be ignored). When I press the button the first time, the click handler hides the textbox. Pressing the button a second time unhides the textbox. The text box is maintaining its value when hidden via view state. (The value is NOT being...
6
4915
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 are not data bound (they just use the built in collection property of the ComboBox), and they are all set to use the DropDownList style. When moving from record to record via a BindingNavigator or a DataGridView (in master/detail format), the text...
7
14643
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") = Null %>
3
7832
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 the public string variables are reset to empty string. As if fresh page is loaded. How to preserve the page level variables during Ajax postback?
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9533
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10461
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10239
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10190
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9057
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6796
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.