| | |
opening user specified file
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 3
Reputation:
Solved Threads: 0
I am trying to open a file after getting its path from the user. here is the code.
when i run the program and i enter , the program closes.
now the weird thigs is when i replace this with it will work.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> #include <cstring> using namespace std; void main(){ const char* filepath; string input, input2; char lines[110]; ifstream infile; cout << "Enter filename (including path): "; getline (cin, input); input2 = "\"" + input + "\""; filepath = input2.c_str(); cout << filepath << endl; system ("pause"); infile.open(filepath, ios::in); if(!infile){ cerr << "Unable to open file!"; exit(1); } int i = 1; while(!infile.eof()){ infile.getline(lines, 110); i++; } cout << i << endl; infile.close(); system ("pause"); }
when i run the program and i enter
•
•
•
•
C://cars.txt
now the weird thigs is when i replace this
C++ Syntax (Toggle Plain Text)
infile.open(filepath, ios::in);
C++ Syntax (Toggle Plain Text)
infile.open(C://cars.txt,ios::in);
>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.
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.
•
•
Join Date: Dec 2008
Posts: 3
Reputation:
Solved Threads: 0
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 with this .
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, 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
anyway, the program runs smoothly if i specify the path of the file in the code. that is, replacing this
C++ Syntax (Toggle Plain Text)
infile.open(filepath, ios::in);
C++ Syntax (Toggle Plain Text)
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,
C++ Syntax (Toggle Plain Text)
input2 = "\"" + input + "\"";
is there a better way to do this? which is simply open a file using the path entered by the user
>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:
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:
Since one backslash is an escape character, you need to use the escape character representing a backslash, which is two backslashes:
This doesn't apply to forward slashes though.
>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:
C++ Syntax (Toggle Plain Text)
// This is what happens when you manually specify the name // Note that I fixed your habitual syntax error by adding quotes infile.open("C://cars.txt", ios::in);
C++ Syntax (Toggle Plain Text)
// This is what happens when you use the string object, // based on the exact contents of the string infile.open("\"C://cars.txt\"", ios::in);
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:
C++ Syntax (Toggle Plain Text)
const char *filename = "C:\file.txt"; // Wrong!
C++ Syntax (Toggle Plain Text)
const char *filename = "C:\\file.txt"; // Correct
I'm here to prove you wrong.
![]() |
Similar Threads
- Newbie C++ Search and Parsing Text file. (C++)
- Question Reading from a file and inputting into a string array I am getting a errors. (C++)
- simple file i/o problem (C++)
- opening .txt file and sorting (C++)
- Opening data file in already open app by double clicking? (Java)
- Opening a .pdf file at the clientside without the open/save dialog box (ASP.NET)
- file open code in VB (Visual Basic 4 / 5 / 6)
- Interactive text file (C++)
- Saving a file using C++ (C++)
Other Threads in the C++ Forum
- Previous Thread: C++ Radix Sort - Bitshift Operators
- Next Thread: Dynamic arrays in a class
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






