Thread: anouther probeblem about strings...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    anouther probeblem about strings...

    Ok I want it so that I can have a string in the code below but I want to put an already delcalred variable in it. This variable also has a word in it. (No spaces). As you can see I already tired something but it didn't work. How do you do this?


    Code:
    sfos.pFrom = "C:\\Documents and Settings\\"+name+"\\Cookies\\*.txt\0";

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    What didn't work about it? Post your errors and more of your code. Me thinks you just need to overload the + operator.

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Yes, posting more code is always a good idea.

    If sfos.pFrom and name are C++ strings (i.e. from the standard string class in <string>) then that code should work fine. If they are C style strings, then you should use strcat to concatenate it all together.

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    oh sorry. I was in a hurry. Well basicaly that is all the code you need to know. The rest of it works. And I guess it is a c++ string.

    Me thinks you just need to overload the + operator.
    Me thinks you are right. How do I do that. (I had one error thatsaid somthing about that).


    sfos.(what ever it was) was nto a string. It is part of the delete file code.


    here is an example that I am using to test with...

    Code:
    //Deletes the Cookies
        SHFILEOPSTRUCT sfos;
        sfos.hwnd = NULL;
        sfos.wFunc = FO_DELETE;
        sfos.pFrom = "C:\\Documents and Settings\\"+name+"\\Cookies\\*.txt\0";
        sfos.pTo = NULL;
        sfos.fFlags = FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT;
        sfos.fAnyOperationsAborted = error;
        sfos.hNameMappings = NULL;
        sfos.lpszProgressTitle = NULL;
    Last edited by Rune Hunter; 09-21-2004 at 05:27 PM.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    + is already overloaded for std::string, it cannot be overloaded for a pair of char*'s or any other POD (plain old data). You are going to have to be more specific about "not working". Does it compile, what string were you expecting, and whats with the \0? If you want a binary zero ebeded in a std::string you need to specify a length when you construct it. Obviously given only a char* you cannot know if the embeded binary zero is part of the string, or marks the end of the string.

    Code:
      std::string one("hi\0there"),two("hi\0there",8);
      std::cout << one << '\n' << two << std::endl;
    prints
    "hi"
    "hi there"

    ::EDIT::
    pFrom is almost certainly not a std::string, you will need to use strcpy, sorry. You do not need to embed a \0.

    fancy way to do this using std::strings
    Code:
    {
        std::string tmp = "C:\\Documents and Settings\\" + std::string(name) + "\\whatever.txt";
        sfos.pName = new char[tmp.size()+1];
        strcpy(sfos.pName,tmp.c_str());
    }
    note that if you had already done this, (assigned pFrom a result of new) you need to remember to delete [] first.
    Last edited by grib; 09-21-2004 at 05:34 PM.

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    oh the \0 has to be there. Sence this is a string to delete a file the \0 marks the end of the string for the delete file function. (I got it from msnd). And I am going to try working with the info you gave me. Thanks

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    oh and I'm jsut getting mroe confused now. It does NOT compile and I was expecting for it to find that string but the user defines the name variable before hand so it can find and delete the files. I get lots of errors about char* and other chars.

    174 C:\Documents and Settings\Alex\Desktop\Comp Cleaner (Alex).cpp cannot convert `std::string' to `const CHAR*' in assignment

    Hope that helps...

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
        SHFILEOPSTRUCT sfos;
        char szFrom[128];
        sprintf(szFrom,"C:\\Documents and Settings\\%s\\Cookies\\*.txt%c",name,0);
        sfos.hwnd = NULL;
        sfos.wFunc = FO_DELETE;
        sfos.pFrom = szFrom;
        sfos.pTo = NULL;
        sfos.fFlags = FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT;
        sfos.fAnyOperationsAborted = error;
        sfos.hNameMappings = NULL;
        sfos.lpszProgressTitle = NULL;

  9. #9
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    be sure to #include<string> for std::string and #include<string.h> for strcpy.
    You could also use #include<cstring> and add either using std::strcpy somewhere, or just type std::strcpy wherever I have just strcpy.

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    bithub points out a memory leak in my version, you need to call delete [] sfos.pFrom someday in my version, you also do indeed need the second null, bithubs version takes care of this nicely, to use my version you need to add space for the second zero and assign it manually.
    Code:
        sfos.pName = new char[tmp.length()+2];
        strcpy(sfos.pName,tmp.c_str());
        sfos.pName[tmp.length()+1] = '\0';
    Ugh, it's getting real ugly.

  11. #11
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    omg bithub you are the best! And grib thanks for helping too, altho your coode kept geting very messy to the point where I didn't understand it. I can now continue with my project!

    Thank you guys!

  12. #12
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    most welcome

  13. #13
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    oh sorry. I was in a hurry. Well basicaly that is all the code you need to know. The rest of it works.
    Well I'm glad your problem's solved, but in future you will want to avoid thinking in the above manner. I've noticed you're fairly secretive about your code in certain projects, but I can assure you that very rarely (or at least not always) is the problem actually in the line number specified in the error message. Let's say you had not declared the variable name. The first line in which you used it would yield an error, but the line by itself would appear to be syntactically correct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. anouther easy probeblem...
    By Rune Hunter in forum C++ Programming
    Replies: 10
    Last Post: 09-15-2004, 06:18 PM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM