Skip to content
Advertisement

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:

  • 1.txt:

Hello,

my class!

  • 2.txt:

Today is

a very nice

day

  • 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..

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

Advertisement

Answer

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);
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement