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)
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)
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; }
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?
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
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; }
Are you sure those are the errors? I don't see anything wrong with cin or cout.
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)
ah, you have to #include <string>
sorry, i forgot to add that in my example.
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?
--------------------Configuration: Cpp21 - Win32 Debug--------------------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; }
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)
the c_str() fxn converts the string to a c-string because ifstream takes a c-string argument.Code:in_stream.open(temp.c_str())