Opening a file from within a program

This is a discussion on Opening a file from within a program within the C++ Programming forums, part of the General Programming Boards category; How would you write a program that asks the user what the path of the file that they want to ...

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    Thumbs up Opening a file from within a program

    How would you write a program that asks the user what the path of the file that they want to open is (including the file extension ofcourse) and then the program opens it?

    Thanks
    -Chris

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    As usual, the answer is "depends", in this case upon what you want to do with the file. There are two common ways of using files for regular I/O, one dates from C times, but is still perfectly valid, works, and is widely used in C++ circles. This would use the fopen() function. You then read with fscanf() and write with fprintf(), (and others).

    The C++ I/O system is stream based. To use this, you declare an object of fstream type, (or ifstream/ofstream), and then call the .open() member of that object, (or use an overloaded constructor which takes a filename). You then read with the >> operator and write with the << operator, (called the stream extraction and stream insertion operators by the way).

    It also depends upon the type of file you are working with. Some files are text based and formatted, others are unformatted binary data. Each has it's own quirks and options.

    A third way to open a file is to use the CreateFile() API routine. Regardless of the name, this will open an existing file, (or port, or directory, or disk etc...). What it does is create a file handle. There are then a series of API function for manipulating files by handle.

    All of these open options will accept a string containg a file name. A real basic skeleton would be something like...

    ifstream File;
    char FileName[MAX_PATH];
    ...
    get the filename and store in FileName
    ...
    File.open(FileName);
    File >> data;
    File.close();

    ... this would open a text file for input.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    As usual, you've come through for me, adrian. I already know how to open a file, but i'd like to know how to get the user to type in the PATH OF THE FILE (eg. C:\My Documents\pic.jpg) instead of just the file name (eg. pic.jpg). I want to do this becuase the way I know, you can only type the file name, thus making it imposible to open a file which isn't in the same directory as the program itself.

    I hope what I am saying makes sense.

    Thanks
    -Chris

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    You can try this:

    char filename[200];
    cout << "User: please enter path name using following format: " << endl;
    cout << "A:\\Programs\\FirstDirectory\\FirstSubDirectory\\ SecondSubdirectory\\filename.fileExtension." << endl;
    cout << "remember to use the double backslashes!!!" << endl;

  5. #5
    Ethereal Raccoon Procyon's Avatar
    Join Date
    Aug 2001
    Posts
    189
    Actually, I don't think you would want the user to use double backslashes. That's only if you're coding the path in directly, since a single backslash is interpreted by the compiler. Once it's past the compiler, backslashes are interpreted normally in scanf(), argv[], and so on.

  6. #6
    Registered User ski6ski's Avatar
    Join Date
    Aug 2001
    Posts
    133
    You could always use the common dialogs....OpenFile()
    C++ Is Powerful
    I use C++
    There fore I am Powerful

  7. #7
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    Alrighty...here's some code.

    Code:
    cout << "Enter the path of the file that you would like to open\n";
    string path;
    cin >> path;
    cout << "\n";
    
    output.open(Path.c_str());
    now how would I change this code to make it open the file according to the path that the user enters (at the moment this program opens/attempts to open the file in the same directory as the program with a name matching what the user enters)

  8. #8
    Registered User ski6ski's Avatar
    Join Date
    Aug 2001
    Posts
    133
    Here is some code to use the one of the common dialogs. This is with windows, using mfc.

    Code:
    void CFilenameDlg::OnBrowse() //dialog button Browse
    {
         CFileDialog theFileDialog(TRUE,"cpp",NULL,OFN_HIDEREADONLY|OFN_FILEMUSTEXIST,"cpp files(*.cpp)|*.cpp||");
         if(theFileDialog.DoModal()==IDOK)
         {
              csFile= theFileDialog.GetPathName();
              m_display1.SetWindowText(csFile);//m_display1 is a member variable for an edit box
         }		
    }
    I made a little dialog with a button [Browse] and an edit box with a member variable for this project. So that when you click [Browse] it brings up the common dialog and when you select one of the files it puts it in the edit box to be opened. If this does not work, i will try again and see if i left anything out.
    C++ Is Powerful
    I use C++
    There fore I am Powerful

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 03:49 PM
  3. Last program!!
    By buckwheat88 in forum C++ Programming
    Replies: 12
    Last Post: 01-17-2006, 11:31 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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