为什么文件会打不开

C++语言 码拜 8年前 (2016-06-08) 690次浏览
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
/* run this program using the console pauser or add your own getch, system(“pause”) or input loop */
int main()
{
ifstream infile;
ofstream outfile;
char name[20];
char c;
cout<<“please enter filename”<<endl;
cin>>name;
infile.open(name);
if(!infile)
{
cout<<“can not open”<<endl;
exit(1);
}
cout<<“start copy”<<endl;
strcat(name,”copyfile”);
outfile.open(name);
if(!outfile)
{
cout<<“can not copy”<<endl;
exit(1);
}
while(!infile.eof())
{
infile.get(c);
outfile<<c;
}
cout<<endl;
infile.close();
outfile.close();
return 0;
}
解决方案

5

亲测题主程序打开并复制了啊,试试双斜杠进行转义咯,如”D:\Readme.txt”

5

1楼说的对,试着写绝对路径,并用双斜杠转义

40

1.确认你输入的路径是不是存在
2.路径含不含一些特殊字符
3.文件是不是有隐藏后缀
4.绝对路径试试

10

首先定位是在哪一句出错的?
你说文件打不开,是哪个文件打不开,infile还是outfile?
第二,在每个判断返回值失败的地方,打印错误代码?
cout << “GetLastError=”<< GetLastError() << “, errno = ” << errno << endl;
GetLastError,是Windows的错误代码,使用VS工具里的“错误查找”工具,可以解释每个错误代码的含义。
errno是标准C/C++的错误代码,就100来个,他的错误代码在头文件 errno.h中,最好理解这些常见错误代码的含义。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明为什么文件会打不开
喜欢 (0)
[1034331897@qq.com]
分享 (0)