C编程-将文件复制到另一个

标签 c file fopen fwrite fread

我实现了以下功能

int copy_files(char *source, char *destination)
{
    FILE *in_fd, *out_fd;

    char *buf;
    char buffer[512];

    time_t t;
    struct tm *td;

    long file_size;
    int result;
    int value;

    int status =  STATUS_ERROR;

    /* Open file for both reading and writing */
    in_fd = fopen(source, "w+");    //
    if(in_fd == NULL)
    {
       fputs ("File error",stderr);
       exit (1);
    }
    else
    {
        printf("writing in file...\n");

        strcpy(buffer, "Test writing in USB Key File \n");
        fprintf(in_fd, buffer); 
        printf("%s\n",buffer);
        t = time(NULL);
        td = gmtime((const time_t *)&t);
        sprintf(buffer, "Date %02d/%02d/%02d Time %02d:%02d:%02d\n", 
        td->tm_mday, td->tm_mon + 1, td->tm_year + 1900,
        td->tm_hour, td->tm_min, td->tm_sec);
        fprintf(in_fd, buffer);
        printf("%s\n",buffer);
        printf("Writing OK\n");
    }

    out_fd = fopen(destination,"w+");
    if(out_fd == NULL)
    {
        fputs ("File error",stderr);
        exit (1);
    }   
    else
    {   
        // obtain file size:
        fseek (in_fd , 0 , SEEK_END);
        file_size = ftell (in_fd);
        rewind (in_fd);

        // allocate memory to contain the whole file:
        buf = (char*) malloc (sizeof(char)*file_size);  

        while (!feof(in_fd)) 
        {
            /* Read  data */
            fread(buf, 1, (sizeof buf), in_fd);
            printf("%s", buf);
        }

        /* Write  data */
        fwrite(buf, 1, (sizeof buf), out_fd);
    } 
    fclose(in_fd);
    fclose(out_fd);
    return STATUS_OK;
}

通过这个函数,我想创建一个文件,用两行填充它并将整个包含复制到另一个文件。

但是我不明白为什么:

  1. 目标文件的创建与源文件相反。
  2. printf 显示:

    写入文件... 测试写入USB Key文件 日期 01/01/1970 时间 00:45:46 书写正常 Testh�v wrih�vtingh�v in h�vUSB h�vKey h�vFileh�v Dah��vte 0h��v1/01h��v/197h��v0 Tih��vme 0h��v0:45h��v:46 h��v:46

如何解决问题?

编辑:

关于第一个问题我不明白为什么会出现下面的代码:

strcat(temp_dest, direntp->d_name);
printf("after strcat temp_dest=%s\n", temp_dest);         
strcat(temp_src, direntp->d_name);
printf("after strcat temp_src=%s\n", temp_src); 
printf("destination:%s\n",temp_dest);
copy_files(temp_src,temp_dest);

返回:

after strcat temp_dest=/tmp/usb/test10
after strcat temp_src=/media/sda1/test10
destination:10

为什么是 10? :/

最佳答案

更正您的 fread()

因为您已经知道文件大小
无需循环,一次调用fread读取file_size字节就足够了。

else                                                                        
{                                                                           
    // obtain file size:                                                    
    fseek (in_fd , 0 , SEEK_END);                                           
    file_size = ftell (in_fd);                                              
    rewind (in_fd);                                                         

    // allocate memory to contain the whole file:                           
    buf = malloc (file_size);                                               

    /* Read file_size no of bytes */                                                        
    fread(buf, 1, file_size, in_fd);                                      
    buf[file_size -1] = 0;                                                  
    printf("File content after Reading :[%s]", buf);                        

    /* Write  data */                                                       
    fwrite(buf, 1, file_size, out_fd);                                      
}

代码中的其他更正。
fprintf 中缺少 %s(第二个参数)。

...
else                                                  
{                                                                           
    printf("writing in file...\n");                                         

    strcpy(buffer, "Test writing in USB Key File \n");                      
    fprintf(in_fd, "%s", buffer); 
    ...                                 

    fprintf(in_fd, "%s", buffer);                                      
    printf("Writing OK\n");     
}

关于C编程-将文件复制到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27542982/

相关文章:

java - 如何从 JsonObject 创建 InputStream 对象

C fopen/fwrite 让我发疯 - fwrite 不写

c - 段错误——无法将一个文件的内容写入另一个文件

PHP:fopen 创建文件夹

c - 嗨,我有一个 C 编程问题。我想知道两者之间的区别以及一个比另一个有用的地方?

c - OpenGL:从读取文件的 while 循环中调用 glutPostRedisplay()

java - 如何从java中的文件夹中获取单个文件?

c - 在C中读取一行未知格式

c - C 中的 for/while 循环内存清除

c - 不确定为什么 while 循环在使用 scanf 时工作不同