删除C++中的文件时出错
Error while deleting a file in C++
#include <iostream>
#include <string.h>
#include <fstream>
#include <conio.h>
int main()
{
int k=0;
char *source = new char[30];
char *destination = new char[30];
while(k==0)
{
cout<<"Enter the source File location: ";
cin>>source;
cout<<endl<<"Enter the destination File location: ";
cin>>destination;
ifstream is(source,ios::in | ios::binary);
ofstream os(destination,ios::out | ios::binary);
if(is==NULL || os==NULL)
{
perror("Either the input or the ouput location is invalid");
cout<<endl<<"Try again with new location.n";
cout<<endl<<"To exit press 7 and to continue press 0";
cin>>k;
}
else
{
os<<is.rdbuf();
k++;
cout<<endl<<"File moved successfully";
cout<<endl<<"Do you want to delete the original file: [y/n]";
if(getch()=='y')
{
if(remove(source)== -1)
perror("Error in deleting File");
else
cout<<" Source File deleted.";
}
}
}
}
文件已成功删除,但在删除文件时显示:
"删除文件时出错"权限被拒绝。
当它要求源地址和目标地址(如 C:/MIT/adi.txt(时,我使用了两种斜杠(向前和向后(,并且我正在使用 mingw 编译器编译它......是操作系统错误还是我的代码或编译器有任何问题?
有两件事突然出现在我面前:
-
对于大多数完整路径,30 个字符是不够的。此外,如果可以避免,则不应使用
new
;固定长度的数组也可以放在堆栈上;如果有的话,这是std::string
的工作。 -
在尝试删除文件之前,您没有关闭该文件。平台在此要求上有所不同,但我认为Windows不喜欢这样。在调用
remove
之前销毁或关闭流。
相关文章:
- 读取文件的最后一行并输入到链接列表时出错
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- .h 和.cpp文件分离时出错,但仅使用 .h 文件时没有错误.我做错了什么?
- 在 C/C++ 中打开驻留在 "/sys/" 下的文件时出错
- 在头文件中定义变量不会出错
- 如何在多写入器情况下对文件支持的共享内存中的大页面出错
- JNI,使用两个 .so 文件时出错,其中一个文件需要另一个文件
- 生成文件中隐式规则中的 -c 标志出错
- 编译时出错 - 链接.cpp和头文件
- 从人员矢量插入和读取文件时出错
- LNK2019 在文件中调用静态成员的方法时出错.cpp
- ./main:加载共享库时出错:libopencv_highgui.so.4.0:无法打开共享对象文件:没有这样的文件或
- 猫鼬 - 包含标准C++库文件时出错
- 在f2c.h文件中定义min()max()宏时出错
- ASIOSDK;从hostsample.cpp生成mex文件时出错
- 使用emscripten将c++文件转换为wasm时出错
- 将文本从一个文件复制到另一个c++流时出错
- 在C++目录中搜索文件的函数输出中出错
- 编译包含LLVM API标头的C++文件时出错
- MySQL C++连接器:添加符号时出错:文件格式无法识别