I am trying to read each line of a .txt file from the command line arguments in Visual Studio. This is the code I have been using:
ifstream inFile(argv[2]); //read text file indicated by argv[2]
if (inFile.is_open() && inFile.good())
{
string line = "";
while (getline(inFile, line))
{
cout << line << '\n';
}
}
However, whenever I compile this code, I always get garbage characters. The funny thing is that the code works just fine and does what it's supposed to do when I run it directly from terminal. It's only when I use VS that I get this issue. I would appreciate any feedback as to why this is happening. Thank you!
I expect (99.9% likely) that your current working directory is different when you run in Visual Studio. There are two simple options.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.