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. CString to Char array

CString to Char array

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structures
19 Posts 6 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.
  • V Offline
    V Offline
    VinayCool
    wrote on last edited by
    #1

    Hi, I have variable FName & Str, CString FName; char str[100]; i want the FName to be stored in str i tryed using str = FName; i am getting error error C2440: '=' : cannot convert from 'class CString' to 'char [100]' Can anyone help me with this convershion or any related notes or links will be helpfull. Regards, Vinay Charan.

    K L H V K 5 Replies Last reply
    0
    • V VinayCool

      Hi, I have variable FName & Str, CString FName; char str[100]; i want the FName to be stored in str i tryed using str = FName; i am getting error error C2440: '=' : cannot convert from 'class CString' to 'char [100]' Can anyone help me with this convershion or any related notes or links will be helpfull. Regards, Vinay Charan.

      K Offline
      K Offline
      kakan
      wrote on last edited by
      #2

      Some alternatives: strcpy(str, (LPCTSTR) FName); strcpy(str, FName.GetBuffer(0)); OR: char * pStr = (LPCTSTR) FName; -- modified at 1:52 Tuesday 23rd May, 2006

      1 Reply Last reply
      0
      • V VinayCool

        Hi, I have variable FName & Str, CString FName; char str[100]; i want the FName to be stored in str i tryed using str = FName; i am getting error error C2440: '=' : cannot convert from 'class CString' to 'char [100]' Can anyone help me with this convershion or any related notes or links will be helpfull. Regards, Vinay Charan.

        L Offline
        L Offline
        Laxman Auti
        wrote on last edited by
        #3

        Vinay wrote:

        Hi,I have variable FName & Str, CString FName; char str[100]; i want the FName to be stored in str i tryed using str = FName; i am getting error error C2440: '=' : cannot convert from 'class CString' to 'char [100]' Can anyone help me with this convershion or any related notes or links will be helpfull.

        See the following code

        strcpy(str,FName.GetBuffer(0));
        FName.ReleaseBuffer();

        Hope this helps you :) Knock out 't' from can't, You can if you think you can :cool:

        V 1 Reply Last reply
        0
        • V VinayCool

          Hi, I have variable FName & Str, CString FName; char str[100]; i want the FName to be stored in str i tryed using str = FName; i am getting error error C2440: '=' : cannot convert from 'class CString' to 'char [100]' Can anyone help me with this convershion or any related notes or links will be helpfull. Regards, Vinay Charan.

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

          use CString FName123; char str123[100]; FName123="test"; wsprintf(str123,"%s",FName123); -------------------------- or str123[0]='\0'; strcat(str123,FName123);_**


          **_

          whitesky


          1 Reply Last reply
          0
          • V VinayCool

            Hi, I have variable FName & Str, CString FName; char str[100]; i want the FName to be stored in str i tryed using str = FName; i am getting error error C2440: '=' : cannot convert from 'class CString' to 'char [100]' Can anyone help me with this convershion or any related notes or links will be helpfull. Regards, Vinay Charan.

            V Offline
            V Offline
            VinayCool
            wrote on last edited by
            #5

            Hi, I have used this code its workin thank you very much strcpy(str, (LPCTSTR) FName); strcpy(str, FName.GetBuffer(100));

            S 1 Reply Last reply
            0
            • V VinayCool

              Hi, I have used this code its workin thank you very much strcpy(str, (LPCTSTR) FName); strcpy(str, FName.GetBuffer(100));

              S Offline
              S Offline
              Stephen Hewitt
              wrote on last edited by
              #6

              Just this will do: strcpy(str, FName); You don't need the (LPCTSTR) cast; it will be called automatically. Having a cast you don't need (especially c-style casts, which should never be used in a C++ program) is at best redundant and can even be dangerous. Calling GetBuffer is not needed and should not be done without a matching call to ReleaseBuffer anyway. Steve

              1 Reply Last reply
              0
              • L Laxman Auti

                Vinay wrote:

                Hi,I have variable FName & Str, CString FName; char str[100]; i want the FName to be stored in str i tryed using str = FName; i am getting error error C2440: '=' : cannot convert from 'class CString' to 'char [100]' Can anyone help me with this convershion or any related notes or links will be helpfull.

                See the following code

                strcpy(str,FName.GetBuffer(0));
                FName.ReleaseBuffer();

                Hope this helps you :) Knock out 't' from can't, You can if you think you can :cool:

                V Offline
                V Offline
                VinayCool
                wrote on last edited by
                #7

                Hi Laxman, Do u remember yesterday i had asked u for changing the file path C:\folder\folder\file.ext to c:/folder/folder/file.ext now i have written a small program in which will change the input and get the output as c:/folder/folder/file.ext now i am facing one more problem file is not opening ShellExecute(NULL, "open", str,NULL, NULL, SW_SHOWNORMAL) is this the correct way to open the file is the code correct... str is of char array char str[100];

                L 1 Reply Last reply
                0
                • V VinayCool

                  Hi Laxman, Do u remember yesterday i had asked u for changing the file path C:\folder\folder\file.ext to c:/folder/folder/file.ext now i have written a small program in which will change the input and get the output as c:/folder/folder/file.ext now i am facing one more problem file is not opening ShellExecute(NULL, "open", str,NULL, NULL, SW_SHOWNORMAL) is this the correct way to open the file is the code correct... str is of char array char str[100];

                  L Offline
                  L Offline
                  Laxman Auti
                  wrote on last edited by
                  #8

                  Can you show me the code how you changed the string path? May be there was any bug i think code is correct. Knock out 't' from can't, You can if you think you can :cool:

                  V 1 Reply Last reply
                  0
                  • L Laxman Auti

                    Can you show me the code how you changed the string path? May be there was any bug i think code is correct. Knock out 't' from can't, You can if you think you can :cool:

                    V Offline
                    V Offline
                    VinayCool
                    wrote on last edited by
                    #9

                    Hi Laxman, below code changes c:\folder\folder\file1.txt to c:/folder/folder/file1.txt void CSearchDlg::OnDblclkSout() { // TODO: Add your control notification handler code here // Code to open the selected file. int d; d=m_SOUT.GetCurSel(); CString FName; m_SOUT.GetText(d,FName); char str[100]; int i,l,k,j,m; strcpy(str, FName); for (i=0;str[i]!='\0';i++) { if (str[i]=='\\') { l=i; k=0; char str1[100]; if (str[l+1]=='\0') { str[l]='/'; } else { for (j=l+1;str[j]!='\0';j++) { str1[k]=str[j]; k++; } str1[k]='\0'; str[l]='/'; for (m=0;str1[m]!='\0';m++) { str[++l]=str1[m]; } } str[++l]='\0'; i=i++; } } m_ctrlEDIT.SetWindowText(str); ShellExecute(NULL, "open", str ,NULL, NULL, SW_SHOWNORMAL); }

                    L 1 Reply Last reply
                    0
                    • V VinayCool

                      Hi Laxman, below code changes c:\folder\folder\file1.txt to c:/folder/folder/file1.txt void CSearchDlg::OnDblclkSout() { // TODO: Add your control notification handler code here // Code to open the selected file. int d; d=m_SOUT.GetCurSel(); CString FName; m_SOUT.GetText(d,FName); char str[100]; int i,l,k,j,m; strcpy(str, FName); for (i=0;str[i]!='\0';i++) { if (str[i]=='\\') { l=i; k=0; char str1[100]; if (str[l+1]=='\0') { str[l]='/'; } else { for (j=l+1;str[j]!='\0';j++) { str1[k]=str[j]; k++; } str1[k]='\0'; str[l]='/'; for (m=0;str1[m]!='\0';m++) { str[++l]=str1[m]; } } str[++l]='\0'; i=i++; } } m_ctrlEDIT.SetWindowText(str); ShellExecute(NULL, "open", str ,NULL, NULL, SW_SHOWNORMAL); }

                      L Offline
                      L Offline
                      Laxman Auti
                      wrote on last edited by
                      #10

                      Try the following code as its tested and works fine. CString str; m_SOUT.GetText(m_SOUT.GetCurSel(),str); ShellExecute(m_hWnd,NULL,str, NULL, NULL, SW_SHOWNORMAL); m_ctrlEDIT.SetWindowText(str); Hope this solve your problem. Knock out 't' from can't, You can if you think you can :cool:

                      V 1 Reply Last reply
                      0
                      • L Laxman Auti

                        Try the following code as its tested and works fine. CString str; m_SOUT.GetText(m_SOUT.GetCurSel(),str); ShellExecute(m_hWnd,NULL,str, NULL, NULL, SW_SHOWNORMAL); m_ctrlEDIT.SetWindowText(str); Hope this solve your problem. Knock out 't' from can't, You can if you think you can :cool:

                        V Offline
                        V Offline
                        VinayCool
                        wrote on last edited by
                        #11

                        Hi Laxman, I tryed the above code it has no effect ..file does not open m_ctrlEDIT.SetWindowText(str); i have used this statment to just to check the value of str str was value was like C:\Box01/00000304/00000034.txt as u said it cant open \ right ???

                        L 1 Reply Last reply
                        0
                        • V VinayCool

                          Hi Laxman, I tryed the above code it has no effect ..file does not open m_ctrlEDIT.SetWindowText(str); i have used this statment to just to check the value of str str was value was like C:\Box01/00000304/00000034.txt as u said it cant open \ right ???

                          L Offline
                          L Offline
                          Laxman Auti
                          wrote on last edited by
                          #12

                          vinaycool wrote:

                          C:\Box01/00000304/00000034.txt

                          How this is possible? Your path in list box should be either C:/Box01/00000304/00000034.txt or C:\Box01\00000304\00000034.txt i think you are not inserting the data into list box correctly.. have you? Knock out 't' from can't, You can if you think you can :cool:

                          V 1 Reply Last reply
                          0
                          • L Laxman Auti

                            vinaycool wrote:

                            C:\Box01/00000304/00000034.txt

                            How this is possible? Your path in list box should be either C:/Box01/00000304/00000034.txt or C:\Box01\00000304\00000034.txt i think you are not inserting the data into list box correctly.. have you? Knock out 't' from can't, You can if you think you can :cool:

                            V Offline
                            V Offline
                            VinayCool
                            wrote on last edited by
                            #13

                            Hi, In the list box items are like C:\Box01/00000304/00000034.txt C:\Box01/00000304/00000035.txt C:\Box01/00000304/00000036.txt but after the function i get the value in the str as C:/Box01/00000304/00000034.txt since the path is correct with file name it should open right ??

                            L 1 Reply Last reply
                            0
                            • V VinayCool

                              Hi, In the list box items are like C:\Box01/00000304/00000034.txt C:\Box01/00000304/00000035.txt C:\Box01/00000304/00000036.txt but after the function i get the value in the str as C:/Box01/00000304/00000034.txt since the path is correct with file name it should open right ??

                              L Offline
                              L Offline
                              Laxman Auti
                              wrote on last edited by
                              #14

                              vinaycool wrote:

                              In the list box items are like C:\Box01/00000304/00000034.txt C:\Box01/00000304/00000035.txt C:\Box01/00000304/00000036.txt but after the function i get the value in the str as C:/Box01/00000304/00000034.txt since the path is correct with file name it should open right ??

                              Can you post the block for insertion of the data into list box ? and Do you want to open only .txt files? Knock out 't' from can't, You can if you think you can :cool:

                              V 1 Reply Last reply
                              0
                              • L Laxman Auti

                                vinaycool wrote:

                                In the list box items are like C:\Box01/00000304/00000034.txt C:\Box01/00000304/00000035.txt C:\Box01/00000304/00000036.txt but after the function i get the value in the str as C:/Box01/00000304/00000034.txt since the path is correct with file name it should open right ??

                                Can you post the block for insertion of the data into list box ? and Do you want to open only .txt files? Knock out 't' from can't, You can if you think you can :cool:

                                V Offline
                                V Offline
                                VinayCool
                                wrote on last edited by
                                #15

                                Hi, code for inseting file names to list box & i have open only txt files only. void CSearchDlg :: disp () { int i = 0; while (strlen (shWord[i].file_name) > 1) { m_SOUT.AddString(shWord[i].file_name); int k = 0; TCHAR buff[100]; strcpy(buff,"\0"); while (shWord[i].w_offset[k] != -1) { TCHAR buffer[50]; int f=shWord[i].w_offset[k]; _itoa(f,buffer,10); strcat(buff,buffer); strcat(buff,","); k++; } buff[strlen(buff)-1]='\0'; m_SOUT.AddString(buff); i++; } }

                                L 1 Reply Last reply
                                0
                                • V VinayCool

                                  Hi, code for inseting file names to list box & i have open only txt files only. void CSearchDlg :: disp () { int i = 0; while (strlen (shWord[i].file_name) > 1) { m_SOUT.AddString(shWord[i].file_name); int k = 0; TCHAR buff[100]; strcpy(buff,"\0"); while (shWord[i].w_offset[k] != -1) { TCHAR buffer[50]; int f=shWord[i].w_offset[k]; _itoa(f,buffer,10); strcat(buff,buffer); strcat(buff,","); k++; } buff[strlen(buff)-1]='\0'; m_SOUT.AddString(buff); i++; } }

                                  L Offline
                                  L Offline
                                  Laxman Auti
                                  wrote on last edited by
                                  #16

                                  while (shWord[i].w_offset[k] != -1)
                                  {
                                  TCHAR buffer[50];
                                  int f=shWord[i].w_offset[k];
                                  _itoa(f,buffer,10);
                                  strcat(buff,buffer);
                                  strcat(buff,",");
                                  k++;
                                  }

                                  try this:

                                  while (shWord[i].w_offset[k] != -1)
                                  {
                                  sprintf(buff,"%s%d,",buff,shWord[i].w_offset[k]);
                                  k++;
                                  }

                                  vinaycool wrote:

                                  m_SOUT.AddString(shWord[i].file_name);

                                  what is datatype of file_name? Knock out 't' from can't, You can if you think you can :cool:

                                  V 1 Reply Last reply
                                  0
                                  • L Laxman Auti

                                    while (shWord[i].w_offset[k] != -1)
                                    {
                                    TCHAR buffer[50];
                                    int f=shWord[i].w_offset[k];
                                    _itoa(f,buffer,10);
                                    strcat(buff,buffer);
                                    strcat(buff,",");
                                    k++;
                                    }

                                    try this:

                                    while (shWord[i].w_offset[k] != -1)
                                    {
                                    sprintf(buff,"%s%d,",buff,shWord[i].w_offset[k]);
                                    k++;
                                    }

                                    vinaycool wrote:

                                    m_SOUT.AddString(shWord[i].file_name);

                                    what is datatype of file_name? Knock out 't' from can't, You can if you think you can :cool:

                                    V Offline
                                    V Offline
                                    VinayCool
                                    wrote on last edited by
                                    #17

                                    Hi Laxman, I got the solution, the problem was some junk character was getting added to the file name,so it was not able to open file,i just altered the file name removed the last junk character str[i-1]='\0';now the file open properly... Since its in the list box user keep on cliking on items and open any number of files but i want only file to be open at a time.. when the user clicks on the item i want the open file to closed(txt files) and slected file to opened is there any way to close and then open a file ?? please let know if there any way

                                    L 1 Reply Last reply
                                    0
                                    • V VinayCool

                                      Hi Laxman, I got the solution, the problem was some junk character was getting added to the file name,so it was not able to open file,i just altered the file name removed the last junk character str[i-1]='\0';now the file open properly... Since its in the list box user keep on cliking on items and open any number of files but i want only file to be open at a time.. when the user clicks on the item i want the open file to closed(txt files) and slected file to opened is there any way to close and then open a file ?? please let know if there any way

                                      L Offline
                                      L Offline
                                      Laxman Auti
                                      wrote on last edited by
                                      #18

                                      vinaycool wrote:

                                      when the user clicks on the item i want the open file to closed(txt files) and slected file to opened is there any way to close and then open a file ??

                                      You have to trace the running processes and get the Handle of the NOTEPAD.exe which is the default application of the .txt files and terminate that process before executing the ShellExecute. Knock out 't' from can't, You can if you think you can :cool:

                                      1 Reply Last reply
                                      0
                                      • V VinayCool

                                        Hi, I have variable FName & Str, CString FName; char str[100]; i want the FName to be stored in str i tryed using str = FName; i am getting error error C2440: '=' : cannot convert from 'class CString' to 'char [100]' Can anyone help me with this convershion or any related notes or links will be helpfull. Regards, Vinay Charan.

                                        K Offline
                                        K Offline
                                        knoxplusplus
                                        wrote on last edited by
                                        #19

                                        Hi !!! You can try to use this. 1.) str = FName.c_str(); 2.) str = (char[])FName; I hope, I helped you. By. :cool: -:KNOX:-

                                        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