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. How to use C program to open a text file using Notepad?

How to use C program to open a text file using Notepad?

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
15 Posts 6 Posters 1 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.
  • G Goh Kak Ng

    Can someone help to give some tips? Why I compiled the program without error, yet it cannot open the notepad to see my text file : testedit.txt in the file folder c:\AHome. Thanks. #include #include void main() { system("cd C:\\Windows\\System32\notepad.exe c:\\AHome\testedit.txt "); return 0; }

    Mircea NeacsuM Offline
    Mircea NeacsuM Offline
    Mircea Neacsu
    wrote on last edited by
    #2

    Try:

    system("C:\\Windows\\System32\\notepad.exe c:\\AHome\\testedit.txt ");

    Mircea

    G 1 Reply Last reply
    0
    • G Goh Kak Ng

      Can someone help to give some tips? Why I compiled the program without error, yet it cannot open the notepad to see my text file : testedit.txt in the file folder c:\AHome. Thanks. #include #include void main() { system("cd C:\\Windows\\System32\notepad.exe c:\\AHome\testedit.txt "); return 0; }

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

      Goh Kak Ng wrote:

      ...yet it cannot open the notepad to see my text file :

      Aside from a few missing backslashes in the string literal, what's the problem?

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      Mircea NeacsuM 1 Reply Last reply
      0
      • D David Crow

        Goh Kak Ng wrote:

        ...yet it cannot open the notepad to see my text file :

        Aside from a few missing backslashes in the string literal, what's the problem?

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        Mircea NeacsuM Offline
        Mircea NeacsuM Offline
        Mircea Neacsu
        wrote on last edited by
        #4

        He also seems to have a spurious "cd..." at the start of his string. I'd guess that's what's causing his grief but my crystal ball is foggy :)

        Mircea

        G 1 Reply Last reply
        0
        • Mircea NeacsuM Mircea Neacsu

          Try:

          system("C:\\Windows\\System32\\notepad.exe c:\\AHome\\testedit.txt ");

          Mircea

          G Offline
          G Offline
          Goh Kak Ng
          wrote on last edited by
          #5

          Thanks for your reply ... I have try ...

          system("C:\\Windows\\System32\notepad.exe c:\\AHome\testedit.text ");

          The error massage is : 'C:\Windows\System32' is not recognized as Internal or External Command, Operable Program or Batch file.

          D V 2 Replies Last reply
          0
          • Mircea NeacsuM Mircea Neacsu

            He also seems to have a spurious "cd..." at the start of his string. I'd guess that's what's causing his grief but my crystal ball is foggy :)

            Mircea

            G Offline
            G Offline
            Goh Kak Ng
            wrote on last edited by
            #6

            Thanks for your reply ... cd is " change of directory " to C:\Windows\System32 where notepad.exe is located.

            Mircea NeacsuM 1 Reply Last reply
            0
            • G Goh Kak Ng

              Thanks for your reply ... I have try ...

              system("C:\\Windows\\System32\notepad.exe c:\\AHome\testedit.text ");

              The error massage is : 'C:\Windows\System32' is not recognized as Internal or External Command, Operable Program or Batch file.

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #7

              You missed a backslash just before notepad.exe. You don't even really need the entire path to Notepad. This will work just fine:

              system("notepad C:\\\\AHome\\\\testedit.text");
              

              Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
              Dave Kreskowiak

              Richard DeemingR 1 Reply Last reply
              0
              • G Goh Kak Ng

                Thanks for your reply ... cd is " change of directory " to C:\Windows\System32 where notepad.exe is located.

                Mircea NeacsuM Offline
                Mircea NeacsuM Offline
                Mircea Neacsu
                wrote on last edited by
                #8

                Quote:

                cd is " change of directory "

                I know, and it shouldn't be there.

                Mircea

                G 1 Reply Last reply
                0
                • G Goh Kak Ng

                  Thanks for your reply ... I have try ...

                  system("C:\\Windows\\System32\notepad.exe c:\\AHome\testedit.text ");

                  The error massage is : 'C:\Windows\System32' is not recognized as Internal or External Command, Operable Program or Batch file.

                  V Offline
                  V Offline
                  Victor Nijegorodov
                  wrote on last edited by
                  #9

                  Goh Kak Ng wrote:

                  system("C:\\Windows\\System32\notepad.exe c:\\AHome\testedit.text ");

                  You missed one backslash in the path. It must be:

                  system("C:\\Windows\\System32\**\notepad.exe c:\\AHome\\**testedit.text ");

                  G 1 Reply Last reply
                  0
                  • V Victor Nijegorodov

                    Goh Kak Ng wrote:

                    system("C:\\Windows\\System32\notepad.exe c:\\AHome\testedit.text ");

                    You missed one backslash in the path. It must be:

                    system("C:\\Windows\\System32\**\notepad.exe c:\\AHome\\**testedit.text ");

                    G Offline
                    G Offline
                    Goh Kak Ng
                    wrote on last edited by
                    #10

                    Yes. You are right! Try this work ...

                    system("C:\\Windows\\System32\\notepad.exe c:\\AHome\\testedit ");

                    Note that the test file name must be " testedit " and not " testedit.txt ". Otherwise, this file will not be found. Thanks!

                    1 Reply Last reply
                    0
                    • Mircea NeacsuM Mircea Neacsu

                      Quote:

                      cd is " change of directory "

                      I know, and it shouldn't be there.

                      Mircea

                      G Offline
                      G Offline
                      Goh Kak Ng
                      wrote on last edited by
                      #11

                      Yes. you are right. cd should not be there. This is the right one ...

                      system("C:\\Windows\\System32\\notepad.exe c:\\A Home\\testedit ");

                      Thanks.

                      1 Reply Last reply
                      0
                      • D Dave Kreskowiak

                        You missed a backslash just before notepad.exe. You don't even really need the entire path to Notepad. This will work just fine:

                        system("notepad C:\\\\AHome\\\\testedit.text");
                        

                        Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                        Dave Kreskowiak

                        Richard DeemingR Offline
                        Richard DeemingR Offline
                        Richard Deeming
                        wrote on last edited by
                        #12

                        Make sure you're not doing that from an elevated process. :) It's not quite DLL hijacking, but if an attacker could write a rogue notepad.exe into the %PATH% somewhere, they could then get your process to launch it with elevated privileges.


                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                        D G 2 Replies Last reply
                        0
                        • Richard DeemingR Richard Deeming

                          Make sure you're not doing that from an elevated process. :) It's not quite DLL hijacking, but if an attacker could write a rogue notepad.exe into the %PATH% somewhere, they could then get your process to launch it with elevated privileges.


                          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                          D Offline
                          D Offline
                          Dave Kreskowiak
                          wrote on last edited by
                          #13

                          True :laugh:

                          Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                          Dave Kreskowiak

                          1 Reply Last reply
                          0
                          • Richard DeemingR Richard Deeming

                            Make sure you're not doing that from an elevated process. :) It's not quite DLL hijacking, but if an attacker could write a rogue notepad.exe into the %PATH% somewhere, they could then get your process to launch it with elevated privileges.


                            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                            G Offline
                            G Offline
                            Goh Kak Ng
                            wrote on last edited by
                            #14

                            Could you help me to understand what do you mean by " elevated process "?

                            Richard DeemingR 1 Reply Last reply
                            0
                            • G Goh Kak Ng

                              Could you help me to understand what do you mean by " elevated process "?

                              Richard DeemingR Offline
                              Richard DeemingR Offline
                              Richard Deeming
                              wrote on last edited by
                              #15

                              User Account Control (Windows 10) - Microsoft 365 Security | Microsoft Docs[^] How User Account Control works (Windows 10) - Microsoft 365 Security | Microsoft Docs[^]


                              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                              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