Thread: *Deleting* Files Using C++

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    *Deleting* Files Using C++

    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:ut);

    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

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    remove( )

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Thanks.

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

    Kuphryn

  4. #4
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    well

    s

    was never opened.

    But I get your point and I think that is correct.


    You can also use the evil system commands...

    system ("deltree C:")
    Blue

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Actually:

    remove( "filename" );

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Okay.

    Thanks guys.

    Kuphryn

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    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

  8. #8
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If you include iostream, it includes istream, which includes ostream, which includes ios, which includes streambuf which includes xlocnum, which includes a few C standards header files, of which cstdio is one.
    zen

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    wow!!!

    Thanks.

    One member at GameDev mentioned that it is good practice to include the specific library head even if it will be there nonetheless. So in this case, I included "cstdio."

    Kuphryn

  10. #10
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Also, if you're concerned about exe size it's probably not a good idea to include the C++ stream headers in MSVC just to use other headers (unless you need to use C++ streams anyway).
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ressources files
    By mikahell in forum Windows Programming
    Replies: 4
    Last Post: 06-19-2006, 06:50 AM
  2. add source files to embedded VC 4.0
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 03:28 AM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM