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. open up a file as read-only

open up a file as read-only

Scheduled Pinned Locked Moved C / C++ / MFC
question
10 Posts 5 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.
  • E Offline
    E Offline
    elephantstar
    wrote on last edited by
    #1

    Is there a way to open up a word document as read-only when it's not a read-only file to begin with. I opened up my file using CreateProcess. Is there a way to do this? Thanks!

    T R B 3 Replies Last reply
    0
    • E elephantstar

      Is there a way to open up a word document as read-only when it's not a read-only file to begin with. I opened up my file using CreateProcess. Is there a way to do this? Thanks!

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      CreateProcess() does not open files !!!!!!! if you want to open a file with the ReadOnly option, you could do this :

      std::ifstream file("theFile.txt"); // read-only by default

      or

      fopen("theFile.txt", "r");


      TOXCCT >>> GEII power
      [toxcct][VisualCalc]

      E 1 Reply Last reply
      0
      • E elephantstar

        Is there a way to open up a word document as read-only when it's not a read-only file to begin with. I opened up my file using CreateProcess. Is there a way to do this? Thanks!

        R Offline
        R Offline
        rocky_pulley
        wrote on last edited by
        #3

        Assuming that you meant CreateFile, pass the second parameter as GENERIC_READ. -- Rocky Dean Pulley

        1 Reply Last reply
        0
        • T toxcct

          CreateProcess() does not open files !!!!!!! if you want to open a file with the ReadOnly option, you could do this :

          std::ifstream file("theFile.txt"); // read-only by default

          or

          fopen("theFile.txt", "r");


          TOXCCT >>> GEII power
          [toxcct][VisualCalc]

          E Offline
          E Offline
          elephantstar
          wrote on last edited by
          #4

          I tried the following and still won't open up. I want to open it up in MS Word which was why I used CreateProcess. What did I do wrong? Thanks! FILE * pFile; pFile = fopen (filename,"r"); if (pFile!=NULL) { fputs ("fopen example",pFile); fclose (pFile); }

          D 1 Reply Last reply
          0
          • E elephantstar

            I tried the following and still won't open up. I want to open it up in MS Word which was why I used CreateProcess. What did I do wrong? Thanks! FILE * pFile; pFile = fopen (filename,"r"); if (pFile!=NULL) { fputs ("fopen example",pFile); fclose (pFile); }

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

            Is using ShellExecute() out of the question?


            "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

            E 1 Reply Last reply
            0
            • D David Crow

              Is using ShellExecute() out of the question?


              "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

              E Offline
              E Offline
              elephantstar
              wrote on last edited by
              #6

              No but how is it different from CreateProcess? There seems to be no parameters that specify it as read-only.

              R 1 Reply Last reply
              0
              • E elephantstar

                No but how is it different from CreateProcess? There seems to be no parameters that specify it as read-only.

                R Offline
                R Offline
                rocky_pulley
                wrote on last edited by
                #7

                I see, you are trying to open with word and not with your own program for reading. What you want to do will probably need to use word OLE automation. I would suggest doing some searching for just that, it's not as simple as just executing the program to do this. It's actually not much more code but understanding the concepts is a bit more complex. I haven't done this sort of thing in a long time so I don't know the code off hand, but I'm sure a quick google search will find something. -- Rocky Dean Pulley

                E 1 Reply Last reply
                0
                • E elephantstar

                  Is there a way to open up a word document as read-only when it's not a read-only file to begin with. I opened up my file using CreateProcess. Is there a way to do this? Thanks!

                  B Offline
                  B Offline
                  bghuang
                  wrote on last edited by
                  #8

                  :laugh: CFile SrcFile; CString SrcFileName="C:\\test.doc"; SrcFile.Open(SrcFileName,CFile::modeRead ,NULL);

                  E 1 Reply Last reply
                  0
                  • B bghuang

                    :laugh: CFile SrcFile; CString SrcFileName="C:\\test.doc"; SrcFile.Open(SrcFileName,CFile::modeRead ,NULL);

                    E Offline
                    E Offline
                    elephantstar
                    wrote on last edited by
                    #9

                    I do not want to read the contents of the file. I want to be able to open the file in Microsoft Word or Notepad as read-only.

                    1 Reply Last reply
                    0
                    • R rocky_pulley

                      I see, you are trying to open with word and not with your own program for reading. What you want to do will probably need to use word OLE automation. I would suggest doing some searching for just that, it's not as simple as just executing the program to do this. It's actually not much more code but understanding the concepts is a bit more complex. I haven't done this sort of thing in a long time so I don't know the code off hand, but I'm sure a quick google search will find something. -- Rocky Dean Pulley

                      E Offline
                      E Offline
                      elephantstar
                      wrote on last edited by
                      #10

                      I found a simpler way. Before opening up the document in Word, I used GetFileAttributes() to get its file attribute. If the returned value is not read-only, I called SetFileAttributes to set it so and then use CreateProcess to open it up within Word. Much simpler. Thanks for your help though.

                      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