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. File Opening problem

File Opening problem

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelptutorialquestion
28 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 VinayCool

    Hi WhiteSky, below is the function which i have used can u please what the error in it 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); }

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

    I can't follow code without formatting. Click the ignore HTML tags checkbox when you enter a message so we can see the formatting. Also give the types of all variables referred to. e.g. The type of the "m_SOUT" variable. Steve

    V 1 Reply Last reply
    0
    • S Stephen Hewitt

      Don't use strcat: this is C++ not C! If you want to concatenate do it like this: CString str = "C:\\"; str += "test.txt"; // This is how you do it! ShellExecute(NULL, "open", str, NULL, NULL, SW_SHOWNORMAL); Steve

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

      i know strcat it was for str3[100]; not cstring:)_**


      **_

      whitesky


      1 Reply Last reply
      0
      • S Stephen Hewitt

        I can't follow code without formatting. Click the ignore HTML tags checkbox when you enter a message so we can see the formatting. Also give the types of all variables referred to. e.g. The type of the "m_SOUT" variable. Steve

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

        Hi Stephen Hewitt, In list box i have items like C:\folder\folder\filename1.txt C:\folder\folder\filename2.txt C:\folder\folder\filename3.txt C:\folder\folder\filename4.txt since by default i get the file name as c:\folder\folder\file.txt i have char array to change it to c:/folder/folder/file.txt final value of str will have C:/Folder/Folder/filename.txt below code i have used to open void CSearchDlg::OnDblclkSout() { // 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); }

        1 Reply Last reply
        0
        • V VinayCool

          Hi WhiteSky, below is the function which i have used can u please what the error in it 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); }

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

          your code now open one text file void CssDlg::OnBnClickedMybutton() { // TODO: Add your control notification handler code here // Code to open the selected file. int d; //d=m_SOUT.GetCurSel();//listbox CString FName; FName="c:\\code.txt"; //m_SOUT.GetText(d,FName); //one function from listbox 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*/m_Edit1.SetWindowText(str);//maybe editbox ShellExecute(NULL, "open", str ,NULL, NULL, SW_SHOWNORMAL); }_**


          **_

          whitesky


          V 1 Reply Last reply
          0
          • S Stephen Hewitt

            I just press it and it works; sorry I can't be of more help. Steve

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

            Vinay wrote:

            one question how to use modify button!?it seems dosent work for me

            What a lazy guy.:| Yesterday asked for same. Knock out 't' from can't, You can if you think you can :cool:

            V 1 Reply Last reply
            0
            • S Stephen Hewitt

              I just press it and it works; sorry I can't be of more help. Steve

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

              thank you for information i know i cant use modify about 3 days ago :)_**


              **_

              whitesky


              1 Reply Last reply
              0
              • L Laxman Auti

                Vinay wrote:

                one question how to use modify button!?it seems dosent work for me

                What a lazy guy.:| Yesterday asked for same. 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

                Hey .. I did my home work ok i wrote a program to change the \ to / . i did nt take anyone help for that .....

                1 Reply Last reply
                0
                • H Hamid Taebi

                  your code now open one text file void CssDlg::OnBnClickedMybutton() { // TODO: Add your control notification handler code here // Code to open the selected file. int d; //d=m_SOUT.GetCurSel();//listbox CString FName; FName="c:\\code.txt"; //m_SOUT.GetText(d,FName); //one function from listbox 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*/m_Edit1.SetWindowText(str);//maybe editbox ShellExecute(NULL, "open", str ,NULL, NULL, SW_SHOWNORMAL); }_**


                  **_

                  whitesky


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

                  Hi WhiteSky, The code will open only 1 file by default, how can we do it dynamically ??is there any solution for this problem..please let me know...

                  H 1 Reply Last reply
                  0
                  • V VinayCool

                    Hi WhiteSky, The code will open only 1 file by default, how can we do it dynamically ??is there any solution for this problem..please let me know...

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

                    yes this code open only one file(text) because i dont know that you how to get files and paths, this code work for one file so maybe your path is wrong your files_**


                    **_

                    whitesky


                    V 1 Reply Last reply
                    0
                    • H Hamid Taebi

                      yes this code open only one file(text) because i dont know that you how to get files and paths, this code work for one file so maybe your path is wrong your files_**


                      **_

                      whitesky


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

                      Hi WhiteSky, ShellExecute(NULL, "open", str ,NULL, NULL, SW_SHOWNORMAL); here i am passing str is of char str[100]; is this can be problem ??? have to change char array to CString ??? if i convert it will work ?? can u please tell me how to change char array to CString .. char str[100]; CString Name;

                      H 1 Reply Last reply
                      0
                      • V VinayCool

                        Hi WhiteSky, ShellExecute(NULL, "open", str ,NULL, NULL, SW_SHOWNORMAL); here i am passing str is of char str[100]; is this can be problem ??? have to change char array to CString ??? if i convert it will work ?? can u please tell me how to change char array to CString .. char str[100]; CString Name;

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

                        I write your code and it work. I am not understanding your question what do you need you said this code will open only one file but i need to run dynamically for another files now you said is this problem(str[100]) if this code will work so you need to trace your code can you explain exactly: 1-you can use ShellExecute or no 2-you need to change char array to CString or no if yes why 3-your code has problem or no 4-how to get files and paths 5-can you use from debugger(F9 and F5)for show str value 6-whats the meaning of change array to cstring 7-in your code you use from one loop why_**


                        **_

                        whitesky


                        V 1 Reply Last reply
                        0
                        • H Hamid Taebi

                          I write your code and it work. I am not understanding your question what do you need you said this code will open only one file but i need to run dynamically for another files now you said is this problem(str[100]) if this code will work so you need to trace your code can you explain exactly: 1-you can use ShellExecute or no 2-you need to change char array to CString or no if yes why 3-your code has problem or no 4-how to get files and paths 5-can you use from debugger(F9 and F5)for show str value 6-whats the meaning of change array to cstring 7-in your code you use from one loop why_**


                          **_

                          whitesky


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

                          Hi WhiteSky, The exact problem is in the list box i have list of file names with there complete path. file names are added by other function which is adding correctly ex C:\Box01\00000304\00000034.txt C:\Box01\00000304\00000035.txt C:\Box01\00000304\00000036.txt C:\Box01\00000304\00000037.txt etc... since i am using ShellExecute as it takes only file name like C:/Box01/00000304/00000037.txt i have used char array & changed the \ to / i am using this stament just as a check point to find what data str has m_ctrlEDIT.SetWindowText(str); in the str i get the data as C:\Box01\00000304\00000037.txt i also ued F5 and cheked str has proper data.. but still the file is not opening ...please tell me waht will be possible cause..

                          H 1 Reply Last reply
                          0
                          • V VinayCool

                            Hi WhiteSky, The exact problem is in the list box i have list of file names with there complete path. file names are added by other function which is adding correctly ex C:\Box01\00000304\00000034.txt C:\Box01\00000304\00000035.txt C:\Box01\00000304\00000036.txt C:\Box01\00000304\00000037.txt etc... since i am using ShellExecute as it takes only file name like C:/Box01/00000304/00000037.txt i have used char array & changed the \ to / i am using this stament just as a check point to find what data str has m_ctrlEDIT.SetWindowText(str); in the str i get the data as C:\Box01\00000304\00000037.txt i also ued F5 and cheked str has proper data.. but still the file is not opening ...please tell me waht will be possible cause..

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

                            C:\Box01\00000304\00000034.txt it seems not problem of course if is valid and i think if you use like this it work you can use CString instead char[100] and if these paths and files are valid so i think you can use ShellExecute that it should open notepad (I guess I'm not sure maybe your string concat to previous string for example you have c:\\1.txt then you want to open another file then c:\\1.txtc:\\2.txt Did you check for this case)_**


                            **_

                            whitesky


                            V 1 Reply Last reply
                            0
                            • H Hamid Taebi

                              C:\Box01\00000304\00000034.txt it seems not problem of course if is valid and i think if you use like this it work you can use CString instead char[100] and if these paths and files are valid so i think you can use ShellExecute that it should open notepad (I guess I'm not sure maybe your string concat to previous string for example you have c:\\1.txt then you want to open another file then c:\\1.txtc:\\2.txt Did you check for this case)_**


                              **_

                              whitesky


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

                              Hi WhiteSky, Thank you very much for the help. 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 1 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 be opened is there any way to close and then open a file ?? please let know if there any way

                              H 1 Reply Last reply
                              0
                              • V VinayCool

                                Hi WhiteSky, Thank you very much for the help. 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 1 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 be opened is there any way to close and then open a file ?? please let know if there any way

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

                                Hope I understood your question you want to close notepad or anything that open your file use FindWindow ... ... ShellExecute(NULL, "open", str ,NULL, NULL, SW_SHOWNORMAL); MessageBox("A"); HWND hWnd = ::FindWindow("Notepad", NULL); ::SendMessage(hWnd,WM_CLOSE,0,0);_**


                                **_

                                whitesky


                                V 2 Replies Last reply
                                0
                                • H Hamid Taebi

                                  Hope I understood your question you want to close notepad or anything that open your file use FindWindow ... ... ShellExecute(NULL, "open", str ,NULL, NULL, SW_SHOWNORMAL); MessageBox("A"); HWND hWnd = ::FindWindow("Notepad", NULL); ::SendMessage(hWnd,WM_CLOSE,0,0);_**


                                  **_

                                  whitesky


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

                                  Hi WhiteSky, Thank you very much ....U are simply great in VC++ Regards, Vinay Charan.:)

                                  1 Reply Last reply
                                  0
                                  • H Hamid Taebi

                                    Hope I understood your question you want to close notepad or anything that open your file use FindWindow ... ... ShellExecute(NULL, "open", str ,NULL, NULL, SW_SHOWNORMAL); MessageBox("A"); HWND hWnd = ::FindWindow("Notepad", NULL); ::SendMessage(hWnd,WM_CLOSE,0,0);_**


                                    **_

                                    whitesky


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

                                    if possibe can u tell me what are HWND hwnd ??? is it an handler ??

                                    H 1 Reply Last reply
                                    0
                                    • V VinayCool

                                      if possibe can u tell me what are HWND hwnd ??? is it an handler ??

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

                                      yes handle to window_**


                                      **_

                                      whitesky


                                      1 Reply Last reply
                                      0
                                      • V VinayCool

                                        Hi Stephen Hewitt, I have checked with this code ShellExecute(NULL, "open", str ,NULL, NULL, SW_SHOWNORMAL); no effect ,its not opening the file .... since i get the file name input as c:\folder\folder\file.ext i have used 2 sting array to convert it to c:/folder/folder/file.ext now i have to change char to CString ????

                                        S Offline
                                        S Offline
                                        Saday Sarkar
                                        wrote on last edited by
                                        #27

                                        Be sure str is being "c:\folder\folder\file.ext " or not. U may have to make str to "c:\folder\folder\file.ext " "c:\\folder\\folder\\file.ext " chk ur String making Calculation.....:) Saday Chand Sarkar Software Engineer Trek Technology(s)Pte.Ltd.

                                        1 Reply Last reply
                                        0
                                        • V VinayCool

                                          Hi, i have a variable str in which i have file name with complete folder path char str[100]; --- ----- --------code --- final str will have a value C:/folder/folder/file.txt then i have used ShellExecute(NULL, "open", "str" ,NULL, NULL, SW_SHOWNORMAL); but this statment has no effect its not opening the file. i checked with example ShellExecute(NULL, "open", "D:/folder/folder/file.txt" ,NULL, NULL, SW_SHOWNORMAL); this code opens the file,can any one tell why the file is not opening in the first case... do we have to convert char array to CString then pass it to ShellExecute ??? Regards, Vinay Charan.

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

                                          Hi !!! Your sample: ------------ ShellExecute(NULL, "open", "str" ,NULL, NULL, SW_SHOWNORMAL); My sample: ---------- ShellExecute(NULL, "open", str ,NULL, NULL, SW_SHOWNORMAL); You cannot give it into the quotes. I hope, I helped you. By. :laugh: -: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