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. Creating and deleting a temp file

Creating and deleting a temp file

Scheduled Pinned Locked Moved C / C++ / MFC
help
15 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.
  • C CodingLover

    I want to create a temp file do some process and delete it. I've create a temp file like this. szTempFile = new TCHAR[MAX_PATH]; if(!::GetTempFileName(".","temp",0,szTempFile)) { return -1; } It's creating a temp file on the current working folder. Then I try to delete the file as follows. if(szTempFile != NULL) { //delete szTempFile; HRESULT b = ::DeleteFile(szTempFile); } This not delete the file. When I check the path it's valid but the result of parameter b is 0. Can someone help me to find the error.

    I appreciate your help all the time... CodingLover :)

    _ Offline
    _ Offline
    _Superman_
    wrote on last edited by
    #2

    Call GetLastError after DeleteFile and check what it returns.

    «_Superman_» I love work. It gives me something to do between weekends.

    C 1 Reply Last reply
    0
    • _ _Superman_

      Call GetLastError after DeleteFile and check what it returns.

      «_Superman_» I love work. It gives me something to do between weekends.

      C Offline
      C Offline
      CodingLover
      wrote on last edited by
      #3

      Yes, I got the error code as 32. I'm mess with the finding what it means.

      I appreciate your help all the time... CodingLover :)

      _ 1 Reply Last reply
      0
      • C CodingLover

        Yes, I got the error code as 32. I'm mess with the finding what it means.

        I appreciate your help all the time... CodingLover :)

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #4

        It means the file is in use. You can delete the file only after it is closed using CloseHandle.

        «_Superman_» I love work. It gives me something to do between weekends.

        C 1 Reply Last reply
        0
        • C CodingLover

          I want to create a temp file do some process and delete it. I've create a temp file like this. szTempFile = new TCHAR[MAX_PATH]; if(!::GetTempFileName(".","temp",0,szTempFile)) { return -1; } It's creating a temp file on the current working folder. Then I try to delete the file as follows. if(szTempFile != NULL) { //delete szTempFile; HRESULT b = ::DeleteFile(szTempFile); } This not delete the file. When I check the path it's valid but the result of parameter b is 0. Can someone help me to find the error.

          I appreciate your help all the time... CodingLover :)

          N Offline
          N Offline
          Nishad S
          wrote on last edited by
          #5

          You might not closing the file properly.

          - ns ami -

          C 1 Reply Last reply
          0
          • _ _Superman_

            It means the file is in use. You can delete the file only after it is closed using CloseHandle.

            «_Superman_» I love work. It gives me something to do between weekends.

            C Offline
            C Offline
            CodingLover
            wrote on last edited by
            #6

            Can you bit explain how to use a handle for the file. I just use TCHAR buffer to hold the path. I hope your clear with my previous code segment.

            I appreciate your help all the time... CodingLover :)

            _ 1 Reply Last reply
            0
            • N Nishad S

              You might not closing the file properly.

              - ns ami -

              C Offline
              C Offline
              CodingLover
              wrote on last edited by
              #7

              As Superman says, closing the file? Can you please explain a bit more how to do it. Actually I don't have handler to the file, in my code posted before.

              I appreciate your help all the time... CodingLover :)

              N 1 Reply Last reply
              0
              • C CodingLover

                As Superman says, closing the file? Can you please explain a bit more how to do it. Actually I don't have handler to the file, in my code posted before.

                I appreciate your help all the time... CodingLover :)

                N Offline
                N Offline
                Nishad S
                wrote on last edited by
                #8

                How is the file opened (or used)?

                - ns ami -

                C 1 Reply Last reply
                0
                • N Nishad S

                  How is the file opened (or used)?

                  - ns ami -

                  C Offline
                  C Offline
                  CodingLover
                  wrote on last edited by
                  #9

                  I use SHCreateStreamOnFile stream on the file. Something like this. if (FAILED(hr = SHCreateStreamOnFile(szTempFile, STGM_CREATE | STGM_WRITE, &pOutFileStream)))

                  I appreciate your help all the time... CodingLover :)

                  I 1 Reply Last reply
                  0
                  • C CodingLover

                    I want to create a temp file do some process and delete it. I've create a temp file like this. szTempFile = new TCHAR[MAX_PATH]; if(!::GetTempFileName(".","temp",0,szTempFile)) { return -1; } It's creating a temp file on the current working folder. Then I try to delete the file as follows. if(szTempFile != NULL) { //delete szTempFile; HRESULT b = ::DeleteFile(szTempFile); } This not delete the file. When I check the path it's valid but the result of parameter b is 0. Can someone help me to find the error.

                    I appreciate your help all the time... CodingLover :)

                    S Offline
                    S Offline
                    Soundman32 2
                    wrote on last edited by
                    #10

                    I wonder if Windows is expecting you to call CreateFile on the filename before you delete it? Have you looked at http://msdn.microsoft.com/en-us/library/aa363875(VS.85).aspx[^] which gives an example of creating and using a temporary file?

                    1 Reply Last reply
                    0
                    • C CodingLover

                      Can you bit explain how to use a handle for the file. I just use TCHAR buffer to hold the path. I hope your clear with my previous code segment.

                      I appreciate your help all the time... CodingLover :)

                      _ Offline
                      _ Offline
                      _Superman_
                      wrote on last edited by
                      #11

                      What are you doing after creating the temporary file? Are you opening it using fopen or CreateFile? What operations are you doing with the file?

                      «_Superman_» I love work. It gives me something to do between weekends.

                      _ 1 Reply Last reply
                      0
                      • C CodingLover

                        I use SHCreateStreamOnFile stream on the file. Something like this. if (FAILED(hr = SHCreateStreamOnFile(szTempFile, STGM_CREATE | STGM_WRITE, &pOutFileStream)))

                        I appreciate your help all the time... CodingLover :)

                        I Offline
                        I Offline
                        Iain Clarke Warrior Programmer
                        wrote on last edited by
                        #12

                        That stream creation call will be opening the file. Then you'll need to make sure you call

                        pOutFileStream->Release ()

                        at a convenient moment. Iain.

                        Codeproject MVP for C++, I can't believe it's for my lounge posts...

                        1 Reply Last reply
                        0
                        • _ _Superman_

                          What are you doing after creating the temporary file? Are you opening it using fopen or CreateFile? What operations are you doing with the file?

                          «_Superman_» I love work. It gives me something to do between weekends.

                          _ Offline
                          _ Offline
                          _Superman_
                          wrote on last edited by
                          #13

                          I read that you're using SHCreateStreamOnFile to open the file. So you must call pOutFileStream->Release() before you can call DeleteFile.

                          «_Superman_» I love work. It gives me something to do between weekends.

                          C 1 Reply Last reply
                          0
                          • _ _Superman_

                            I read that you're using SHCreateStreamOnFile to open the file. So you must call pOutFileStream->Release() before you can call DeleteFile.

                            «_Superman_» I love work. It gives me something to do between weekends.

                            C Offline
                            C Offline
                            CodingLover
                            wrote on last edited by
                            #14

                            Yes I've already close the stream before delete the file. What I'm doing is, create a temp file, then write some data to it. And then read the content into ostream and use that stream for processing. I want to delete the temp file. When I check the temp file path it's on working folder, like this.

                            .\ad.tmp

                            It wont be a case right?

                            I appreciate your help all the time... CodingLover :)

                            C 1 Reply Last reply
                            0
                            • C CodingLover

                              Yes I've already close the stream before delete the file. What I'm doing is, create a temp file, then write some data to it. And then read the content into ostream and use that stream for processing. I want to delete the temp file. When I check the temp file path it's on working folder, like this.

                              .\ad.tmp

                              It wont be a case right?

                              I appreciate your help all the time... CodingLover :)

                              C Offline
                              C Offline
                              CodingLover
                              wrote on last edited by
                              #15

                              Thanks a lot my friends. I've solve my problem. Actually I didn't properly close one of stream handler in the process. Thanks again.

                              I appreciate your help all the time... CodingLover :)

                              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