opening files

This is a discussion on opening files within the C++ Programming forums, part of the General Programming Boards category; i need to have the user enter what fle they want to open in my program. is there a way ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    57

    opening files

    i need to have the user enter what fle they want to open in my program. is there a way to do this so that the name they input can be used toopen the file, like this....... in_stream.open(filename)

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Code:
    #include <iostream>
    #include <fstream>
    
    int main() {
    
    std::string temp;
    
    std::cout << "Enter filename: ";
    std::cin >> temp;
    
    std::ifstream infile(temp.c_str());
    if(!infile) {
        std::cerr << "Error opening file" << std::endl;
        exit(1);
    }
    
    return 0;
    }

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    57
    thanks for the response.......couple questions though,

    1. is there anyway to do this just using the regular cin and cout?

    2. what did that last part exactly do?

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    1) Put 'using namespace std' right below the includes, and you can lop off the std:: crap.

    2) I'm assuming you're talking about the if( !inFile ) chunk. It just makes sure that there were no problems opening the file. If a problem was encountered, the program will exit.
    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

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    57
    any ideas why this code isnt working, i keepgetting two errors that have to do with the cin and cout operrators

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    struct list
    {
    	char data;
    	list *link;
        list *beginning;
        list *current;
    	list *pend;
    };
    
    typedef list* listptr;
    
    void main()
    {
    	string temp;
    	char *filename;
    	char next;
    	cout << "Enter filename";
    	cin >> temp;
    	cout << temp;
    }

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Are you sure those are the errors? I don't see anything wrong with cin or cout.

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    57
    yep......here are the errors

    --------------------Configuration: Cpp21 - Win32 Debug--------------------
    Compiling...
    Cpp21.cpp
    C:\Documents and Settings\owner\Cpp21.cpp(23) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no accepta
    ble conversion)
    C:\Documents and Settings\owner\Cpp21.cpp(24) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no accepta
    ble conversion)
    Error executing cl.exe.

    Cpp21.obj - 2 error(s), 0 warning(s)

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    ah, you have to #include <string>

    sorry, i forgot to add that in my example.

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    57
    ok i got that to work but now i'm getting an error with this code, shouldnt it work the same as the last code?
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    struct list
    {
    	char data;
    	list *link;
        list *beginning;
        list *current;
    	list *pend;
    };
    
    typedef list* listptr;
    
    void main()
    {
    	string temp;
    	char next;
    	ifstream in_stream;
    	cout << "Enter filename";
    	cin >> temp;
    	cout << temp;
    	in_stream.open(temp);
    	in_stream.get(next);
    	cout << next;
    }
    --------------------Configuration: Cpp21 - Win32 Debug--------------------
    Compiling...
    Cpp21.cpp
    C:\Documents and Settings\owner\Cpp21.cpp(26) : error C2664: 'void __thiscall std::basic_ifstream<char,struct std::char_traits<char> >:pen(const char *,int)' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<c
    har>,class std::allocator<char> >' to 'const char *'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    Error executing cl.exe.

    Cpp21.obj - 1 error(s), 0 warning(s)

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Code:
    in_stream.open(temp.c_str())
    the c_str() fxn converts the string to a c-string because ifstream takes a c-string argument.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Opening ASCII files in C?
    By Mavix in forum C Programming
    Replies: 6
    Last Post: 04-25-2007, 02:23 PM
  2. Need help opening a series of files
    By ramparts in forum C Programming
    Replies: 9
    Last Post: 11-14-2006, 04:49 PM
  3. Opening files with UNICODE file names
    By decohk in forum Linux Programming
    Replies: 2
    Last Post: 11-09-2006, 04:25 AM
  4. opening files
    By angelic79 in forum C Programming
    Replies: 3
    Last Post: 10-19-2004, 06:52 AM
  5. Opening files - Giving options?
    By wwwGazUKcom in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2001, 07:06 AM

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