#include "pch.h"
#include <iostream>
#include<fstream>
using namespace std;
int main() {
char N[20];
string str("iostream_exercise.txt");
ifstream in(str);
in.open(str);
if (in.is_open())
in >> N;
else
cout << "读取失败" << endl;
for (int j = 0; j < 20; j++) {
cout << N[j] << " ";
}
in.close();
}
文本文件内容是:woaini 文本文件编码方式为ANSI 放在了本工程文件内,运行在VS2017。
输出:
读取失败
???????????????????
请赐教,感谢!
请赐教!
string str("iostream_exercise.txt");
看下这个文件所在的路径,是不是和程序所在的路径相同
用完整路径
string str("x:\path\iostream_exercise.txt"); 这样的,试试看
然后检查下权限是不是有问题,用管理员权限运行看看
这个文件不能放在工程目录,要放在.exe的输出目录,否则请使用绝对路径