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. *Deleting* Files Using C++

*Deleting* Files Using C++

Scheduled Pinned Locked Moved C / C++ / MFC
c++ioshelpquestion
6 Posts 4 Posters 4 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
    valikac
    wrote on last edited by
    #1

    Hi. I have a question about deleting a few. Here is a scenario. The program first asks the user to enter the name of a file to take to. ostream outFile; outFile.open(userchoice, ios::out); With ofstream, I found that it will create the file if it cannot find it. I tried implementing "nocreate," but VC++ 6 gave me an error that there is not such function "ios::nocreate." Anyways, the program would create the file if it cannot find it and the file would be empty or 0 byte size. Afterward, the program calculates specific data (not worth mentioning) and outputs data to the outpuf *if applicatable*. So sometimes the program writes nothing and I would a 0 byte file under the name that the user originally entered. I would like to know if there is a way to delete a file. So in the case above, there would be a line of code to delete the file if applicable. Thanks, Kuphryn

    T V 2 Replies Last reply
    0
    • V valikac

      Hi. I have a question about deleting a few. Here is a scenario. The program first asks the user to enter the name of a file to take to. ostream outFile; outFile.open(userchoice, ios::out); With ofstream, I found that it will create the file if it cannot find it. I tried implementing "nocreate," but VC++ 6 gave me an error that there is not such function "ios::nocreate." Anyways, the program would create the file if it cannot find it and the file would be empty or 0 byte size. Afterward, the program calculates specific data (not worth mentioning) and outputs data to the outpuf *if applicatable*. So sometimes the program writes nothing and I would a 0 byte file under the name that the user originally entered. I would like to know if there is a way to delete a file. So in the case above, there would be a line of code to delete the file if applicable. Thanks, Kuphryn

      T Offline
      T Offline
      Tim Smith
      wrote on last edited by
      #2

      remove It is just a simple old C routine. Tim Smith Descartes Systems Sciences, Inc.

      V 1 Reply Last reply
      0
      • T Tim Smith

        remove It is just a simple old C routine. Tim Smith Descartes Systems Sciences, Inc.

        V Offline
        V Offline
        valikac
        wrote on last edited by
        #3

        Thanks. ofstream x; x.open(file, ios::out); s.remove(file); // Is this a valid syntax? Kuphryn

        R 1 Reply Last reply
        0
        • V valikac

          Thanks. ofstream x; x.open(file, ios::out); s.remove(file); // Is this a valid syntax? Kuphryn

          R Offline
          R Offline
          Rick York
          wrote on last edited by
          #4

          No, it is not. You can use : remove(file); or DeleteFile(file); if you prefer to use the Win32 API directly. FWIW, remove() calls DeleteFile() but remove is portable.

          1 Reply Last reply
          0
          • V valikac

            Hi. I have a question about deleting a few. Here is a scenario. The program first asks the user to enter the name of a file to take to. ostream outFile; outFile.open(userchoice, ios::out); With ofstream, I found that it will create the file if it cannot find it. I tried implementing "nocreate," but VC++ 6 gave me an error that there is not such function "ios::nocreate." Anyways, the program would create the file if it cannot find it and the file would be empty or 0 byte size. Afterward, the program calculates specific data (not worth mentioning) and outputs data to the outpuf *if applicatable*. So sometimes the program writes nothing and I would a 0 byte file under the name that the user originally entered. I would like to know if there is a way to delete a file. So in the case above, there would be a line of code to delete the file if applicable. Thanks, Kuphryn

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

            remove("file") works well. There is one weird issue. the remove() function works even when I do not include "cstdio." This is also true for a few other functions. Why? Note I do not use "namespace std." Is VC++ compiler including some libraries automatically? Kuphryn

            R 1 Reply Last reply
            0
            • V valikac

              remove("file") works well. There is one weird issue. the remove() function works even when I do not include "cstdio." This is also true for a few other functions. Why? Note I do not use "namespace std." Is VC++ compiler including some libraries automatically? Kuphryn

              R Offline
              R Offline
              Ravi Bhavnani
              wrote on last edited by
              #6

              remove() is a C function prototyped in stdio.h or io.h. You can also use _unlink(). /ravi "There is always one more bug..." ravib@ravib.com http://www.ravib.com

              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