opening user specified file

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2008
Posts: 3
Reputation: J4YP33 is an unknown quantity at this point 
Solved Threads: 0
J4YP33 J4YP33 is offline Offline
Newbie Poster

opening user specified file

 
0
  #1
Dec 9th, 2008
I am trying to open a file after getting its path from the user. here is the code.

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cstring>
  5. using namespace std;
  6. void main(){
  7. const char* filepath;
  8. string input, input2;
  9. char lines[110];
  10. ifstream infile;
  11. cout << "Enter filename (including path): ";
  12. getline (cin, input);
  13. input2 = "\"" + input + "\"";
  14. filepath = input2.c_str();
  15. cout << filepath << endl;
  16. system ("pause");
  17. infile.open(filepath, ios::in);
  18. if(!infile){
  19. cerr << "Unable to open file!";
  20. exit(1);
  21. }
  22. int i = 1;
  23. while(!infile.eof()){
  24. infile.getline(lines, 110);
  25. i++;
  26. }
  27. cout << i << endl;
  28. infile.close();
  29. system ("pause");
  30. }

when i run the program and i enter
C://cars.txt
, the program closes.

now the weird thigs is when i replace this
  1. infile.open(filepath, ios::in);
with
  1. infile.open(C://cars.txt,ios::in);
it will work.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,751
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 739
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: opening user specified file

 
0
  #2
Dec 9th, 2008
>void main(){
main returns int. In C++ main returns 0 automagically, so you don't even have the excuse of saving keystrokes anymore.

>input2 = "\"" + input + "\"";
This is unnecessary. In fact, that's likely a big part of your problem.

>while(!infile.eof()){
The timing of eof() is such that this won't work. The eofbit is only set after you try and fail to read, which means that your loop has an off-by-one error.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 3
Reputation: J4YP33 is an unknown quantity at this point 
Solved Threads: 0
J4YP33 J4YP33 is offline Offline
Newbie Poster

Re: opening user specified file

 
0
  #3
Dec 9th, 2008
all right, i will take note of your advice.

anyway, the program runs smoothly if i specify the path of the file in the code. that is, replacing this
  1. infile.open(filepath, ios::in);
with this
  1. infile.open(C://cars.txt,ios::in);
.

i just want to know why it does not work when i get the user to specify the file path instead. As for this line,
  1. input2 = "\"" + input + "\"";
i am trying to concantante quotation marks to the filepath that the user specified. it will then be converted to a character array and put into infile.open(...............

is there a better way to do this? which is simply open a file using the path entered by the user
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,751
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 739
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: opening user specified file

 
1
  #4
Dec 9th, 2008
>all right, i will take note of your advice.
>anyway,
Anyway, you clearly didn't bother to fully read and comprehend my previous post because my advice included a fix for your immediate problem.

>i just want to know why it does not work when
>i get the user to specify the file path instead.
Because you're doing it wrong. You're also comparing two different pieces of code as if they were the same. Here, look:
  1. // This is what happens when you manually specify the name
  2. // Note that I fixed your habitual syntax error by adding quotes
  3. infile.open("C://cars.txt", ios::in);
  1. // This is what happens when you use the string object,
  2. // based on the exact contents of the string
  3. infile.open("\"C://cars.txt\"", ios::in);
In the second example you're trying to open the file called "C://cars.txt", where the quotations are a part of the name. That's not the case on your file system, so the specified file does not exist. Is that clear enough for you?

And for future reference, you don't need to play games with double (back)slashes with input. The only time where a backslash makes a difference is in a string literal, where the backslash introduces an escape character:
  1. const char *filename = "C:\file.txt"; // Wrong!
Since one backslash is an escape character, you need to use the escape character representing a backslash, which is two backslashes:
  1. const char *filename = "C:\\file.txt"; // Correct
This doesn't apply to forward slashes though.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 3
Reputation: J4YP33 is an unknown quantity at this point 
Solved Threads: 0
J4YP33 J4YP33 is offline Offline
Newbie Poster

Re: opening user specified file

 
0
  #5
Dec 9th, 2008
ok thanks
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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