Cannot open text file in Visual Studio Community 2017 v.15.7.6 - tried everything
Clash Royale CLAN TAG#URR8PPP
Cannot open text file in Visual Studio Community 2017 v.15.7.6 - tried everything
I am trying to read coordinates from a text file to initialize a 2d array, stored here in a custom object called boolMatrix. However, I cannot for the life of me get Visual Studio to find my file, names data.txt. I have changed the directory, added the file in every way imaginable, and even tried using the file path. Nothing has worked. Here is the function that is supposed to initialize the boolMatrix object matrix1 to the values stored in data.txt:
void setMatrix(boolMatrix &matrix1)
ifstream inFile;
string filename = "data.txt";
inFile.open(filename);
while (filename != "quit")
if (inFile)
int a, b;
while (inFile >> a >> b)
matrix1.set(a, b, true);
inFile.close();
else
cout << "Error, couldn't open file." << endl;
inFile.close();
cout << "Enter the name of the file (type ""quit"" to quit): ";
cin >> filename;
inFile.open(filename);
Here is a better example to use as it will not give you a false negative when it loops and the file was already read from. ideone.com/iIeZqj
– Retired Ninja
Aug 12 at 2:17
Ok, now it works. I followed your advice and deleted everything after the error message, the first line after my else statement. Then I make filename = quit. Now it works. Thank you. This is what happens when you reuse your own code from a completely different program and expect it to integrate properly.
– Tyler Brassel
Aug 12 at 2:37
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Your code works fine for me once I turned it into a complete example. I placed the data.txt file in the directory I have set as the working directory when the executable is launched from the ide. You'll need to provide more details on the behavior you're seeing and how you're determining the file is not opened. To start I would remove everything but the first line from your else since it will say the file could not be opened even if it was successfully opened earlier and read until the end..
– Retired Ninja
Aug 12 at 2:13