使用系统调用在 Linux 中使用 C 将两个文本文件合并为新文件(每一行来回)

Merging two text files into new one (back and forth every new line) using C in Linux using system-calls

因此作业是使用系统调用合并 Linux 中的两个文本文件:

Hello,

my class!

Today is

a very nice

day

Today is

Hello,

a very nice

my class!

day

问题是我得到了(很抱歉把它作为代码示例):

Hello,
+Today is
my class!


a very nice

day

每次执行时,+ 号都会在 ("#", "(", "0", "_", ..) 之间变化..

为什么我有额外的新行和带 * 的东西? 提前谢谢你。

我的代码:

#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>

int main(int argc, char* argv[])
{
int p1,p2,pNEW;
char Buff1[1], Buff2[1];
p1=open(argv[1],O_RDONLY);
p2=open(argv[2],O_RDONLY);
pNEW=open(argv[3],O_WRONLY|O_CREAT,0644);

while(read(p1,&Buff1,1)>0 || read(p2,&Buff2,1)>0)
{
    do{
        write(pNEW,&Buff1,1);
        if((Buff1[0]=='\n'))
            break;
    }while(read(p1,&Buff1,1)>0);

    do{
        write(pNEW,&Buff2,1);
        if((Buff2[0]=='\n'))
            break;
    }while(read(p2,&Buff2,1)>0);
}

close(p1);
close(p2);
close(pNEW);
}

无论是否读取了所有文件,都必须保留,因为第一个 while 中的读取将...读取,而这不是您想要的。

评论后编辑的代码:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>

bool WriteLineFromFile(int dst, int src, bool *srcTerminated)
{
    int     lastChar    = EOF;
    char    currentChar;
    ssize_t nbCharRead;
    ssize_t nbCharWrite;

    do {
        if ((nbCharRead = read(src, &currentChar, 1)) < 0) {
            fprintf(stderr, "%s : read(src, &buf, 1) : src=%d, errno='%s'.\n", __func__, src, strerror(errno));
            return (false);
        }
        // End of file
        if (nbCharRead == 0) {
            (*srcTerminated) = true;
            // Adding '\n' if necessary
            if (lastChar != '\n' && lastChar != EOF) {
                currentChar = '\n';
                while ((nbCharWrite = write(dst, &currentChar, 1)) != 1) {
                    if (nbCharWrite < 0) {
                        fprintf(stderr, "%s :  write(dst, &buf, 1) : dst=%d, errno='%s'.\n", __func__, dst, strerror(errno));
                        return (false);
                    }
                    sleep(1);
                }
            }
            return (true);
        }
        // Writing a char into the dst file
        while ((nbCharWrite = write(dst, &currentChar, 1)) != 1) {
            if (nbCharWrite < 0) {
                fprintf(stderr, "%s :  write(dst, &buf, 1) : dst=%d, errno='%s'.\n", __func__, dst, strerror(errno));
                return (false);
            }
            sleep(1);
        }
        lastChar = currentChar;
    } while (currentChar != '\n');

    return (true);
}

bool FileMerging(char *inputPathFile1, char *inputPathFile2, char *outputPathFile)
{
    int  inputFile1      = -1;
    bool file1Terminated = false;
    int  inputFile2      = -1;
    bool file2Terminated = false;
    int  outputFile      = -1;
    bool returnFunction  = false;

    // Openning all the file descriptor
    if ((inputFile1 = open(inputPathFile1, O_RDONLY)) == -1) {
        fprintf(stderr, "%s : open(inputPathFile1, O_RDONLY) : inputPathFile1='%s', errno='%s'.\n", __func__, inputPathFile1, strerror(errno));
        goto END_FUNCTION;
    }
    if ((inputFile2 = open(inputPathFile2, O_RDONLY)) == -1) {
        fprintf(stderr, "%s : open(inputPathFile2, O_RDONLY) : inputPathFile2='%s', errno='%s'.\n", __func__, inputPathFile2, strerror(errno));
        goto END_FUNCTION;
    }
    if ((outputFile = open(outputPathFile, O_WRONLY | O_CREAT, 0644)) == -1) {
        fprintf(stderr, "%s : open(outputPathFile, O_RDONLY) : outputPathFile='%s', errno='%s'.\n", __func__, outputPathFile, strerror(errno));
        goto END_FUNCTION;
    }

    // Alternativly print a line from inputFile1 and inputFile2 to outputFile
    do {
        if (!file1Terminated) {
            if (!WriteLineFromFile(outputFile, inputFile1, &file1Terminated)) {
                goto END_FUNCTION;
            }
        }
        if (!file2Terminated) {
            if (!WriteLineFromFile(outputFile, inputFile2, &file2Terminated)) {
                goto END_FUNCTION;
            }
        }
    } while (!file1Terminated || !file2Terminated);


    returnFunction = true;
    /* GOTO */END_FUNCTION:
    if (inputFile1 != -1) {
        close(inputFile1);
    }
    if (inputFile2 != -1) {
        close(inputFile2);
    }
    if (outputFile != -1) {
        close(outputFile);
    }
    return (returnFunction);
}

int main(int argc, char *argv[])
{
    if (argc != 4) {
        fprintf(stderr, "This program wait 3 arguments on the command-line : inputFilePath1 inputPathFile2 outputPathFile.\n");
        return (EXIT_FAILURE);
    }
    if (!FileMerging(argv[1], argv[2], argv[3])) {
        return (EXIT_FAILURE);
    }
    return (EXIT_SUCCESS);
}