【发布时间】:2018-04-08 19:33:54
【问题描述】:
我正在尝试使用fstream 读取文件。我正在用 C++11 编写,但通过 Android Studio 中的 JNI 将它与 Java 交互。由于某种原因,它没有打开文件。我正在使用相对文件路径,但我不明白为什么它无法打开文件。该文件名为proverbs.txt。名称中没有任何差异,例如 proverbs.txt.txt 或类似名称。
代码如下:
void storeProverbs() {
string path = "/Users/tenealaspencer/Desktop/proverbs.txt";
std::ifstream provInput(path.c_str(), std::ios::in);
//provInput.open("/Users/tenealaspencer/Desktop/proverbs.txt");
// opens the proverbs text file
equivInput.open("/Users/tenealaspencer/AndroidStudioProjects/example/app/src/main/cpp/stored.txt"); // opens the stored (English) proverbs text file
if (!provInput.is_open()) {
cout << "error ";
}
while (!provInput.eof()) // while not at the end of the proverbs file
{
getline(provInput, phrase); // read proverbs in line by line
getline(equivInput, storedProv); // read english proverbs in line by line
【问题讨论】:
-
您收到任何错误吗?如果是这样,请将其包含在您的帖子中。
-
不,我没有收到错误消息或任何东西。
-
您能否验证文件是否存在于给定位置?通常程序将 bin 文件夹作为当前位置
-
是的,我也这样做了。我打开了一个 Xcode 项目并运行了完全相同的代码,它能够找到该文件。