文件复制问题

qwe136ase 2012-09-10 04:14:36
#include<iostream>
#include<fstream>
#include <string>
using namespace std;
int FileCopy(string SouFile,string DirFile)
{
ifstream in;
ofstream out;
in.open(SouFile.c_str(),ios::binary);
if (in.fail())
{
cout<<"Error:Fail to open the souFile."<<endl;
in.close();
out.close();
return 0;
}
out.open(DirFile.c_str(),ios::binary);
if (out.fail())
{
cout<<"Error:Fail to open the dirFile."<<endl;
in.close();
out.close();
return 0;

}
else
{
out<<in.rdbuf();
out.close();
in.close();
return 1;
}
}
void main()
{
ifstream config;
string index;
config.open("config.txt",ios::binary);
while (getline(config,index))
{
string sou="c:/";
string dir="e:/";
sou+=index;
dir+=index;
if (FileCopy(sou,dir))
{
cout<<"成功"<<endl;
}
else
{
cout<<"失败"<<endl;
}
}
config.close();
}
其中config.txt有文件名,要求把c盘的对应文件复制到e盘,但是源文件无法打开,提示Error:Fail to open the souFile.
但我c盘的确有对应文件呀。。
...全文
323 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
superarhow 2012-09-11
  • 打赏
  • 举报
回复
win7吧,没用管理员模式运行吧
一叶之舟 2012-09-10
  • 打赏
  • 举报
回复
用CopyFile函数
nice_cxf 2012-09-10
  • 打赏
  • 举报
回复
/和\作为目录在windows下是一样的,问题不是在这里,估计是后面多了些什么其他字符
赵4老师 2012-09-10
  • 打赏
  • 举报
回复
system("copy /y file1 file2");
zhaoZero41 2012-09-10
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 的回复:]

引用 11 楼 的回复:

1.运行中输入c:/asd.txt看能否打开文件。
2.检查c:/asd.txt文件是否被其他程序打开。
3.在第一行再新建一个t1.txt测一下。

C/C++ code
void main()
{
//ifstream config;
//string index;
//config.open("config.txt",io……
[/Quote]

多了个空位的原因跟\0没有关系,\0只是字符串终止符,很有可能是你config.txt里文件名之后多加了空格。你自己再检查下。
xmu_才盛 2012-09-10
  • 打赏
  • 举报
回复
双斜杠的路径问题吗???
qwe136ase 2012-09-10
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]

1.运行中输入c:/asd.txt看能否打开文件。
2.检查c:/asd.txt文件是否被其他程序打开。
3.在第一行再新建一个t1.txt测一下。
[/Quote]
void main()
{
//ifstream config;
//string index;
//config.open("config.txt",ios::binary);
//while (getline(config,index))
//{
// string sou="c:\\";
// string dir="e:\\";
// sou+=index;
// dir+=index;
if (FileCopy("c:\\asd.txt","e:\\asd.txt"))
{
cout<<"成功"<<endl;
}
else
{
cout<<"失败"<<endl;
}
//}
//config.close();
}

这样的话就没问题了,然后我在函数入口处设了断点,发现string最后多了个空位,可能是/0引起的
zhaoZero41 2012-09-10
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

引用 9 楼 的回复:

引用 8 楼 的回复:

输出没问题
c:/asd.txt
e:/asd.txt


你这个明显就是有问题的。看4L的回复。
我按照4楼的改了,还是不行,所以我就改回来了,按照我的输出时这样的,按照四楼的输出是
c:\asd.txt
e:\asd.txt
[/Quote]

这个才是正规的文件路径,上面所有的回复都把斜杠搞错了。
这个路径是没有问题的,你确保你的c盘根目录下有asd.txt文件么?
aopha 2012-09-10
  • 打赏
  • 举报
回复
在运行输入检查c:\asd.txt看能否打开文件
检查是否有文件打开了c:\asd.txt。工具:unlock
aopha 2012-09-10
  • 打赏
  • 举报
回复
1.运行中输入c:/asd.txt看能否打开文件。
2.检查c:/asd.txt文件是否被其他程序打开。
3.在第一行再新建一个t1.txt测一下。
qwe136ase 2012-09-10
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]

引用 8 楼 的回复:

输出没问题
c:/asd.txt
e:/asd.txt


你这个明显就是有问题的。看4L的回复。
[/Quote]我按照4楼的改了,还是不行,所以我就改回来了,按照我的输出时这样的,按照四楼的输出是
c:\asd.txt
e:\asd.txt
zhaoZero41 2012-09-10
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

输出没问题
c:/asd.txt
e:/asd.txt
[/Quote]

你这个明显就是有问题的。看4L的回复。
qwe136ase 2012-09-10
  • 打赏
  • 举报
回复
输出没问题
c:/asd.txt
e:/asd.txt
zhaoZero41 2012-09-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

还是不行。。。。。。
[/Quote]
哪里不行。我自己测试只改了红色部分的代码,测试没有问题,能够正常复制。把你的config.txt里的东西发出来看看
qwe136ase 2012-09-10
  • 打赏
  • 举报
回复
还是不行。。。。。。
aopha 2012-09-10
  • 打赏
  • 举报
回复
把拼好的sou和dir cout处理看看。
zhaoZero41 2012-09-10
  • 打赏
  • 举报
回复
string sou="c:\\";
string dir="e:\\";
qwe136ase 2012-09-10
  • 打赏
  • 举报
回复
config.txt文件能打开,就在工程目录下,config文件里有asd.txt,这个在c盘下,这个打不开
aopha 2012-09-10
  • 打赏
  • 举报
回复
config.open("c:/config.txt",ios::binary);
qwe136ase 2012-09-10
  • 打赏
  • 举报
回复
所有分都给出了,求助呀,在线等

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