What is the working directory of my C++ program? [duplicate]

独自空忆成欢 提交于 2021-01-29 10:58:49

问题


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;
}

回答1:


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.



来源:https://stackoverflow.com/questions/51567426/what-is-the-working-directory-of-my-c-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!