I can not get Visual Studio to read in a text file. Below is the code I have. The file opens perfectly in a Unix Environment, but it does not work when copied and pasted into Visual Studio. I am using fstream to open the file. Why the file is not being read?
When I run the program, it builds but I get no output. I have an output statement cout << "inf\n"
. So the loop is not even being reached, which is why I believe the file is not being read. Again, when I run the same code in a Unix environment the output statement does display and the values from the file are displayed ( via tree.insert(), tree.remove() ).
I tried the solution in this link. As it suggested, I changed my working directory to $(ProjectDir)\Debug and $(ProjectDir)\Release. Also, I moved my text file from the Resources folder to my Source Folder in the Solution Explorer. However, the file still was not being read.
I also updated my code to include cerr << "Error: " << strerror(errno);
directly after fstream inf ("BTREE5_1.txt"). With this line of code the output I get is
Error: No such file or directory
Can someone please explain why? My text files are in the same folder as my code as explained above.
#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include "BTree.h"
using namespace std;
int main()
{
bool first = true;
BTree tree(6, 2);
int value;
char s[80], command;
ifstream inf("BTree5_1.txt");
cerr << "Error: " << strerror(errno);
inf.getline(s, 80);
while (inf >> command >> value)
{
cout << "inf\n";
if (command == 'i')
tree.insert(value);
else
{
if (first)
{
cout << "After all insertions.\n";
tree.print();
first = false;
} // if first
cout << "Deleting " << value << ". \n";
tree.remove(value);
tree.print();
// fgets(s, 80, stdin);
} // else deletion
} // while
system("PAUSE");
return 0;
} // main