【问题标题】:Creating a file and copying its content to other file in c languagec语言创建文件并将其内容复制到其他文件
【发布时间】:2013-06-18 18:39:56
【问题描述】:

我需要一个 C 程序将一个文件的内容复制到另一个文件,同时满足以下条件:

1.) 我们从中读取数据的文件可能存在也可能不存在。

2.) 数据被复制到的文件可能存在也可能不存在。

如果文件存在,则直接复制数据,如果文件不存在,应该有一个选项创建文件,输入数据,然后将文件内容复制到另一个文件。

我编写了以下代码,

目前我修改的代码是:

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{
 FILE *f1,*f2;
 char c;
 char name1[20],name2[20];
 clrscr();
 setvbuf(stdout, 0, _IONBF, 0);
 printf("\nEnter the source file name :");
 scanf("%s",name1);
 if((f1=fopen(name1,"r"))==NULL)
 {
  fclose(f1);
  f1=fopen(name1,"w");
  printf("\nThe specified file does not exist \nNew file will be created");
  printf("\nEnter the data for the new file : ");
  while((c=getchar())!=EOF)
  {
   putc(c,f1);
  }
  fclose(f1); 
 }
 f1=fopen(name1,"r");
 printf("\nEnter the destination file name :");
 scanf("%s",name2);
 f2=fopen(name2,"w+");
 while((c=getc(f1))!=EOF)
 {
  putc(c,f2);
 }
 rewind(f1);
 rewind(f2);
 printf("The data in the source file is :\n");
 while((c=getc(f1))!=EOF)
 {
  printf("%c",c);
 }
 printf("\nThe data in the destination file is :\n");
 while((c=getc(f2))!=EOF)
 {
  printf("%c",c);
 }
 fclose(f1);
 fclose(f2);
 fflush(stdin);
 getch();
}

但程序只有在源文件已经存在时才能正常工作。如果我创建一个新文件,则不会对目标文件名进行任何输入,并且目标文件文件中的数据为空白。那我现在该怎么办?

我应该修改什么以使其工作? 建议我您选择的任何代码...谢谢!

【问题讨论】:

  • 究竟是什么不起作用? “它不起作用”是非信息性的。
  • 如果我创建一个新文件,那么目标文件名不会被输入并且目标文件文件中的数据是空白的。

标签: c file copy file-handling copying


【解决方案1】:

好吧,我在您的代码中看到了几个问题,但最重要的是表达式:

(c=getc(f1))!=EOF

将始终评估为 true,因此您将运行无限循环。如果您阅读 getc 文档,您会发现它返回的是 int 而不是 char

getc documentation

所以基本上你正在做的是将EOF(通常定义为-1的int)截断为char,当:

c=getc(f1)  // At this point C = 255(0xFF) if getc returned EOF

然后将c 提升为intEOF 相比,因为int 很大 足以容纳 255 进行的比较是 255 != -1,这始终是正确的。

要解决这个问题,只需将 c 声明为 int

更多提示:

您可能还想通过使用feof 来确保它是文件结束条件,因为getc 在其他错误条件下也会返回EOF

您可能希望在 printf 调用中将 "\n" 移动到句子的末尾以强制刷新标准输出。或者,您也可以在程序开始时添加:

setvbuf(stdout, 0, _IONBF, 0);

您似乎将文件名存储在 name1name2 中,因此您可能希望从 fopen 文件名中删除双引号,以便实际使用它们。

当您打开name2 时,您只使用写访问权限,但在最后您尝试读取并显示其内容时,您可能希望使用"w+" 作为访问模式。

当您尝试打印结果数据时,f2 中的文件处理程序已经关闭。并且文件光标已经在文件的末尾,所以你可能想使用rewind,例如

//fclose(f2);
rewind(f1);
rewind(f2);
printf("The data in the source file is :\n");
 while((c=getc(f1))!=EOF)
 {
  printf("%c",c);
 }
 printf("The data in the destination file is :\n");
 while((c=getc(f2))!=EOF)
 {
  printf("%c",c);
 }
 fclose(f1);
 fclose(f2);

【讨论】:

  • 非常感谢@Julio Moreno
  • 我添加了 rewind(f1),rewind(f2) 语句;删除了 fclose(f2) 语句;删除了双引号;将模式更改为 w+;一切都很顺利......我没有发现 getc() 的任何问题。但是程序只有在源文件已经存在时才能正常工作。如果我创建一个新文件,则不会对目标文件名进行任何输入,并且目标文件文件中的数据为空白。那我现在该怎么办?
【解决方案2】:

最后成功的代码是:

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{
 FILE *f1,*f2;
 char c;
 char name1[20],name2[20];
 clrscr();
 printf("\nEnter the source file name :");
 scanf("%s",name1);
 if((f1=fopen(name1,"r"))==NULL)
 {
  fclose(f1);
  f1=fopen(name1,"w");
  printf("\nThe specified file does not exist \nNew file will be created");
  printf("\nEnter the data for the new file : ");
  while((c=getchar())!='^')
  {
   putc(c,f1);
  }
  fclose(f1); 
 }
 f1=fopen(name1,"r");
 printf("\nEnter the destination file name :");
 scanf("%s",name2);
 f2=fopen(name2,"w+");
 while((c=getc(f1))!=EOF)
 {
  putc(c,f2);
 }
 rewind(f1);
 rewind(f2);
 printf("The data in the source file is :\n");
 while((c=getc(f1))!=EOF)
 {
  printf("%c",c);
 }
 printf("\nThe data in the destination file is :\n");
 while((c=getc(f2))!=EOF)
 {
  printf("%c",c);
 }
 fclose(f1);
 fclose(f2);
 fflush(stdin);
 getch();
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-13
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    相关资源
    最近更新 更多