当 运行 在 python 代码中的 c++ exe 时,不要更改文本文件

When run a c++ exe in a python code, don't change text file

我有一个 C++ 代码,我在其中更改了文本文件中的字符串。 此代码 运行 正确。但是当我 运行 在 python 文件中执行此代码时, 运行 正确但不更改文本文件的字符串。 我必须做什么?

我的部分 C++ 更改文本文件:

ofstream myfile;
 myfile.open ("example.txt");
myfile << i << endl;
 myfile.close();

和我 运行 此代码在 python 中的 exe 与这部分代码:

os.system(mycppcode)

这里的问题是您编译的 cpp 二进制文件在错误的目录中运行(检查您的用户目录中的 example.txt

您需要在 python 脚本中指定可执行文件的完整路径:

os.system(os.getcwd() + '/test')