Newbie Deleting a file

This is a discussion on Newbie Deleting a file within the C Programming forums, part of the General Programming Boards category; FILE *myscene; myscene = fopen("Doiv","wb"); fwrite(Scene, 1, 200, myscene); fclose(myscene); How does one go about removing/deleting the file i create.. ...

  1. #1
    CNewbie
    Join Date
    Nov 2005
    Location
    Oz
    Posts
    31

    Newbie Deleting a file

    FILE *myscene;
    myscene = fopen("Doiv","wb");
    fwrite(Scene, 1, 200, myscene);
    fclose(myscene);

    How does one go about removing/deleting the file i create..

    I believe Remove is the option...
    But i can't get it to work at all...

    remove(Doiv); (tells me it's undeclared in this function)
    How does one declare it in this function?

    And why does writing the file, not need declaring also?

    remove(myscene); Also doesn't work......

    Anyone?

  2. #2
    Registered User
    Join Date
    Feb 2005
    Posts
    38
    [edit]
    Last edited by NoUse; 11-28-2005 at 12:14 AM.
    I like to play pocket pool.

  3. #3
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    remove takes a string of the pathname, so in your case:
    Code:
    remove("Doiv");
    Presuming it's in the current working directory.

    Quote Originally Posted by NoUse
    You would use:
    Code:
    remove(myscene);
    No you wouldn't.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    38
    Yea, you're right. I probably should have reviewed the function. I assumed it would just need the file pointer.
    I like to play pocket pool.

  5. #5
    CNewbie
    Join Date
    Nov 2005
    Location
    Oz
    Posts
    31
    I just tried it again, and it compiles without problem..
    as a commandline .exe, But does not give the same error.....

    Yet the test object is still not deleted.... So it just ignores it.

    If i add the almost identical code, to my plugin (a .dll)
    It instantly crashed, my plugin and it's host.....

    It gave me a warning not an error.....
    [Warning] passing arg 1 of `remove' from incompatible pointer type.

    This time i moved it, in case it was deleting the file, before i was finished calling it,

    So i moved it down a few lines.
    and it does not crash, but still does not delete the file...
    So i'm bloody confused. Anybody, weird....


    EDIT: Thanks the direct name worked.... Thanks!

    I think as a C Newbie, that's plain stupid.....
    I'd rather just use remove(myfile); As that is far more logical..
    But it works..... Well it deletes my object before it's used it...
    But i'll figure that out i guess, but an if or else statement should fix that.... Oh i hope...
    Last edited by Freestyler; 11-28-2005 at 12:59 AM.

  6. #6
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    If you get that warning, you are not passing a string as I posted above.

    What is your remove line now?

  7. #7
    CNewbie
    Join Date
    Nov 2005
    Location
    Oz
    Posts
    31
    remove("Doiv");
    Does indeed work, (thankyou)

    It was the remove(myfile); that would sometimes cause the warning....



    What is the fastest way to make sure i only delete the file
    after it's usage.....

    For example, the commandline test works fine......

    But my .dll, actually writes an object file, and then manipulates
    that file, and then should remove it.....

    However in the .dll, the remove statement, is read before my local-evaluate commands (specific host functions).... So the plugin failes, because it's deleted directly after it's created, i'm guessing because the C compiler, works this way......

    Code:
    FILE *myfile;
    myfile = fopen("Void","wb");
    fwrite(Obj, 1, 400,  myfile);
    fclose(myfile);
    
    local->evaluate( local->data, "LastItem" );
    
    remove("Doiv");
    
       return AFUNC_OK;
    What's the easiest method, to make sure it's only deleted
    after the local-evaulate command is issued first.....

    Thanks...

  8. #8
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    In the above code, the remove statement is after the local->evaluate call. The remove statement will only execute after local->evaluate finishes. If that function somehow sets in motion operations that continue to occur after the function finishes, then of course remove() may be called before the end. This is specific to whatever bizarre platform/environment you're using.

    As an aside, on most operating systems, a remove call will only actually delete the file once any process that has it open has finished with it.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    mktemp?


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

  10. #10
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by quzah
    mktemp?


    Quzah.
    mktemp is a fairly non-portable BSD derived function, and the implementation tends to be broken.

  11. #11
    CNewbie
    Join Date
    Nov 2005
    Location
    Oz
    Posts
    31
    Doh!

    No that works fine now, thanks again everyone....
    I'd kept an unintentional remove(myfile) earlier in my code..

    Thanks again.....

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by cwr
    mktemp is a fairly non-portable BSD derived function, and the implementation tends to be broken.
    I wasn't paying attention to what section it was in the book I looked it up in for reference. :P


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21