text to LPTSTR?

This is a discussion on text to LPTSTR? within the C++ Programming forums, part of the General Programming Boards category; I have a function that takes 'LPTSTR' as one of its input, but I am having a hard time converting ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    text to LPTSTR?

    I have a function that takes 'LPTSTR' as one of its input, but I am having a hard time
    converting a string into it. What I need to do is create a string that holds the path to
    a folder with a number at the end.

    Like this:
    int n = 0;
    string path = "c:\\myData\\Image_";
    path += intToString(n++);
    path += ".bmp";

    But I am then unable to convert path into a 'LPTSTR'.

    If it matters, the function I am trying to call is this one ( it works with the TEXT() )
    capFileSaveDIB(hWndC1,TEXT("C:\\folder\\test.bmp") );

    Any ideas?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    c_str() of a std::string should give you a C style string.


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Thank you fr your reply, but I have already tried that. It does not give an error, but it does not work (no file is saved)

    Here is what I am "using"
    Code:
    std::string path = "C:\\Users\\Ole\\Documents\\test_";
    std::stringstream temp;
    temp << iter++;
    path += temp.str();
    path += ".bmp";
    
    capFileSaveDIB(hWndC1, path.c_str());

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by h3ro View Post
    Thank you fr your reply, but I have already tried that. It does not give an error, but it does not work (no file is saved)

    Here is what I am "using"
    Code:
    std::string path = "C:\\Users\\Ole\\Documents\\test_";
    std::stringstream temp;
    temp << iter++;
    path += temp.str();
    path += ".bmp";
    
    capFileSaveDIB(hWndC1, path.c_str());
    more efficient to build the whole string with stringstream:
    Code:
    std::stringstream temp;
    temp <<  "C:\\Users\\Ole\\Documents\\test_" <<  iter++ << ".bmp";
    std::string path (temp.str());
    LPTSTR - is pointer to buffer of TCHAR
    so when compiling in UNICODE - you get the wrong string...

    you could use
    std::wstringstream and std::wstring in this case

    or if you want to have a code compilable two ways - use template version
    basic_stringstream <TCHAR> and basic_string<TCHAR>
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Thanks vart.

    I just ended up with turning off UNICODE.

    Why do Microsoft (and probably a lot of others) insist on creating new variable types for everything?

  6. #6
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    Simply because they can --- they are the big dog on the block!

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by kcpilot View Post
    Simply because they can --- they are the big dog on the block!
    I'd say it's more to do with the multiple language support. Unicode (in it's 16-bit character form) is much more suitable to asian, cyrillic and arabic languages [to give but a few examples], whilst the standard ANSI/ASCII is just barely able to support most European languages.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by matsp View Post
    I'd say it's more to do with the multiple language support. Unicode (in it's 16-bit character form) is much more suitable to Asian, Cyrillic and Arabic languages [to give but a few examples], whilst the standard ANSI/ASCII is just barely able to support most European languages.
    Exactly, while you might not care that your software can run on any PC, no matter what language, for real businesses this is often essential. Plus it's a real pain to convert to Unicode later on, when you suddenly realize that you need it after all.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  9. #9
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    In this case, it's not a new variable type. It's a typedef (or #define) for char* or wchar*, depending on the UNICODE flag. This way, you can compile your program in UNICODE or ANSI, by just setting a single flag. Imagine the amount of work if you had to replace every single instance of "char" in your code with "wchar".
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    I don't think Microsoft created unicode, and I don't know if Microsoft was the one to set a "de facto" standard for mapping wide -> unicode in compilers targeting PC x86/x64, so the blame is misplaced.
    I really think the lack is for teaching who tend to teach char by default, when they really should be teaching students to use wchar.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble drawing text xlib.
    By Qui in forum Linux Programming
    Replies: 1
    Last Post: 05-10-2006, 12:07 PM
  2. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 03:49 PM
  3. Appending text to an edit control
    By dit6a9 in forum Windows Programming
    Replies: 3
    Last Post: 08-13-2004, 09:52 PM
  4. Scrolling The Text
    By GaPe in forum C Programming
    Replies: 3
    Last Post: 07-14-2002, 04:33 PM
  5. Replies: 1
    Last Post: 07-13-2002, 05:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21