Thread: can you....

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    14

    can you....

    can you use File I/O to create files anywhere like C:\blah? cuz when I try and do that with C++ Builder 6, it doesn't work

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Yes all you have to do is eg.
    Code:
    ofstream file("c:\\Loser.txt");
    and you should be good to go
    Woop?

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    14
    thanks so much!!!!!!!!!!!!!

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Your welcome so much and welcome to the boards
    Woop?

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    14
    thanks for the help but I have another question. how can you delete a file? and if an error comes up for deleting (if it ever does), is there a way to ignore this error, but store this error in a variable or something?
    Last edited by cude87; 07-10-2004 at 10:08 PM.

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Try this method
    Code:
    #include <iostream>
    #include <fstream>
    #include <io.h>//Need for the _unlink function :)
    using namespace std;
    
    int main()
    {
      ofstream file("File.txt");
      file.close();   
      _unlink("File.txt");
      return 0;
    }
    [edit] forgot the <io.h> and the file.close() [/edit]
    Last edited by prog-bman; 07-10-2004 at 10:20 PM.
    Woop?

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    14
    thanks for the help but I could've done that with basic ofstream. that just empty's the file. I wanna know how to completely delete it. and do you know how to do shutdown, log off, restart, and quick shutdown (insecure) functions?

    I'm trying to build a shutdown program which recongnize keystrokes and does the desired function. like ctrl + F1 is shutdown, ctrl + alt + F1 is emergency instant shutdown, etc. the delete and writing is for the settings file. I did the key part. I just need the functions and delete function.

    EDIT: nvm, the delete function works perfectly. thanks, but I still need to know the other functions :-/

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    14
    I think I found something.....is this true? :

    Code:
    BOOL ExitWindowEx(UINT option here, DWORD dwRes)
    option is:

    EWX_FORCE
    EWX_LOGOFF
    EWX_POWEROFF
    EWX_REBOOT
    EWX_SHUTDOWN


    does this work? if so, which one is emergency shutdown? FORCE, POWEROFF?

  9. #9
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You would use EWX_FORCE and EWX_POWEROFF most likely for an emergency shutdown.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  10. #10
    Registered User
    Join Date
    Jul 2004
    Posts
    14
    I think I need to include a file. which one? windows.h?

  11. #11
    Registered User
    Join Date
    Mar 2004
    Posts
    113
    you can use too:

    Code:
    ofstrem obj("c:/some_dir/some_dir/etc",  ios::out);
    just check you use the "/" not the "\", because in a c/c++ is a special char(used to new line)

  12. #12
    Registered User
    Join Date
    Jul 2004
    Posts
    14
    I know about that. I want to know how to shutdown, logoff, etc. I think the functions there work, but I don't know which include file I need or something....

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >ofstream file("c:\\Loser.txt");
    It couldn't hurt to explain why this is so, prog-bman. cude87: The reason you need to double the backslashes is because string literals in C++ support escape characters, where any character or special sequence preceded by a backslash is treated specially. A good example is '\n'. Instead of meaing '\' and 'n', this escape character becomes a newline. If you say "c:\blah.txt" then C++ will think that you're trying to use the escape character '\b'. In this case you're lucky because that's a real escape character that stands for a backspace. If it weren't legal then you would be invoking undefined behavior. The reason '\\' works is because if you escape the backslash character, the result is a backslash, which is what you want.

    Some people may recommend that you use a forward slash instead to avoid escaping the next character, but that's not portable and won't work everywhere. If you can avoid non-portable constructs (like now because it's easy) then you should.

    >_unlink("File.txt");
    Not portable, nor is <io.h>. C++ supports a standard function called remove that does what the OP wants. It's found in <cstdio>.
    My best code is written with the delete key.

  14. #14
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    oh i thought _unlink wasn't portable but i wasn't sure. and thanks for the remove info
    Woop?

Popular pages Recent additions subscribe to a feed