get user input for filename to open

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2009
Posts: 8
Reputation: ss2602 is an unknown quantity at this point 
Solved Threads: 0
ss2602 ss2602 is offline Offline
Newbie Poster

get user input for filename to open

 
0
  #1
Jul 21st, 2009
i want the user to enter the name of the file to open and i want it in c++, i have seen a similr work done in vc but i m nt abl 2 implement da same in c++
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: get user input for filename to open

 
0
  #2
Jul 21st, 2009
Could you post your own try first?
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 8
Reputation: ss2602 is an unknown quantity at this point 
Solved Threads: 0
ss2602 ss2602 is offline Offline
Newbie Poster

Re: get user input for filename to open

 
0
  #3
Jul 21st, 2009
[char name[100];
ifstream infile;
cout<<"Enter filename>>";
gets(name);
infile.open(name);]

this is my code segment that is not working. i suppose i am missing something
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 382
Reputation: tomtetlaw is an unknown quantity at this point 
Solved Threads: 4
tomtetlaw's Avatar
tomtetlaw tomtetlaw is offline Offline
Posting Whiz

Re: get user input for filename to open

 
0
  #4
Jul 21st, 2009
I'm going to give you the code.
You're going to study it

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main( ) {
  5. cout << "Please enter a file name: \n";
  6. string name;
  7. cin >> name;
  8. ifstream ist(name.c_str( )); //c style strings are required by some system interfaces
  9. if(!ist) cerr << "Can't open file!\n"; // if the file failed to open
  10. cin.clear();
  11. cin.get();
  12. }

This doesn't actully do anything with the file, but it opens the file that the user requests.

I havn't tested it, so you might have to fix it up. Have fun!
Last edited by tomtetlaw; Jul 21st, 2009 at 8:30 am.
...
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 8
Reputation: ss2602 is an unknown quantity at this point 
Solved Threads: 0
ss2602 ss2602 is offline Offline
Newbie Poster

Re: get user input for filename to open

 
0
  #5
Jul 21st, 2009
thank you for the code but my compiler does not accept 'string' data type
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 382
Reputation: tomtetlaw is an unknown quantity at this point 
Solved Threads: 4
tomtetlaw's Avatar
tomtetlaw tomtetlaw is offline Offline
Posting Whiz

Re: get user input for filename to open

 
0
  #6
Jul 21st, 2009
Just use char then
...
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 8
Reputation: ss2602 is an unknown quantity at this point 
Solved Threads: 0
ss2602 ss2602 is offline Offline
Newbie Poster

Re: get user input for filename to open

 
0
  #7
Jul 21st, 2009
can you please tell me what the last 2 lines of the code are there for?
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 382
Reputation: tomtetlaw is an unknown quantity at this point 
Solved Threads: 4
tomtetlaw's Avatar
tomtetlaw tomtetlaw is offline Offline
Posting Whiz

Re: get user input for filename to open

 
0
  #8
Jul 21st, 2009
cin.clear() is for throwing away 'enter' (the button you pressed to enter the file name)

cin.get() is to tell it to wait for the user to press enter to close the window.

just ask away if you have any more questions
...
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: get user input for filename to open

 
0
  #9
Jul 21st, 2009
Tomtelaw, you forgot to add the following line to your code: #include <string>
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 382
Reputation: tomtetlaw is an unknown quantity at this point 
Solved Threads: 4
tomtetlaw's Avatar
tomtetlaw tomtetlaw is offline Offline
Posting Whiz

Re: get user input for filename to open

 
0
  #10
Jul 21st, 2009
tux4life, it's included in <iostream> .... I think

EDIT: Woops, you're right lol sorry
Last edited by tomtetlaw; Jul 21st, 2009 at 9:02 am.
...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC