繁体   English   中英

在C中以相反的顺序将字节从一个文件复制到另一个文件

[英]copy bytes from one file to another in reverse order in C

这里检查答案并在我的环境中运行它后,我发现代码仍然遇到与我的代码相同的问题。 这个问题是,每当我有一个与此类似的输入文件...

 FILE A                 
|---------------------| 
| ABCDE               | 
| abcde               | 
|---------------------| 

我在目标文件中生成了额外的换行符空白。

 FILE B                 
|---------------------| 
| edcba               | 
|                     | 
| EDCBA               | 
|---------------------| 

调试代码后,我可以看到两次将\\ n复制到目标文件中,我想了解为什么lseek会这样做。

附件是我的代码。 有问题的关键部分在do / while循环中。 非常确定我的思考过程是正确的,因为我从此答案中查找的代码将输出完全相同的结果。

#define CHAR_SIZE 2048 
#define BUFF_SIZE 1    
#define PERMS 0666   


int main(int argc, char* argv[]){                                          
    if (argc < 3) {                                                        
        return 1;                                                          
        printf ("ERROR: not enough arguments\n");                          
    }                                                                      
    int src_file = 0; int dest_file = 0;                                   
    int n = -1;                                                            
    if((src_file=open(argv[1],O_RDONLY)) < -1) return 1;                   
    if((dest_file = creat(argv[2], PERMS)) < -1) return 1;                 
    printf ("The filesize is: %d\n", lseek(src_file, (off_t) 0, SEEK_END));
    char buffer[BUFF_SIZE];                                                
    lseek (src_file,n--,SEEK_END);                                         
    do {                                                                   
        read(src_file,buffer,BUFF_SIZE);                                   
        printf ("%s", buffer);                                             
        write(dest_file,buffer,BUFF_SIZE);                                 
    }while (lseek (src_file,n--,SEEK_END) > -1);                           
    printf("\n");                                                          
    return 0;                                                              

}                       

您有许多问题。 你额外的换行符来自该POSIX的线端定义为一个事实newline (虽然不是所有的文件符合)。 因此,要摆脱多余的换行符,您需要:

int n = -2;

代替

int n = -1;

(明确读取POSIX行尾并将其写入输出文件)

您的其他问题是您将不知道文件打开是否失败,因为您不正确地检查了返回值:

// if ((src_file = open (argv[1],O_RDONLY)) < -1) return 1;
// if ((dest_file = creat (argv[2], PERMS)) < -1) return 1;

失败时返回-1 因此,您需要:

if ((src_file = open (argv[1],O_RDONLY)) == -1) return 1;
if ((dest_file = creat (argv[2], PERMS)) == -1) return 1;

接下来,您的缓冲区将不包含字符串。 至少需要一个2-char数组来保存1-char以及nul-terminating字符。 以下内容不起作用:

#define BUFF_SIZE 1
...
char buffer[BUFF_SIZE];
...
    printf ("%s", buffer);

您已将一个非终止字符发送给printf 如果您在启用警告的情况下进行编译(例如-Wall -Wextra ),您会知道,因为编译器会警告您(即使没有适当的警告标志,也可能会警告您,具体取决于您的编译器)。 至少要完成上述工作,您需要:

#define BUFF_SIZE 2

并且必须在调用printf之前确保您的nul-terminate buffer 您可以使用以下方法完成此操作:

char buffer[BUFF_SIZE] = "";
...
    read (src_file, buffer, BUFF_SIZE - 1);
    buffer[1] = 0;
    printf ("%s", buffer);
    write (dest_file, buffer, BUFF_SIZE - 1);

(您应该检查readwrite的返回值,以验证您正在读取/写入您认为是的字节数...)

将更正放在一起(并显示您可以完全消除缓冲区,而只需将int用作buf char)即可执行以下操作:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define CHAR_SIZE 2048
#define BUFF_SIZE 2
#define PERMS 0666

