How can I clear a string? and not just by making all the characters spaces.
Thanks
Printable View
How can I clear a string? and not just by making all the characters spaces.
Thanks
Do you mean like the whole screen?? If so then clr() will do it!Quote:
Originally posted by Jperensky
How can I clear a string? and not just by making all the characters spaces.
Thanks
Or,
This assumes, of course, that your string variable is named 'str'. Now, if it's named 'Rover'... ;)Code:std::string str = "Now is the time...";
std::cout << str << std:endl;
str = " ";
Note that your compiler will probably insist that you put a 'space' character between the quotes.
-Skipper
Assuming this is a std::string you can simply say:
str.erase();
If it's a C string you'll have to define "empty" better, for most uses doing this will cause the string to become empty:
str[0] = '\0';
-Prelude
Hi,
Do you mean a string std::string or an array of char's?
To clear an array of char's so that C sees an empty string just make the first element 0(zero).
char mystr[20] = "this is a test";
printf("\n%s",mystr); //prints this is a test
mystr[0] = 0;
printf("\n%s",mystr); //prints only the '/n'
Hope this helps.
its generally better to assign the element of the string to NULL rather than zero
>its generally better to assign the element of the string to NULL rather than zero
If you mean the nul character '\0' then I agree. If you mean the NULL macro defined in cstdio I would like to know why you think so.
-Prelude
Look at the definition of NULL (_null.h) and you'll be surprised :).Quote:
Originally posted by iain
its generally better to assign the element of the string to NULL rather than zero
I am using an array of chars not std::string and I tried _null.h but I am programming this to work under Linux so that isn't working.
I am looping threw a text file char by char storing each char to the next available spot (or cell) in the array once it reads a \n char it stops and I can process the string (array of chars) after that I need to "reset the string." The problem I am having is I can't get the string to clear. So when I rewrite over the char array if anyline previously stored was longer than anyone after I have the extra char(s) after the string (line) I am working with.
*Example*
The text file contains:
this is line one
and line 2
also 3
I am getting this when I print it to screen:
this is line one
and line 2 one
also 3ne 2 one
Anyone have a solution?
There is a function called strset() defined in string.h.
e.g.#include<string.h>
strset(samplestring,0);
This will set all characters in the string samplestring to the NULL character with the ASCII value 0.
You copy the string into the buffer one char at a time?Quote:
Originally posted by Jperensky
I am using an array of chars not std::string and I tried _null.h but I am programming this to work under Linux so that isn't working.
I am looping threw a text file char by char storing each char to the next available spot (or cell) in the array once it reads a \n char it stops and I can process the string (array of chars) after that I need to "reset the string." The problem I am having is I can't get the string to clear. So when I rewrite over the char array if anyline previously stored was longer than anyone after I have the extra char(s) after the string (line) I am working with.
*Example*
The text file contains:
this is line one
and line 2
also 3
I am getting this when I print it to screen:
this is line one
and line 2 one
also 3ne 2 one
Anyone have a solution?
That explains it. After you have copied all characters, add a '\0' character last. This is a NULL terminator, meaning the string ends there (not showing up the rest of the junk from the previous string).
When I append a '\0' char to the end of the array all I can see is the last char in the string... it is the correct char though.
Source:
Code:#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
#include <string.h>
void main()
{
ifstream OpenFile("text.txt");
char ch, temp_str[50];
int cord = 0, reset = 0;
while(!OpenFile.eof())
{
OpenFile.get(ch);
if (ch == '\n') {
cord++;
temp_str[cord] = '\0';
cout<<temp_str<<"\n";
}
else {
temp_str[cord] = ch;
cord++;
}
reset = 0;
cord = 0;
}
OpenFile.close();
cout<<temp_str;
}
You set the index to 0 in every loop, so you will never get further than the first element.
Quote:
Originally posted by Jperensky
Code:OpenFile.get(ch);
if (ch == '\n')
{
cord++;
temp_str[cord] = '\0';
cout<<temp_str<<"\n";
}
else
{
temp_str[cord] = ch;
cord++;
}
reset = 0;
cord = 0;
You said I set the index to '\0'. I set the last char in the string to '\0' as I was instructed to do in a previous post. How should I do it?
You should move the cord = 0; to the if statement:
If you read a newline character you display the string and reset the index (cord) to 0. Now you can read the next line starting at the beginning of your string.Code:if (ch == '\n') {
cord++;
temp_str[cord] = '\0';
cout<<temp_str<<"\n";
cord = 0;
}
else {
temp_str[cord] = ch;
cord++;
}
reset = 0;
This should work:
Code:#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
#include <string.h>
void main()
{
ifstream OpenFile("text.txt"); // Your text file.
char ch, temp_str[50];
int cord = 0, last_count = 0;
while(!OpenFile.eof())
{
OpenFile.get(ch);
if (ch == '\n') {
temp_str[cord] = '\0';
cout<<temp_str<<"\n"; /* Instead of printing it to your console you could process it however before preceding. */
cord = 0;
last_count = 0;
}
else {
temp_str[cord] = ch;
cord++;
last_count++;
}
}
OpenFile.close();
last_count = last_count - 1;
temp_str[last_count] = '\0';
cout<<temp_str<<endl; /* Same as line 23 (to process the current string) just for the last line in the text file since there is no '\n' at the end of the line. */
}
Eh...Quote:
Originally posted by Staz
void main()
*quietly walking away, avoiding the wraith of some of the older members here...*
>*quietly walking away, avoiding the wraith of some of the older members here...*
Any two, or three, in particular who come to mind? :D
* curled up in foxhole awaiting 'incoming' *
-Skipper
Quote:
Originally posted by Staz
...
int main()
...