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 12:34 PM
What’s wrong with the following? vrn UNIX for Dummies Questions & Answers 8 03-19-2006 09:09 PM
where have i gone wrong? Blip Shell Programming and Scripting 3 01-28-2004 04:43 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-07-2008
WelshDave WelshDave is offline
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;
}

  #2 (permalink)  
Old 06-08-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,945
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);
   }
}

  #3 (permalink)  
Old 06-08-2008
WelshDave WelshDave is offline
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
  #4 (permalink)  
Old 06-09-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 753
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.
  #5 (permalink)  
Old 06-09-2008
WelshDave WelshDave is offline
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
  #6 (permalink)  
Old 06-09-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

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

What machine are you running this program on and can you show the errors you are getting.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:01 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0