Thread: file directory.

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    151

    Question file directory.

    I made a simple notepad and was wondering if anyone knew how to let the user choose where the file is saved.
    Here is my code.
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main(){
    	string text;
    	string name;
    	string txt = ".txt";
    	ofstream test;
    	char edit;
    
    	
    	cout<<"Enter some text\n";
    	getline(cin, text);
    
    	cout<<"enter the file name\n";
    	getline(cin, name);
    	string two = name + txt;
    	test.open (two);
    	test<< text;
    	test.close();
    	system("pause");
    }
    Thank You.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Type in a path (relative to current working directory) along with the file name in the code you've already provided and it would work as is (provided the path exists).
    "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
    Nov 2009
    Posts
    151
    Quote Originally Posted by hk_mp5kpdw View Post
    Type in a path (relative to current working directory) along with the file name in the code you've already provided and it would work as is (provided the path exists).
    Could you give me an example?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You are asking the user to enter the filename. Just type in the path along with the filename at that point in your program and the file would be created there... there is no magic.
    "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

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    19
    i.e. instead of "filename", have the user enter "c:/directory/filename"

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    Quote Originally Posted by Walle View Post
    i.e. instead of "filename", have the user enter "c:/directory/filename"
    thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM