John
John

Reputation: 57

File reversing copy in c

I am trying to finish a program that can fork function a child process, and parent can get the input file (under same directory) , reverse the content of this file, then using pipe function pass to the child process. Child will read the message from pipe and generate an output file. I have finished fork, create pipe and reverse function. However I got stucked on write it to the pipe. I know there must some type confusion when i try to pass the parameter into the write function, Any hits would be appreciated.

Here is the Code I have so far:

#include <stdio.h>
#include <stdlib.h> //exit
#include <string.h>
#include <sys/types.h>  //pid_t

#define READ_END 0
#define WRITE_END 1

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

long loc;
FILE *in, *out;
char ch;

if (argc != 3)
{
    printf("Usage %s message\n", argv[0]);
    exit(EXIT_FAILURE);
}
int pipefd[2];
int pipe_return = pipe(pipefd);

if((in = fopen(argv[1], "rb")) == NULL) {
        printf("Cannot open input file.\n");
        exit(1);
}

if((out = fopen(argv[2], "wb"))==NULL) {
        printf("Cannot open output file.\n");
        exit(1);
}

if(pipe_return == -1)
{
    printf("Unable to create pipe\n");
    exit(EXIT_FAILURE);
}

pid_t return_from_fork = fork();

if (return_from_fork == -1)
{
    printf("Unable to fork\n");
    exit(EXIT_FAILURE);
}

else if (return_from_fork == 0) //this is a child
{
    char msg;
    close(pipefd[WRITE_END]);
    int read_return = read(pipefd[READ_END], &msg, 1);
    printf("read return:%d\n", read_return);
    while(read_return > 0){
        fputc(ch, out);
        printf("%c",msg);
        read_return = read(pipefd[READ_END], &msg, 1);
    }
    printf("child ends\n");
    close(pipefd[READ_END]);
    exit(EXIT_SUCCESS);
}

else if (return_from_fork > 0)
{
    close(pipefd[READ_END]);
    printf("this is parent\n");
    fseek(in, 0L, SEEK_END);
    loc = ftell(in);        
    while(loc >= 0L){
        fseek(in, loc, SEEK_SET);
        ch = fgetc(in);
        printf("%c",ch);
        int write_r = write(pipefd[WRITE_END], ch, 1);//here is the problem the printf() return -1
        printf("%d",write_r);
        loc--;
    }
    printf("\n");   
    close(pipefd[WRITE_END]);
    wait(NULL);
    printf("file successful generated.\n");
    fcloseall();
    exit(EXIT_SUCCESS);
}

}

And Here is the compile result:

zzz@ubuntu:~/Desktop/test$ gcc filereversecopy.c -o run
zzz@ubuntu:~/Desktop/test$ ./run src.txt out.txt
this is parent
�-1
-1e-1c-1n-1e-1t-1n-1e-1s-1 -1a-1 -1s-1i-1 -1s-1i-1h-1T-1
read return:0
child ends
file successful generated.
zzz@ubuntu:~/Desktop/test$ 

Upvotes: 0

Views: 144

Answers (1)

Michael
Michael

Reputation: 3729

On the line you say is problem you are passing ch to write, and ch is type char. I'm sure you mean &ch instead. I bet if you change that write will return 1 instead of -1.

Also, you seek to the end to start reading, but when you seek to the end you are pointing at EOF. You need to start reading at the position before EOF. So after "fseek(in, 0L, SEEK_END); loc = ftell(in);" adding "loc--; fseek(in, loc, SEEK_SET);" makes it work.

Upvotes: 1

Related Questions