Hey guys, i was making a program and i came across an instance where i need to write characters to a string, check them, then delete them and start over again about 25 times. Any ideas on how to clear the contents of a string? Thanks Alot!!
Printable View
Hey guys, i was making a program and i came across an instance where i need to write characters to a string, check them, then delete them and start over again about 25 times. Any ideas on how to clear the contents of a string? Thanks Alot!!
stringname.empty();
Didn't work:
request for member `empty' in `stringname', which is of non-aggregate type `char[50]'
it is a character array, im using DEV-C++. If that helps...
Once again thanks alot!
Well if it is an actual string it will be terminated by the null '/0' character. So maybe you could loop through your string and zero the values until you came across the '\0' character.
If you are using C++ you really should be using the string type provided with the STL. Here is a FAQ that demonstrates the use of C++ strings. Let me know if you have any questions.
pssst its '\0' not '/0'
D'oh! :p Thanx for the catch. I fixed my post.
-Andy
Or you could just change the first element to '\0'
>>Any ideas on how to clear the contents of a string?
Well, this has been asked and answered before, but the very very simplest way is to simply set the first element of the string to '\0', indicating that the string terminates at the first character and therefore has length 0. Examples of how to do this:
theString[0] = '\0';
theString[0] = 0;
*(theString) = '\0';
*(theString) = 0;
strcpy(theString, "");
memcpy(theString, "", 1);
memset(theString, 0, 1);
memset(theString, 0, THE_STRINGS_LENGTH);
etc. etc. etc.
Alternately if you're using a C++ string (std::string), you'd just do:
theString = ""; //But you can't do this with a char* or char[]!!!
**Notice: Edited, as someone pointed out I was looking for memset() - not memcpy(). Incidentally though, memcpy() happens to work as well with little modification :)
Thanks alot guys!
Alright now the checkfile works but i cant get the program to work, any fixes or suggestions you guys can find would be appreciated.
Code:char filename[50];
for(int d=64; d<90; d++)
{
for(int r=0; r<50; r++)
{
filename[r]='\0';
}
(char)d += filename;
strcat(filename, ":/login/enter.txt");
ifstream fin(filename);
if(!fin.is_open())
{
cout<<"Drive "<<(char)d<<" Doesn't Have Required Information.";
cout<<"\n";
fin.close();
continue;
}
else{
cout<<"Drive "<<(char)d<<"Has Required Log On Files.\n";
cout<<"Logging In...";
break;
}
}
}
Thanks!
Code:/* LIMITATION: Will only work on ascii systems. In this case,
* the rest of the code is for a specific platform as well, so we
* don't mind too much. */
for(char d = 'A'; d <= 'Z'; d++)
{
/* Only need to set the first character
* to nul to create an empty string. */
filename[0] = '\0';
/* Use sprintf to create our filename string. */
sprintf(filename, "%c:/login/enter.txt", d);
ifstream fin(filename);
>>(char)d += filename;
What this does is take the numeric value of d, truncate it to the maximum size of a char, and add the value of the address of the first character in filename[]. What I think you're really looking for is stringstream:
Code:#include <sstream>
...
std::ostringstream theStream;
theStream << (char)d << ":/login/enter.txt" << std::flush;
std::string fileName = theStream.str();
std::ifstream fin(fileName.c_str());
Isn't the result of a cast an r-value, unless the target type is a reference?
Whoops, you're right. Well, assuming it ended up as an l-value then ;)
> /* Only need to set the first character to nul to create an empty string. */
> filename[r]='\0';
Slight typo:
filename[0]='\0';