C++ Trouble Reading a Text File

后端 未结 2 1959
孤街浪徒
孤街浪徒 2020-12-19 16:17

I\'m trying to read a text file but nothing is coming out. I feel like maybe It\'s not linking correctly in my Visual Studio Resources folder but if I double click it - it o

2条回答
  •  离开以前
    2020-12-19 17:07

    You try to open file by name without path, this means the file shall be in current working directory of your program.

    The problem is with current directory when you run your program from VS IDE. VS by default sets current working directory for runnning program to project directory $(ProjectDir). But your test file resides in resources directory. So open() function could not find it and getline() immediately fails.

    Solution is simple - copy your test file to project directory. Or copy it to target directory (where your program .exe file is created, typically $(ProjectDir)\Debug or $(ProjectDir)\Release) and change working directory setting in VS IDE: Project->Properties->Debugging->Working Directory, set to $(TargetDir). In this case it will work both from IDE and command line/Windows Explorer.

    Another possible solution - set correct path to file in your open() call. For testing/education purposes you could hardcode it, but actually this is not good style of software development.

提交回复
热议问题