![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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;
}
|
|
||||
|
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 |
|
||||
|
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.
|
|
||||
|
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 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|