c++ fstream文本读取问题~~急啊!!!!!

来源:百度知道 编辑:UC知道 时间:2024/06/04 19:41:05
如何用fstream将本地磁盘内中文路径的文本读出,比如文档名叫"新建文本文档.txt"就是该文档已经存在了~~~里面内容也是全中文的~~~

这两天一直在研究~~~没研究明白~~~希望哪位高手能尽快解答~~我是个c++初学者~~~~希望能给个完整点的代码~~~~在下感激不尽~~~~~~

ifstream fin("新建文本文档.txt");
if(!fin)
{
cout<<"open fail!"<<endl;
exit(1);
}
char temp;
while(fin>>temp)
{
cout<<temp;
}
fin.close();

预处理中要有 #include <fstream>
.........
int main()
{
ifstream inData;//定义一个文件输入流的变量,变量名inData可自己改变
inData.open("新建文本文档.txt");//打开文件
inData >> .....//接着就可以使用了,基本上cin能用的inData都能用,按cin用就可以了
.....
inData.close();//用完记得关闭文件,否则其他文件输入流对象无法打开那个文件
......
}
记住inData的名字是自己定的ifstream inData改成ifstream inFile都可以的
如果文件名是不确定的inData.open(fileName.c_str());//fileName是个string变量,记住此时预处理要有#include<string>

下面是例子:
#include <string>
#include <fstream>

using namespace std;

int main()
{
string message;
string::size_type position;
ifstream inData;
ofstream outData;
inData.open(&quo