0

I am trying to get my program to read a file and use the file as input information. I put the file in the same directory as my program, but still nothing.

Here is my code:

int main()
{
    ifstream inFile;
    inFile.open("inData.txt");
    if (inFile.fail())            
    {
        cout << "file did not open please check it\n";
        system("pause");
        system("exit");
    }
    studentType sList[20];
    getData(inFile, sList, 20);
    calculateGrade(sList, 20);
    printResult(sList, 20);
    inFile.close();
    system("pause");
    return 0;
}
OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87
5MikesOut
  • 1
  • 1
  • 3
  • 1
    What compiler are you using? MSVS uses where the source files are as the working directory – NathanOliver Jul 28 '18 at 02:09
  • 1
    I believe you are running the code from an IDE, such as MSVS. So an idea is to print the directory where the program is running and make sure there is a "inData.txt" file there . Take a look at https://stackoverflow.com/a/198099/4289700. – HugoTeixeira Jul 28 '18 at 02:35
  • 2
    If your top priority is to get this working, I suggest using the complete path. Ex: inFile.open("C:\\myHome\\testFile.txt");. that is also a good practice to do in your production code. – Omi Jul 28 '18 at 07:00
  • The working directory is initialised when the process is started. It depends on how the process is started. How do you start the process? – David Heffernan Jul 28 '18 at 07:07
  • @DavidHeffernan 'Initialized' isn't a great choice of words. It is *determined* by where you are when you start the process. – user207421 Jul 28 '18 at 07:55
  • @EJP No it isn't. Initialized really is correct in at least some platforms. Look at the arguments to CreateProcess on Windows for instance. Perhaps things are different on other platforms but it would be surprising to me if the parent had no control over the initial working directory of a new child process. – David Heffernan Jul 28 '18 at 08:13
  • @DavidHeffernan in case of PDP-based (Like DOS and its successors), RT11-based or POSIX-based platform it's just an environment variable, but standard API to change it is offerered. – Swift - Friday Pie Jul 28 '18 at 08:19

1 Answers1

0

Compile and run this program from the same location your program is. Wherever it creates the file output.txt is your working directory:

#include <fstream>

int main(int argc, char **argv)
{
    std::ofstream myfile;
    myfile.open("output.txt");
    myfile << "output\n";
    myfile.close();
    return 0;
}

When you run your program, put your inData.txt file in that directory.

OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87
  • 2
    You can just pass the file name to the constructor of `std::ofstream` and let the destructor close the file for you. – eesiraed Jul 28 '18 at 04:04
  • I ran this, and it turns out it is the same directory where I originally placed my "inData.txt" file. – 5MikesOut Jul 28 '18 at 04:37
  • @5MikesOut did you run my program and yours from the exact same location? if so, can you add the ls output (or windows equivalent) of this directory? – OrenIshShalom Jul 28 '18 at 05:08