编写一个包含带参数的main函数的程序,实现为指定的文本文件创建一个副本文件的命令。
【测试数据】
命令为:16_4 D:\a.txt C:\b.txt
【运行结果】
在C盘根文件夹下创建文件“b.txt”,并将D盘下的文件“a.txt”内容复制到“b.txt”中。
#include
int main(int argc,char *argv[])
{
FILE *fp;
fp=fopen("a.txt","r");
if(fp== NULL) {
puts ("Can't open file .");
exit (1);
}
剩下的就不会了
#include<stdio.h>
int main() {
char ch;
FILE *source, *target;
char source_file[]="D:\\a.txt";
char target_file[]="C:\\b.txt";
source = fopen(source_file, "r");
if (source == NULL) {
printf("Press any key to exit...\n");
return -1;
}
target = fopen(target_file, "w");
if (target == NULL) {
fclose(source);
printf("Press any key to exit...\n");
return -1;
}
while ((ch = fgetc(source)) != EOF)
fputc(ch, target);
printf("File copied successfully.\n");
fclose(source);
fclose(target);
return 0;
}
主函数
system("copy c:\abc.txt d:\abc.txt");
#include<stdio.h>
int main() {
char ch;
FILE *source, *target;
char source_file[]="D:\\a.txt";
char target_file[]="C:\\b.txt";
source = fopen(source_file, "r");
if (source == NULL) {
printf("Press any key to exit...\n");
return -1;
}
target = fopen(target_file, "w");
if (target == NULL) {
fclose(source);
printf("Press any key to exit...\n");
return -1;
}
while ((ch = fgetc(source)) != EOF)
fputc(ch, target);
printf("File copied successfully.\n");
fclose(source);
fclose(target);
return 0;
}
#include <stdio.h>
#include <string.h>
int main(){
freopen("D:/a.txt", "r", stdin);
freopen("C:/b.txt", "w", stdout);
char str[1024];
while (gets(str)){
printf("%s\n", str);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h> // For exit()
int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
printf("Enter the filename to open for reading \n");
scanf("%s", filename);
// Open one file for reading
fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
printf("Enter the filename to open for writing \n");
scanf("%s", filename);
// Open another file for writing
fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
// Read contents from file
c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}
printf("\nContents copied to %s", filename);
fclose(fptr1);
fclose(fptr2);
return 0;
}