Thread: Clearing a string...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    Question Clearing a string...

    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!!

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    stringname.empty();
    Last edited by MadCow257; 02-07-2005 at 08:01 PM.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    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!

  4. #4
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    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

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    pssst its '\0' not '/0'

  6. #6
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    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

  7. #7
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Or you could just change the first element to '\0'

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>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.

  9. #9
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Thanks alot guys!

  10. #10
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    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!

  11. #11
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    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.

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>(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.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

  14. #14
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    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.

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > /* Only need to set the first character to nul to create an empty string. */
    > filename[r]='\0';
    Slight typo:
    filename[0]='\0';

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM