Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 复制文件的系统调用 #include <stdlib.h> int main(int argc, char *argv[]) { FILE *from, *to; char ch; if(argc!=3) { printf("Usage: copy \n"); exit(1); } /* open source file */ if((from = fopen(argv[1], "rb"))==NULL) { printf("Cannot open source file.\n"); exit(1); } /* open destination file */ if((to = fopen(argv[2], "wb"))==NULL) { printf("Cannot open destination file.\n"); exit(1); } /* copy the file */ while(!feof(from)) { ch = fgetc(from); if(ferror(from)) { printf("Error reading source file.\n"); exit(1); } if(!feof(from)) fputc(ch, to); if(ferror(to)) { printf("Error writing destination file.\n"); exit(1); } } if(fclose(from)==EOF) { printf("Error closing source file.\n"); exit(1); } if(fclose(to)==EOF) { printf("Error closing destination file.\n"); exit(1); } return 0; } #包括 #包括 int main(int argc,char*argv[]) { 文件*从,*到; char ch; 如果(argc!=3){ printf(“用法:复制\n”); 出口(1); } /*开源文件*/ if((from=fopen(argv[1],“rb”))==NULL){ printf(“无法打开源文件。\n”); 出口(1); } /*打开目标文件*/ if((to=fopen(argv[2],“wb”))==NULL){ printf(“无法打开目标文件。\n”); 出口(1); } /*复制文件*/ 而(!feof(from)){ ch=fgetc(来自); 如果(费罗(来自)){ printf(“读取源文件时出错。\n”); 出口(1); } 如果(!feof(from))fputc(ch,to); if(费罗(to)){ printf(“写入目标文件时出错。\n”); 出口(1); } } 如果(fclose(from)=EOF){ printf(“关闭源文件时出错。\n”); 出口(1); } 如果(fclose(to)=EOF){ printf(“关闭目标文件时出错。\n”); 出口(1); } 返回0; }_C++ - Fatal编程技术网

C++ 复制文件的系统调用 #include <stdlib.h> int main(int argc, char *argv[]) { FILE *from, *to; char ch; if(argc!=3) { printf("Usage: copy \n"); exit(1); } /* open source file */ if((from = fopen(argv[1], "rb"))==NULL) { printf("Cannot open source file.\n"); exit(1); } /* open destination file */ if((to = fopen(argv[2], "wb"))==NULL) { printf("Cannot open destination file.\n"); exit(1); } /* copy the file */ while(!feof(from)) { ch = fgetc(from); if(ferror(from)) { printf("Error reading source file.\n"); exit(1); } if(!feof(from)) fputc(ch, to); if(ferror(to)) { printf("Error writing destination file.\n"); exit(1); } } if(fclose(from)==EOF) { printf("Error closing source file.\n"); exit(1); } if(fclose(to)==EOF) { printf("Error closing destination file.\n"); exit(1); } return 0; } #包括 #包括 int main(int argc,char*argv[]) { 文件*从,*到; char ch; 如果(argc!=3){ printf(“用法:复制\n”); 出口(1); } /*开源文件*/ if((from=fopen(argv[1],“rb”))==NULL){ printf(“无法打开源文件。\n”); 出口(1); } /*打开目标文件*/ if((to=fopen(argv[2],“wb”))==NULL){ printf(“无法打开目标文件。\n”); 出口(1); } /*复制文件*/ 而(!feof(from)){ ch=fgetc(来自); 如果(费罗(来自)){ printf(“读取源文件时出错。\n”); 出口(1); } 如果(!feof(from))fputc(ch,to); if(费罗(to)){ printf(“写入目标文件时出错。\n”); 出口(1); } } 如果(fclose(from)=EOF){ printf(“关闭源文件时出错。\n”); 出口(1); } 如果(fclose(to)=EOF){ printf(“关闭目标文件时出错。\n”); 出口(1); } 返回0; }

C++ 复制文件的系统调用 #include <stdlib.h> int main(int argc, char *argv[]) { FILE *from, *to; char ch; if(argc!=3) { printf("Usage: copy \n"); exit(1); } /* open source file */ if((from = fopen(argv[1], "rb"))==NULL) { printf("Cannot open source file.\n"); exit(1); } /* open destination file */ if((to = fopen(argv[2], "wb"))==NULL) { printf("Cannot open destination file.\n"); exit(1); } /* copy the file */ while(!feof(from)) { ch = fgetc(from); if(ferror(from)) { printf("Error reading source file.\n"); exit(1); } if(!feof(from)) fputc(ch, to); if(ferror(to)) { printf("Error writing destination file.\n"); exit(1); } } if(fclose(from)==EOF) { printf("Error closing source file.\n"); exit(1); } if(fclose(to)==EOF) { printf("Error closing destination file.\n"); exit(1); } return 0; } #包括 #包括 int main(int argc,char*argv[]) { 文件*从,*到; char ch; 如果(argc!=3){ printf(“用法:复制\n”); 出口(1); } /*开源文件*/ if((from=fopen(argv[1],“rb”))==NULL){ printf(“无法打开源文件。\n”); 出口(1); } /*打开目标文件*/ if((to=fopen(argv[2],“wb”))==NULL){ printf(“无法打开目标文件。\n”); 出口(1); } /*复制文件*/ 而(!feof(from)){ ch=fgetc(来自); 如果(费罗(来自)){ printf(“读取源文件时出错。\n”); 出口(1); } 如果(!feof(from))fputc(ch,to); if(费罗(to)){ printf(“写入目标文件时出错。\n”); 出口(1); } } 如果(fclose(from)=EOF){ printf(“关闭源文件时出错。\n”); 出口(1); } 如果(fclose(to)=EOF){ printf(“关闭目标文件时出错。\n”); 出口(1); } 返回0; },c++,C++,在C++标准库中还没有文件系统库,所以您有几个选择: 使用特定于平台的API:您将使用特定于操作系统的不可移植功能,但这将满足您的需要 使用(跨平台)库:一个好的库是boost::filesystem,但我不确定它是否允许移动/复制文件,只需检查文档中的函数即可 使用std::system()调用特定于操作系统的命令行命令:std::system(“copy fileA.txt fileB.txt”或类似命令应该可以在windows上工作,它是特定于平台的,从安全角度来看可能很危险,但它可以工

在C++标准库中还没有文件系统库,所以您有几个选择:

  • 使用特定于平台的API:您将使用特定于操作系统的不可移植功能,但这将满足您的需要
  • 使用(跨平台)库:一个好的库是boost::filesystem,但我不确定它是否允许移动/复制文件,只需检查文档中的函数即可
  • 使用std::system()调用特定于操作系统的命令行命令:std::system(“copy fileA.txt fileB.txt”或类似命令应该可以在windows上工作,它是特定于平台的,从安全角度来看可能很危险,但它可以工作

C++标准库中还没有文件系统库,所以您有几个选择:

  • 使用特定于平台的API:您将使用特定于操作系统的不可移植功能,但这将满足您的需要
  • 使用(跨平台)库:一个好的库是boost::filesystem,但我不确定它是否允许移动/复制文件,只需检查文档中的函数即可
  • 使用std::system()调用特定于操作系统的命令行命令:std::system(“copy fileA.txt fileB.txt”或类似命令应该可以在windows上工作,它是特定于平台的,从安全角度来看可能很危险,但它可以工作

此代码用C语言复制一个文件。您可以修改它以复制到多个文件

#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { FILE *from, *to; char ch; if(argc!=3) { printf("Usage: copy \n"); exit(1); } /* open source file */ if((from = fopen(argv[1], "rb"))==NULL) { printf("Cannot open source file.\n"); exit(1); } /* open destination file */ if((to = fopen(argv[2], "wb"))==NULL) { printf("Cannot open destination file.\n"); exit(1); } /* copy the file */ while(!feof(from)) { ch = fgetc(from); if(ferror(from)) { printf("Error reading source file.\n"); exit(1); } if(!feof(from)) fputc(ch, to); if(ferror(to)) { printf("Error writing destination file.\n"); exit(1); } } if(fclose(from)==EOF) { printf("Error closing source file.\n"); exit(1); } if(fclose(to)==EOF) { printf("Error closing destination file.\n"); exit(1); } return 0; } #包括 #包括 int main(int argc,char*argv[]) { 文件*从,*到; char ch; 如果(argc!=3){ printf(“用法:复制\n”); 出口(1); } /*开源文件*/ if((from=fopen(argv[1],“rb”))==NULL){ printf(“无法打开源文件。\n”); 出口(1); } /*打开目标文件*/ if((to=fopen(argv[2],“wb”))==NULL){ printf(“无法打开目标文件。\n”); 出口(1); } /*复制文件*/ 而(!feof(from)){ ch=fgetc(来自); 如果(费罗(来自)){ printf(“读取源文件时出错。\n”); 出口(1); } 如果(!feof(from))fputc(ch,to); if(费罗(to)){ printf(“写入目标文件时出错。\n”); 出口(1); } } 如果(fclose(from)=EOF){ printf(“关闭源文件时出错。\n”); 出口(1); } 如果(fclose(to)=EOF){ printf(“关闭目标文件时出错。\n”); 出口(1); } 返回0; }
此代码以C语言复制一个文件。您可以修改它以复制多个文件

#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { FILE *from, *to; char ch; if(argc!=3) { printf("Usage: copy \n"); exit(1); } /* open source file */ if((from = fopen(argv[1], "rb"))==NULL) { printf("Cannot open source file.\n"); exit(1); } /* open destination file */ if((to = fopen(argv[2], "wb"))==NULL) { printf("Cannot open destination file.\n"); exit(1); } /* copy the file */ while(!feof(from)) { ch = fgetc(from); if(ferror(from)) { printf("Error reading source file.\n"); exit(1); } if(!feof(from)) fputc(ch, to); if(ferror(to)) { printf("Error writing destination file.\n"); exit(1); } } if(fclose(from)==EOF) { printf("Error closing source file.\n"); exit(1); } if(fclose(to)==EOF) { printf("Error closing destination file.\n"); exit(1); } return 0; } #包括 #包括 int main(int argc,char*argv[]) { 文件*从,*到; char ch; 如果(argc!=3){ printf(“用法:复制\n”); 出口(1); } /*开源文件*/ if((from=fopen(argv[1],“rb”))==NULL){ printf(“无法打开源文件。\n”); 出口(1); } /*打开目标文件*/ if((to=fopen(argv[2],“wb”))==NULL){ printf(“无法打开目标文件。\n”); 出口(1); } /*复制文件*/ 而(!feof(from)){ ch=fgetc(来自); 如果(费罗(来自)){ printf(“读取源文件时出错。\n”); 出口(1); } 如果(!feof(from))fputc(ch,to); if(费罗(to)){ printf(“写入目标文件时出错。\n”); 出口(1); } } 如果(fclose(from)=EOF){ printf(“关闭源文件时出错。\n”); 出口(1); } 如果(fclose(to)=EOF){ printf(“关闭目标文件时出错。\n”); 出口(1); } 返回0; } 明目张胆地从

窃取,您可以使用该功能

#包括
...
std::system(“cp from.txt to.txt”);
您可以使用该功能

#包括
...
std::system(“cp from.txt to.txt”);

小心使用“系统”功能,这就像从DOS窗口复制文件一样,基本上是绕过窗口。

小心使用“系统”函数,就像从DOS窗口复制文件,基本上是绕过窗口。

C++本身没有系统调用,你是说如何给系统添加一个系统调用来做这个?或者如何真正地调用它。应该是,我喜欢在C++上写一个代码,指示从另一个目的地复制一个文件,是的。至少,向我的操作系统添加一个系统调用以及如何进行调用是两者兼而有之。我的问题不是“是”或“否”的问题。但无论如何,为了开始讨论系统调用,您需要告诉我们您使用的是什么操作系统。其次,根据您的操作系统,您可能需要专门使用C来创建新的系统调用(例如linux)C++本身没有系统调用,你是说如何给你的OS添加系统调用来做这个?或者如何真正地调用它。应该是,我喜欢写C++上的代码,指示从另一个目的地复制文件,是的,这是两个,向我的OS添加系统调用以及如何调用它。这不是一个肯定或否定的问题。但无论如何,为了开始讨论系统调用,您需要告诉我们您使用的操作系统。其次,根据您的操作系统,您可能需要专门使用C来创建新的系统调用(例如linux)。您假设这是在Windows上。OP没有指定。它可能是DOS命令,但仍然通过操作系统层。Joe White-True,这将取决于命令行界面在该平台上的工作方式,但相同的代码可能无论如何都不可移植,因为命令可能不同。Martin York-True,但使用DOS命令行有时会引起问题,尤其是删除,因为您没有回收站支持。无论如何,我会考虑使用“系统”。作为一个黑客,所以我一般会避免它,只是我的意见。你假设这是在Windows上。OP没有指定。它可能是一个DOS命令,但仍然通过操作系统层。Joe White-是的,这将取决于命令行界面在该平台上的工作方式,但同样的代码可能无论如何都不可移植,因为该命令会可能会有所不同。Martin York-没错,但是使用DOS命令行有时会导致问题