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!!
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();
Last edited by MadCow257; 02-07-2005 at 08:01 PM.
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.
Last edited by andyhunter; 02-07-2005 at 08:36 PM.
i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced
It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah
Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem
D'oh! Thanx for the catch. I fixed my post.
-Andy
i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced
It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah
Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem
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
Last edited by Hunter2; 02-07-2005 at 10:23 PM.
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
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);
Last edited by anonytmouse; 02-08-2005 at 02:16 PM. Reason: Typo pointed out by swoopy. Thanks.
>>(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());
Last edited by Hunter2; 02-07-2005 at 10:31 PM.
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
Isn't the result of a cast an r-value, unless the target type is a reference?
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Whoops, you're right. Well, assuming it ended up as an l-value then
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
> /* Only need to set the first character to nul to create an empty string. */
> filename[r]='\0';
Slight typo:
filename[0]='\0';