The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
what is wrong with this tr -d? cleansing_flame UNIX for Dummies Questions & Answers 3 02-06-2008 09:34 AM
What’s wrong with the following? vrn UNIX for Dummies Questions & Answers 8 03-19-2006 06:09 PM
where have i gone wrong? Blip Shell Programming and Scripting 3 01-28-2004 01:43 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-07-2008
Registered User
 

Join Date: Jun 2008
Posts: 3
What am I doing wrong!!!

Hi all, I'm just getting back in to C programming after some time and I thought I would start off with a simple XOR encryption program. However I'm having trouble getting it to work. The code in prompt() function works great when it is just placed in side of main() however I want to be able to pass my arrays and file pointers to the prompt function to make the code more modular. Any help is much appreciated!

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char encrypt(char, char *);
int prompt(FILE **, FILE **, char *, char *, char *, char *); 

int main()
{
    FILE *inputFile, *outputFile;   // Self explantory
    char fileIn[256] = {'\0'}, fileOut[256] = {'\0'},
          password[30] = {'\0'}, password2[30] = {'\0'}; /* Input file name,  output file name and password and password2 */                                                                          
    int holding = 1, fcharin = 1, retStat;   /* Where we keep the byte from the file thats been
                                       encrypted and the byte read from the file */
                                     
    printf("XORencrypt by David James\nCreated: 29/05/08\n\n");

    retStat = prompt(&inputFile, &outputFile, fileIn, fileOut, password, password2);
    
         /*   Below is the encryption part.  Each byte from the file is read in to a (char) then XOR'd with element 0
            of the password array, then element 1 then element 2 etc until NULL is reached in the password array.  Then
            the now encrypted (char) byte is written to the output file.
         */ 

         while ( (fcharin = fgetc(inputFile)) != EOF ) {
            holding = encrypt(fcharin, password);
            fputc(holding, outputFile);
         }
         
    printf("\nEncryption/Decryption complete!\n");
               
    return 0;
}


/* The XOR encryption function */

char encrypt(char fcharin, char *password)
{
    int i = 0, holding = 0;
    
    for ( i = 0; password[i] != '\n'; i++ ) {
        holding = fcharin ^ password[i];
    }
    
    return holding; 
}

/* If the user gives no command line arguments */

int prompt(FILE **inputFile, FILE **outputFile, char *fileIn, char *fileOut, char *password, char *password2)
{
    printf("File to encrypt/decrypt?: ");
    scanf("%255s", fileIn);

         if ( (*inputFile = fopen(fileIn, "rb")) == NULL ) { 
            printf("Error opening file.\n");
            exit(1);
         }
    
         printf("Save the encrypted/decrypted file as?: ");
         scanf("%255s", fileOut);
      
        do {
            printf("Enter a password (less than 30 characters [no spaces]): ");
            scanf("%29s", password);
            printf("Again: ");
            scanf("%29s", password2);
        } while ( strcmp(password, password2) );
       
        if ( (*outputFile = fopen(fileOut, "wb")) == NULL ) { 
           printf("Error writing file %s or it could not be opened.\n", fileOut);
           exit(1);
        }

        return 0;
}
Reply With Quote
Forum Sponsor
  #2  
Old 06-08-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/fl
Posts: 1,059
I have simplified your example to just print out the file so that the required change is more obvious:
Code:
#include <stdio.h>
#include <stdlib.h>

/* prototype */
int prompt(FILE **);

int
main(int argc, char *argv)
{
    int fcharin;
    FILE *in;

    prompt(&in);

    while ((fcharin = fgetc(in)) != EOF )
      fprintf(stdout, "%d\n", fcharin);

    return 0;
}


int
prompt(FILE **inp)
{
   char fileIn[256];

   printf("File to encrypt/decrypt?: ");
   scanf("%255s", fileIn);

   if ((*inp = fopen(fileIn, "rb")) == NULL ) {          
       printf("Error opening input file.\n");
       exit(1);
   }
}
Reply With Quote
  #3  
Old 06-08-2008
Registered User
 

Join Date: Jun 2008
Posts: 3
Thanks

Thanks for simplifying it. Next time I post code I will too. I can't see what I'm doing wrong its driving me mad! I did have lots of books on C untill my house got robbed... so I'm at the mercy of fellow C coders for help

Dave
Reply With Quote
  #4  
Old 06-09-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 567
This is the case of the dangling pointer problem. You declare and initialize the variables in the prompt() function but when it returns to main() all of those disappear leaving the pointer variable in a state of limbo. The fact that your code works when it is all in main() should tell you that there is a disconnect in the way the functions exchange data.
Reply With Quote
  #5  
Old 06-09-2008
Registered User
 

Join Date: Jun 2008
Posts: 3
ok

Yeah I guessed there was some kind of dissconnect between the funtions. So how do I fix it? The variables where declared in function main btw. And I passed them by address so there values where changed in main as they where processed in function prompt(); however, thay are not the variables I am having trouble with. Its the FILE ** that are messing up. Anyhelp please?!

Dave
Reply With Quote
  #6  
Old 06-09-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 567
Show errors

What machine are you running this program on and can you show the errors you are getting.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:26 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0