-1

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
  • What happens when you run the program? –  Jul 08 '17 at 18:55
  • 3
    "trouble" and "does not work" are the opposite of descriptive. What *actually* goes wrong? What output do you get, if any? Can you include an example of the text file that isn't working? – Cody Gray - on strike Jul 08 '17 at 18:56
  • Kindly check the error code using 'strerror(errno);'. Most probably the file path issue. Alternatively you can validate the same by providing complete path instead of relative. – Hemant Gangwar Jul 08 '17 at 18:56
  • Can you explain how I would check the error code? – Scott Proctor Jul 08 '17 at 18:58
  • Please check following link for the same: https://stackoverflow.com/questions/17337602/how-to-get-error-message-when-ifstream-open-fails – Hemant Gangwar Jul 08 '17 at 19:00
  • I have edited my post to give a description about what happens when I run the program. Sorry for the confusion. – Scott Proctor Jul 08 '17 at 19:03
  • Using stderror(erno) right after ifstream inf("BTree5_1.txt"), I get "Error: No such file or directory.." Can someone please explain why? As explained in the post, I moved my text file from the Resources Folder to the Sources Folder in the Solution Explorer. – Scott Proctor Jul 08 '17 at 19:15
  • right click the project in the solution explorer, select properties. Look at Configuration Properties, Debugging, Working Directory. This will be "cwd" when you run the application in the debugger. Optionally you can just have main print the cwd to double check, or provide the full path to the file. If it says something like "$(ProjectDir)" click the dropdown on the right, click the "Macros>>" button, and see what the actual value of ProjectDir is. – Kenny Ostrom Jul 08 '17 at 20:14
  • Did you make sure your files were not "BTree5_1.txt.txt"? I have seen this recently in this question: https://stackoverflow.com/questions/44935386/c-createfile-does-not-found-txt-file-in-same-folder-as-exe#comment76847095_44935386 – drescherjm Jul 08 '17 at 20:20
  • User rights? Non-printable characters in the file name? Try listing the files that exist, try using process monitor, try creating a new file without copy-pasting or transferring it from elsewhere etc. – James Z Jul 08 '17 at 22:28
  • Hi Kenny. My project name is p2. Under "Macros>>" my "$(ProjectDir)" is"C:\Users\s.proctor\Documents\Visual Studio 2015\Projects\ecs60\p2\p2\p2." Should there be 3 p2's? – Scott Proctor Jul 09 '17 at 02:30

1 Answers1

0

The problem was that I copied and pasted my text files from the Unix Environment. To fix this I just placed the text files into my Directory from my C Drive.

ie>) C:\Users\s.proctor\Documents\Visual Studio 2015\Projects\ecs60\p2\p2\p2