简体   繁体   English

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

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

So the assignment is merging two text files in Linux using system calls: 因此,任务是使用系统调用在Linux中合​​并两个文本文件:

  • 1.txt: 1.txt:

Hello, 你好,

my class! 我的课!

  • 2.txt: 2.txt:

Today is 今天是

a very nice 一个非常好的

day

  • NEW.txt: NEW.txt:

Today is 今天是

Hello, 你好,

a very nice 一个非常好的

my class! 我的课!

day

Problem is I get (sorry for putting this as code sample): 问题是我得到了(很抱歉将此作为代码示例):

Hello,
+Today is
my class!


a very nice

day

The + sign keeps changing between ("#", "(", "0", "_", ..) with each execution.. 每次执行时,+号都会在(“#”,“(”,“ 0”,“ _”,..)之间切换。

Why do I have extra new lines and this thing with the *? 为什么我还有多余的新行,而带有*的东西呢? Thank you in advance. 先感谢您。

My code: 我的代码:

#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);
}

you have to retain if you have read all your file or not, because the read in the first while will ... read, and that's not what you want. 如果您已阅读所有文件,则必须保留,因为在第一时间读取的内容将...已阅读,而这不是您想要的。

Code edited after comment : 注释后编辑代码:

#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);
}

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

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