int main(int argc, char **argv) {

    if (argc < 3) {
        return 1;
        printf ("ERROR: not enough arguments\n");
    }

    int src_file = 0;
    int dest_file = 0;
    int n = -2;
    int nl = '\n';  /* explicit newline when writing in reverse */

    // if ((src_file = open (argv[1],O_RDONLY)) < -1) return 1;
    // if ((dest_file = creat (argv[2], PERMS)) < -1) return 1;
    if ((src_file = open (argv[1],O_RDONLY)) == -1) return 1;
    if ((dest_file = creat (argv[2], PERMS)) == -1) return 1;

    printf ("The filesize is: %ld\n", lseek(src_file, (off_t) 0, SEEK_END));

    lseek (src_file, n--, SEEK_END);
#ifdef WBUFCHAR
    int bufchar;
    do {    /* validate both read and write */
        if (read (src_file, &bufchar, BUFF_SIZE - 1) == 0) {
            fprintf (stderr, "error: read failure.\n");
            return 1;
        }
        putchar (bufchar);
        if (write (dest_file, &bufchar, BUFF_SIZE - 1) == 0) {
            fprintf (stderr, "error: write failure.\n");
            return 1;
        }
    } while (lseek (src_file, n--, SEEK_END) > -1);
#else
    char buffer[BUFF_SIZE];
    do {    /* validate both read and write */
        if (read (src_file, buffer, BUFF_SIZE - 1) == 0) {
            fprintf (stderr, "error: read failure.\n");
            return 1;
        }
        buffer[1] = 0;          /* nul-terminate */
        printf ("%s", buffer);
        if (write (dest_file, buffer, BUFF_SIZE - 1) == 0) {
            fprintf (stderr, "error: write failure.\n");
            return 1;
        }
    } while (lseek (src_file, n--, SEEK_END) > -1);
#endif
    /* explicity write the newline you removed earlier */
    if (write (dest_file, &nl, BUFF_SIZE - 1) == 0) {
        fprintf (stderr, "error: write failure.\n");
        return 1;
    }
    putchar ('\n');
    return 0;
}

注意:可以正常编译以使用您的buffer -DWBUFCHAR编译以使用bufchar代码。

编译和输出示例

$ cat testabcde.txt
ABCDE
abcde

$ gcc -Wall -Wextra -Ofast -o bin/extranl extranl.c

$ ./bin/extranl testabcde.txt testrev.txt
The filesize is: 12
edcba
EDCBA

$ cat testrev.txt
edcba
EDCBA

$ gcc -Wall -Wextra -Ofast -DWBUFCHAR -o bin/extranlwbc extranl.c

$ ./bin/extranlwbc testabcde.txt testrevwbc.txt
The filesize is: 12
edcba
EDCBA

$ cat testrevwbc.txt
edcba
EDCBA

试试看,如果您还有其他问题,请告诉我。

#include <unistd.h>                                                         
#include <fcntl.h>                                                          
#include <sys/types.h>                                                      

#define CHAR_SIZE 2048                                                      
#define BUFF_SIZE 1                                                         
#define PERMS 0666                                                          

int main(int argc, char* argv[]){                                           
    if (argc < 3) {                                                         
        printf ("ERROR: not enough arguments\n");                           
        return 1;                                                           
    }                                                                       
    int src_file = 0; int dest_file = 0;                                    
    int n = -1;                                                             
    if((src_file=open(argv[1],O_RDONLY)) == -1) return 1;                   
    if((dest_file = creat(argv[2], PERMS)) == -1) return 1;                 
    printf ("The filesize is: %d\n", lseek(src_file, (off_t) 0, SEEK_END)); 
    char buffer[BUFF_SIZE];                                                 
    lseek (src_file,n--,SEEK_END);                                          
    do {                                                                    
        read(src_file,buffer,BUFF_SIZE);                                    
        printf("%02x\n", *buffer);                                          
        if (*buffer == '\n')                                                
            n--;                                                            
        write(dest_file,buffer,BUFF_SIZE);                                  
    }while (lseek (src_file,n--,SEEK_END) > -1);                            
    printf("\n");                                                           
    return 0;                                                               

}                                                                           

该解决方案是修复输出文件中多余的换行符(\\ n \\ n)的最佳方法。

通过将这部分添加到do / while循环中,我可以补偿添加的\\ n。

if (*buffer == '\n')                                                
                n--;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM