File Input/Output

This is a discussion on File Input/Output within the C++ Programming forums, part of the General Programming Boards category; I'm learning how to handle file input and output, and I have a quick question. Let's say I make a ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    15

    File Input/Output

    I'm learning how to handle file input and output, and I have a quick question. Let's say I make a program with a 30 day trial on it. I'd make a file that says the date the program was made. I don't want that file to be edited by anybody, so I would put it in a place that is not so easy to find such as the windows folder. How would I go about doing that, however? I had something like:

    Code:
    ofstream license("C:\Windows\license.txt");
    but that generates an error. Anybody have any suggestions?

    Mornic Programm
    Last edited by Mornic_Programm; 05-30-2005 at 11:22 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Quote Originally Posted by Mornic_Programm
    I'm learning how to handle file input and output, and I have a quick question. Let's say I make a program with a 30 day trial on it. I'd make a file that says the date the program was made. I don't want that file to be edited by anybody, so I would put it in a place that is not so easy to find such as the windows folder. How would I go about doing that, however?
    Things like this are typically done by placing an entry in the registry.

    Quote Originally Posted by Mornic_Programm
    I had something like:

    Code:
    ofstream license("C:\Windows\license.txt");
    but that generates an error. Anybody have any suggestions?

    Mornic Programm
    Try:

    Code:
    ofstream license("C:\\Windows\\license.txt");
    A single "\" character by itself indicates an escape sequence, i.e. \n (newline) or \t (tab) for instance. You need to use two of them together "\\" to get the effect you want.
    Last edited by hk_mp5kpdw; 05-30-2005 at 11:51 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    15

    Thank You!!!

    Thank you so much! I knew it had to be something easy. I'm new to C++, sorry. Just got out of programming in BASIC. Major differences...thanks again!

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    The '\' signals an 'escape sequence', and escape sequences have a special meaning in C++. If you literally want a slash, you have to 'escape' the slash, which produces the somewhat strange syntax: '\\'

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    15
    new problem...same issue...if that makes any sense. Some people have their windows folders located in a place OTHER than the C drive (my friend does, at least) So my most obvious guess would be to do:

    Code:
    ofstream license("%windir%\\license.txt");
    At first glance, I think that would work. THen I try and run it and...well it doesn't go to the windows directory. In fact, it doesn't go anywhere. Any solutions?

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    If "windir" is an environment variable you could probably use the getenv function to obtain the value and concatenate it with your file name.

    Code:
    string filename(getenv("windir"));
    filename += "\\license.txt";
    
    ofstream license(filename.c_str());
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    15
    ...

    %windir% is a windows command that brings you to the Windows folder...I really don't know if you can use it or not...

    like I said I'm a noob at programming, so...yeah...

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Trust me, it is an environment variable. I just tried my code and it worked.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    15
    okay thank you so much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. File being filled with NULLs
    By Tigers! in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2009, 05:28 PM
  3. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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