【问题标题】:Copy contents of text file and copy it to another复制文本文件的内容并将其复制到另一个
【发布时间】:2011-10-18 07:56:37
【问题描述】:

这是我的教授用他自己的话告诉我的。

“编写一个程序,复制用户指定的文本文件的内容并将其复制到另一个文本文件“copy.txt”。文件中的任何行都不应超过 256 个字符。”

这是我到目前为止用他的信息设计的代码:

#include <stdio.h>
#include <io.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

int main ( void )
{
   char filename[256]="";       //Storing File Path/Name of Image to Display
   static const char file2name[] = "C:\\Users\\Rael Jason\\Desktop\\Rael's iPod\\Codeblocks\\copy.txt";
   FILE *file; 
   FILE *write;
   printf("Please enter the full path of the text file you want to copy text: ");
   scanf("%s",&filename);
   file = fopen ( filename, "r" );
   file = fopen (file2name, "r" );

   if ( file != NULL )
   {
      char line [ 256 ]; /* or other suitable maximum line size */
      char linec [256]; // copy of line

      while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
      {

         fputs ( line, stdout ); /* write the line */
         strcpy(linec, line);



         fprintf (write , linec);
         fprintf (write , "\n");
      }
      fclose (write);
      fclose ( file );
   }
   else
   {
      perror ( filename ); /* why didn't the file open? */
   }
   return 0;
}

我似乎无法正确完成文件写入?你能帮忙吗?

【问题讨论】:

  • 不应该把 file = fopen (file2name, "r" ) 写成 = fopen(file2name, "w") 吗?
  • 再次检查打开文件的行。我相信你会想出来的! :)

标签: c file copy


【解决方案1】:

您遇到了 stacker 提到的复制和粘贴问题。并且无需添加额外的换行符。试试下面的代码。

            #include <stdio.h>
            #include <io.h>
            #include <stdlib.h>
            #include <conio.h>
            #include <string.h>

            int main ( void )
            {
               char filename[256]="";       //Storing File Path/Name of Image to Display
              static const char file2name[] = "C:\\Users\\Rael Jason\\Desktop\\Rael's iPod\\Codeblocks\\copy.txt"; Copy File
               FILE *file;
               FILE *write;
               char line [ 256 ]; /* or other suitable maximum line size */
               char linec [256]; // copy of line

               printf("Please enter the full path of the text file you want to copy text: ");
               scanf("%s",&filename);                       // Enter source file
               file = fopen ( filename, "r" );
               write = fopen (file2name, "w" );

               if ( file != NULL )
               {

                  while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
                  {

                 fputs ( line, stdout ); /* write the line */
                 strcpy(linec, line);



                 fprintf (write , linec);
                // fprintf (write , "\n");         // No need to give \n
                  }
                  fclose (write);
                  fclose ( file );
               }
               else
               {
                  perror ( filename ); /* why didn't the file open? */
               }
               return 0;
            }

【讨论】:

    【解决方案2】:

    如果这是您执行的代码,那么它有一些问题。

    1. 您正在使用相同的FILE 指针文件同时打开源文件和目标文件。
    2. 您正在以读取模式打开目标文件,它应该以写入模式打开。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-26
      • 1970-01-01
      • 2016-03-30
      • 1970-01-01
      • 2018-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多