Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. convert 'char *' to 'LPCTSTR'

convert 'char *' to 'LPCTSTR'

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
14 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Kenan Aksoy

    I think you should use swprintf instead of stprintf

    dlfkgj lsdfkglfkgjlfgl jldfk fldkk jk

    C Offline
    C Offline
    Cedric Moonen
    wrote on last edited by
    #5

    No, stprintf is a macro that resolves to sprintf if UNICODE is not declared or to swprintf if UNICODE is declared. So your code still compiles even if you change unicode settings.


    Cédric Moonen Software developer
    Charting control [v1.2]

    1 Reply Last reply
    0
    • K krmed

      Since len is apparently the length of a file, you won't need to allocate your buffer that big, but you will need enough characters to hold all the digits.

      int len = finder.GetLength();
      TCHAR *len_str = new TCHAR[(int)(log10(len) + 2)]; // leave one extra character for null terminator
      _stprintf(len_str,_T("%d"),(int)(log10(len) + 2));
      m_Files.SetItemText(i,1,len_str);
      delete[] len_str;

      Hope that helps.

      Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #6

      Yes that's right, I didn't think too much before posting my reply :-D. But you should print the len in the string, not the number of characters ;P


      Cédric Moonen Software developer
      Charting control [v1.2]

      K 1 Reply Last reply
      0
      • K krmed

        Since len is apparently the length of a file, you won't need to allocate your buffer that big, but you will need enough characters to hold all the digits.

        int len = finder.GetLength();
        TCHAR *len_str = new TCHAR[(int)(log10(len) + 2)]; // leave one extra character for null terminator
        _stprintf(len_str,_T("%d"),(int)(log10(len) + 2));
        m_Files.SetItemText(i,1,len_str);
        delete[] len_str;

        Hope that helps.

        Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #7

        I understand the point, but maybe it would be nice to avoid both continuos allocation/deallocation of memory and use of the log10. Simply allocate once a (relatively) big buffer on the stack.

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

        In testa che avete, signor di Ceprano?

        K 1 Reply Last reply
        0
        • N neha agarwal27

          Hi all, I am trying to print size of my file in a list control using this code

          int len = finder.GetLength();
          char *len_str = new char;
          sprintf(len_str,"%d",len);
          m_Files.SetItemText(i,1,len_str);

          but it gives this error error C2664: 'CListCtrl::SetItemText' : cannot convert parameter 3 from 'char *' to 'LPCTSTR' i am using VC-2005 can anybosy tell me how to resolve this problem

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #8

          neha.agarwal27 wrote:

          char *len_str = new char; sprintf(len_str,"%d",len);

          Since you are using MFC:

          CString len_str;
          len_str.Format("%d", len);


          "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          1 Reply Last reply
          0
          • CPalliniC CPallini

            I understand the point, but maybe it would be nice to avoid both continuos allocation/deallocation of memory and use of the log10. Simply allocate once a (relatively) big buffer on the stack.

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

            K Offline
            K Offline
            krmed
            wrote on last edited by
            #9

            Well, perhaps that would be ok, but if the file size is large (2 GB) the new of a 2GB char array would probably fail, and you don't need anywhere near that many characters to hold a file size. Perhaps a fixed buffer size of say [MAX_PATH_LEN] (which is 256) would be more than enough characters to hold any file size.

            Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

            CPalliniC 1 Reply Last reply
            0
            • K krmed

              Well, perhaps that would be ok, but if the file size is large (2 GB) the new of a 2GB char array would probably fail, and you don't need anywhere near that many characters to hold a file size. Perhaps a fixed buffer size of say [MAX_PATH_LEN] (which is 256) would be more than enough characters to hold any file size.

              Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #10

              krmed wrote:

              Well, perhaps that would be ok, but if the file size is large (2 GB) the new of a 2GB char array would probably fail, and you don't need anywhere near that many characters to hold a file size.

              Of course the buffer can't be sized equal to file dimension, have you missed my aknowledge about your point? That stated, my post completely agree with your bottom line:

              krmed wrote:

              Perhaps a fixed buffer size of say [MAX_PATH_LEN] (which is 256) would be more than enough characters to hold any file size.

              :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

              In testa che avete, signor di Ceprano?

              K 1 Reply Last reply
              0
              • CPalliniC CPallini

                krmed wrote:

                Well, perhaps that would be ok, but if the file size is large (2 GB) the new of a 2GB char array would probably fail, and you don't need anywhere near that many characters to hold a file size.

                Of course the buffer can't be sized equal to file dimension, have you missed my aknowledge about your point? That stated, my post completely agree with your bottom line:

                krmed wrote:

                Perhaps a fixed buffer size of say [MAX_PATH_LEN] (which is 256) would be more than enough characters to hold any file size.

                :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                K Offline
                K Offline
                krmed
                wrote on last edited by
                #11

                Sorry if you thought I meant that all at you. Actually, I was agreeing with the fixed size, but wanted the OP to realize that he probably can't new a char array the size of the file length (and it wouldn't be needed anyway). I was simply suggesting that a fixed size (like you said) would work, and MAX_PATH_LEN would be more than enough - in fact 20 characters will hold more than ever needed. Have a great day!

                Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

                1 Reply Last reply
                0
                • C Cedric Moonen

                  Yes that's right, I didn't think too much before posting my reply :-D. But you should print the len in the string, not the number of characters ;P


                  Cédric Moonen Software developer
                  Charting control [v1.2]

                  K Offline
                  K Offline
                  krmed
                  wrote on last edited by
                  #12

                  You're right about the len in the printed string - my turn to not think clearly about what he's doing!

                  Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

                  1 Reply Last reply
                  0
                  • N neha agarwal27

                    Hi all, I am trying to print size of my file in a list control using this code

                    int len = finder.GetLength();
                    char *len_str = new char;
                    sprintf(len_str,"%d",len);
                    m_Files.SetItemText(i,1,len_str);

                    but it gives this error error C2664: 'CListCtrl::SetItemText' : cannot convert parameter 3 from 'char *' to 'LPCTSTR' i am using VC-2005 can anybosy tell me how to resolve this problem

                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #13

                    and in addition to DavidCrow's reply... Since you're using MFC and a CString, which is a generic string type, use generic literals as well...

                    CString len_str;
                    len_str.Format(_T("%d"), len);

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    1 Reply Last reply
                    0
                    • N neha agarwal27

                      Hi all, I am trying to print size of my file in a list control using this code

                      int len = finder.GetLength();
                      char *len_str = new char;
                      sprintf(len_str,"%d",len);
                      m_Files.SetItemText(i,1,len_str);

                      but it gives this error error C2664: 'CListCtrl::SetItemText' : cannot convert parameter 3 from 'char *' to 'LPCTSTR' i am using VC-2005 can anybosy tell me how to resolve this problem

                      H Offline
                      H Offline
                      Hamid Taebi
                      wrote on last edited by
                      #14

                      You can assign your value to control with Set SetDlgItemInt and you dont need to other variables.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups