0

I am trying to read a file so that I can average out the numbers listed in the file. I believe my code is correct, but I keep getting an error in Visual Studio stating, "Unable to start program ... The system cannot find the file specified." The file I want to read, "numbers.dat" is in the directory, but it still shows this error.

I'm new to C++ so I was wondering if anyone would be able to help?

Here is my code

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
    ifstream myfile;
    myfile.open("numbers.dat");
    int total = 0, count = 0, num;
    while (!myfile.eof()){
        myfile>>num;
        total += num;
        count++;
    }

    cout<<"The "<<count<<" numbers total "<<total<<" and average "<<total/count<<endl;
    myfile.close();
    system("pause");
    return 0;
}
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
  • Is your file in the same directory as code files? That is the problem – Spanky May 29 '15 at 06:34
  • Unrelated to your problem, but you need to read [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). – Some programmer dude May 29 '15 at 06:42
  • I doubt that specific message is in the output window because of a failure to open your specified file. Rather, that specific message is usually because the executable Visual Studio is trying to *load* as the debuggee cannot be loaded *at all*. Walking you through tracing down whether this is a project misconfiguration or a missing dependency (a redist DLL, for example) for such a simple program would be time consuming, and your time is likely better spent creating a new *console* project, copying your source code in, and trying with a fresh project. – WhozCraig May 29 '15 at 06:42
  • Short version: "Unable to start program" means your code isn't even loading; thus whether your *running* code is able to open the file you desire hasn't even become a problem yet, since your code isn't even running in the first place because VS isn't successfully loading it. – WhozCraig May 29 '15 at 06:45

2 Answers2

0

Spanky is most likely correct, but to provide more information -

Visual Studio compiles your program into a Build Directory. When you run your program through Visual Studio it runs from the Build Directory. By default the Build Directory is not the same folder as your source code, so your program won't find files in that are mixed in with your source code.

Possible solutions:

  • You can copy the file into your Build Directory
  • You can change your Build Directory
  • You can use absolute or relative file paths to point to the correct location
mbrandeis
  • 250
  • 1
  • 2
0

I don't use MSS for development, but it's a usual practice that people miss a thing that a directory, from where the program is launched, and the directory you think the program is launched, are different.

For example, u've got a build directory d:\project\build,

Compiled binary (.exe) is located in d:\project\build\debug directory.

Well, your numbers.dat should probably be located in d:\project\build directory and NOT in d:\project\build\debug.

dazewell
  • 396
  • 2
  • 17