Encrypting a txt file

This is a discussion on Encrypting a txt file within the C Programming forums, part of the General Programming Boards category; I'm trying to figure out how to write out this code. These are the 3 steps... 1. Parent reads from ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    31

    Encrypting a txt file

    I'm trying to figure out how to write out this code. These are the 3 steps...

    1. Parent reads from a file “input.txt” and writes into a pipe A that connects to
    child process #1.
    2. Child process 1 reads from pipe A and add a value v, that is provided by the user as a command line argument, to each character read from the pipe. Child 1 then writes
    the new character values into a second pipe B, which allows communication with child
    process #2.
    3. Child process 2 reads character by character from pipe B and writes the values
    to a new file “output.txt”.

    I think I have #1 done but I'm not sure if I can use the read command this way. I was wondering if it would actually be easier using fopen or fgets? I'm also wondering if I actually have to write out the encryption or this is an encrption command in some header.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main(void)
    {
    
    	int fd[2];
    	char buf[5];
    	ssize_t nbytes;
    	int status;
    	
    
    	status = pipe(fd);
    	if (status == -1) {
    		perror("fork");
    		exit(1); }
    	
    	switch (fork()) {
    	case -1: //Handle error
    		break;
    
    	case 0:  //Child reads from pipe
    		close(fd[1]);				//Write end is unused
    		nbytes = read(fd[0], buf, 100);    // Get data from pipe
    		//close(fd[0]);
    		exit(EXIT_SUCCESS);
    
    	case 1:  // Another child
    	
    	default: //Parent-readms/writes to pipe
    		  f = fopen("input.txt", "rt");
    		  nbytes = read(fd[0], buf, 5);
       		  write(fd[1], s, 5);

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    I update my code. I need help with the character array. I'm not sure if I doing it right but im trying to get the program to ask the user to add a value to the character that is being displayed.

    Code:
    #include <unistd.h>
    #include <sys/types.h>
    
    
    int main()
    {
    	int fd[2]; 
    	int nbytes;
    	pid_t childpid;
    	char garbage[30];
    	int m;	
    
    	pipe(fd);
    	
    		if((childpid = fork()) == -1)
    		{
    		perror("fork");
    		exit(1);
    		}
    		  else	if(childpid == 0)
    		{
    			//Child process 1
    			dup2(fd[0], 0);
    			read(fd[0], garbage, sizeof garbage);
    			while (printf("Character: %d", garbage[]), fgets(garbage, 30, stdin), !feof(stdin)){
    			if
    			}	
    		else if(childpid > 0) 
    		{	
    			//Parent
    			read(fd[#], "input.txt", "rb");
    			write(fd[#], garbage, sizeof garbage);
    			childpid = fork();
    			if(childpid == -1){
    			perror("fork");
    			exit(1);
    		}	
    		 else if (childpid == 0)
    		{
    			//Child process 2
    			close(fd[0]);
    			dup2(fd[1], 1);
    	
    		}
    
    return 0;

  3. #3
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    did you compile this code without errors?
    I already spot some syntactic errors. Fix 'em run the code and then repost if you're still having problems.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  3. Encrypting text file with ASCII hex
    By supaben34 in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2005, 05:35 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21