13

I know this is a noob question, but I've worked with Python before and when you wanted to simply access a .txt file for example, all you had to do was make sure the txt file was in the same directory. I have the following C++ code below but it's not finding the Numbers.txt file that I have saved on my desktop. All I have in the file is one line of numbers of type double. All I want to do is to find the average of all of the numbers in the file. The program runs fine, but it doesn't print the output correctly. After checking to see what is printing into output by just printing output[0], I've discovered that the file is not copying it's contents into the array. Could someone clear this little problem up for me or at least point me in the right direction to a good tutorial?

int main() {
    cout << "Getting File Information..." << endl;
    ifstream file;
    char output[100];
    //int x;

    file.open("Numbers.txt", ios::in);    // open file

    cout << "Opened File Successfully ****************" << endl;
    file >> output;              // empty file contents into output
    cout << output;              // print out contents of file
    cout << "Should have printed out results by now" << endl;
    //file >> x;

    file.close();

    return 0;
}
Juri
  • 32,424
  • 20
  • 102
  • 136
Josh Bradley
  • 4,630
  • 13
  • 54
  • 79

4 Answers4

7

Visual Studio sets the working directory to YourProjectDirectory\Debug\Bin when running in debug mode. If your text file is in YourProjectDirectory, you need to account for that difference.

The easiest way to do that is to include your text files in the project and set their build action (in the Properties window) to Content.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • 2
    You are talking about C# or VB.Net not C++ – Cem Kalyoncu Sep 18 '09 at 22:37
  • 2
    Maybe the answer need some revise, I have the same question but I didn't find the `\Bin` sub-folder under `\Debug` on VS2015. Luckily, the second method still works. – Jaege Jan 28 '16 at 02:17
4

If you're talking about running the code within the Visual Studio debugger via F5 or Debug / Start Debugging, you can set the working directory of your program via Project / <Project name> Properties / Configuration / Debugging / Working directory.

Put your text file in a directory somewhere, and set Working directory to point to that directory.

RichieHindle
  • 272,464
  • 47
  • 358
  • 399
  • 2
    Never use an absolute path. It will break when moved to another computer. – Cem Kalyoncu Sep 18 '09 at 22:41
  • The Working Directory setting isn't stored in the project file anyway, so that's not an issue. – RichieHindle Sep 18 '09 at 22:44
  • Think of this way, you set working dir to c:\somedir and placed config files, move the program elsewhere it will not work *properly* – Cem Kalyoncu Sep 18 '09 at 22:46
  • 1
    This doesn't seem to work with visual studio 2013 within a unit test project. Maybe unit test projects are special kinds. When I write a unit test that requires an input file I always have to specify a path relative to the location of the executable regardless of what I set working directory property to. It's quite strange. I always thought that was the purpose of the working directory property too. In hindsight if you need to be able to run the exe outside of the debugger it is not good to use working directory property anyway. – shawn1874 Nov 29 '16 at 19:22
4

I just had this same problem, and I didn't find any of those answers to work. Then I remembered what I learned a long time ago in OOP. What you have to do is take that text file on your desktop, and find the project folder in your visual studio projects within your computers documents, and put the text file in that folder outside of visual studio. Then in visual studio under source files, right click-> add existing item->(your text file)

:)

btw I bumped this thread because this thread said it was a good idea, and I wanted it updated for the sake of people googling the same question. https://meta.stackexchange.com/questions/125965/is-bumping-old-questions-allowed

Community
  • 1
  • 1
J L
  • 367
  • 1
  • 5
  • 11
0

Working path is project directory.

Cem Kalyoncu
  • 14,120
  • 4
  • 40
  • 62