#include
#include
using namespace std;
int main()
{
char str[12];
ifstream ifile;
ifile.open("test", ios::out);
if (!ifile)
{
cerr << "Can't open test file." << endl;
system("pause");
return 0;
}
ifile >> str;
cout << str << endl;
ifile.close();
system("pause");
return 1;
}
为什么总是出现这样的情况.......那个test文件真的不用写后缀吗?
需要写后缀。"test.txt", 如果test.txt不在程序所在目录或不在工作目录下,还需要完整路径
应该是读取路径不对,指定读取路径应该就可以读到,在试试看
而且你想读取内容而不是输出内容,不应该指定ios::out,ifstream默认指定了ios::in
以下是CPP I/O相关的API说明文档, 含有demo.
CPP Stream IO and File IO](https://www.ntu.edu.sg/home/ehchua/programming/cpp/cp10_IO.html "CPP Stream IO and File IO")
老哥, 这类问题建议认真阅读一下官方文档, 耐心点, 不要急于求成, 打好基础很重要的.
ofstream ofile;
ofile.open("test.txt", ios::out);
试试
ifile.open("test", ios::out);
if (!ifile)
在test后加上TXT后缀名试一下
知道了....应该和cpp文件放在同一个目录下...
不过还是要谢谢大家的帮助了