fstream 打开文件失败问题 ==

时间:2022-10-18 00:32:40
最近使用到了<fstream>
在生成无向网的时候,手动输入比较麻烦,于是想要利用txt文件进行输入,但无论怎么改路径,文件都没有打开,于是最基本的测试了一下.open()函数,结果发现每次都打不开,这是怎么回事呢???
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main()
{
fstream b;
int* a;
a = new int[10];
b.open("‪‪C:\tmp\tmp.txt");
if (!b)
cout << "文件打开失败" << endl;
for (int i = 0; i < 10; i++)
b >> a[i];
for (int i = 0; i < 10; i++)
cout << setw(3) << a[i];
b.close();
system("pause");
    return 0;
}
每次都输出“文件打开失败”

3 个解决方案

#1


参考 http://blog.csdn.net/kingstar158/article/details/6859379

#2


b.open("‪‪C: \\tmp \\tmp.txt"); 试试

#3


\是转义字符,比如\n,\t等等
要用\\才是\符号本身
谭浩强的书都有,我猜测一般C语言教材都有这个细节。

#1


参考 http://blog.csdn.net/kingstar158/article/details/6859379

#2


b.open("‪‪C: \\tmp \\tmp.txt"); 试试

#3


\是转义字符,比如\n,\t等等
要用\\才是\符号本身
谭浩强的书都有,我猜测一般C语言教材都有这个细节。