On Mar 20, 1:19 am, noobles <z3ph...@gmail. comwrote:
I'm trying to make a command line parser, but I'm not exactly sure
where to put my file.txt file so that this thing can read it. The most
logical place seemed to be the directory the project was in, but that
didn't work.
If you do not specify an absolute pathname to the file, the filename
is relative to the current working directory when you start the
project. I believe that in VS, by default, this is the directory the
EXE ends up in -- which is your output directory (like "Release" or
"Debug"), not the project directory. You could try putting it there.
You could also go to your Project Properties -Configuration
Properties -Debugging and change "Working Directory" to the
directory that contains your file. VS will then change to that
directory before starting your program if you run it from the IDE.
Another option is to call SetCurrentDirec tory() before opening the
file to set the current working directory to whatever.
Yet another option is to specify the absolute path to the file in your
program:
string fileName ="c:\\wherever\ \file.txt";
In any case, if the filename is a relative path it must be in the
current working directory. Upon starting the program, VS sets the
current working directory to the output path by default (I think). So
pick any solution that puts the file in the current working directory.
That said, this question is not on topic for comp.lang.c++. In the
future you'll probably want to head to one of the microsoft.publi c.*
newsgroups instead. This newsgroup is for specific questions about the
syntax and semantics of the C++ language itself.
Jason
>
#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main(){
ifstream file;
string fileName ="file.txt";
file.open(fileN ame.c_str());
if (!file){
cout << "Failed to open file" << endl;
return -1;
}
string newLine;
while(std::getl ine(file, newLine)) {
cout<<newLine<< endl;
}
return 1;
}