1

So, I am using https://stackoverflow.com/a/298713/1472828 to put an argument "hands.txt" (my agrv[1], which is a file I wanna open) in my command arguments. I have tried both hands.txt and "hands.txt", neither of them worked.

int FileParsing(vector<Card> & v, char * FileName) {
    ifstream ifs;
    ifs.open(FileName);
    if (!ifs.is_open()){
        cout << "file cannot be opened." << endl;
    } else {

So I use debugger to step through my main:

int main(int argc, char * argv[]){
    if (argc !=2 ){
        //ErrorMessage();
    } else {
         ...

Debugger tells me that my argc is 2, which is right, but how come every time the debugger just goes to

cout << "file cannot be opened." << endl;

which means the argument just fails at reading it

ifstream ifs;
ifs.open(FileName);

Is there something I missed or I passed the argument in a wrong way?

p.s. The text file was read perfectly from cmd, so it's not the problem of code.

Community
  • 1
  • 1
HoKy22
  • 4,057
  • 8
  • 33
  • 54
  • 4
    Crazy idea: since you're sending everything *but* the `argv[1]` to `cout`, maybe let that join in the fun as well. At least then you know you have the right argument. The argument is passed to the program from the IDE from the Program Arguments line in the debugger configuration page of the project. Furthermore, you should be aware that **VStudio defaults to running your program from the same folder the project file (.vcxproj) is located in unless you change it; *not* the folder where the .exe resides after build.** – WhozCraig Mar 10 '13 at 00:55
  • @WhozCraig you should post that as an answer – JaredPar Mar 10 '13 at 01:03
  • Excellent I trust you also found the "Startup" folder entry as well then. Good. – WhozCraig Mar 10 '13 at 01:04
  • @JaredPar its actually a dupe. I'm sure of it because I know I've answered it at least twice in the past month or so. If I can find the dupe I'll post as such and we can close this out. – WhozCraig Mar 10 '13 at 01:04
  • possible duplicate of [Visual Studio Debugger / Cannot load a data file](http://stackoverflow.com/questions/5927940/visual-studio-debugger-cannot-load-a-data-file) – WhozCraig Mar 10 '13 at 01:07

1 Answers1

4

Got the idea from @WhozCraig, while running your program in cmd, the text file is put under debug directory. But if you run it using debugger, you have to put the text file in the same directory with other cpp and h files.

HoKy22
  • 4,057
  • 8
  • 33
  • 54