All right I'm having one problem writing this program. I compile the code, got many errors and warnings, fixed them all up, and compiled it again.
The compiler gave no errors while compiling. However, when i execute the program it gives me a weird error:
Code:
6 [main] a 1748 _cygtls::handle_exceptions: Error while dumping state (probably corrupted stack)
Segmentation fault (core dumped)
Here is my new source code:
Code:
/* EncodingTest.c
A program that encodes the text file
D:\A and A\Programming\EncodingTest.txt
by complementing all its bytes.
The new file is saved in the same directory as "EncodedTest.txt".
It can also decrypt the file. the first character in the file
informs the program if it is already encrypted or not. 'e' means
it's encrypted, and 'd' means it's ready to be used.
21/7/2007 By Abda92
*/
#include <stdio.h>
void crypt (FILE *source, FILE *target);
char askUser (void);
int readFile (char determin, FILE *rfile);
int writeFile (char determin, FILE *wfile);
int main()
{
int i; /* Temperary purposes */
char determin; /* to hold whether user wants to encrypt or decrypt (e, d) */
FILE *rFile; /* pointer to the file being read */
FILE *wFile; /* pointer to the file being written to */
determin = askUser();
/* Open the file to be read */
i = readFile(determin, rFile);
if (i == '\0') return 0;
/* Open file to be written to */
i = writeFile(determin, wFile);
if (i == '\0') return 0;
/* start En/Decrypting */
crypt(rFile, wFile);
fclose(rFile); /* close file */
fclose(wFile); /* close file */
return 0; /* Exit program */
}
void crypt (FILE *source, FILE *target)
{
char c;
c = fgetc(source);
while (c != EOF) /* there is still something to input */
{
fputc((~c), target);
c = fgetc(source);
}
printf("File cryption finished\n");
}
/* Ask user whether he would like to encrypt or decrypt */
char askUser(void)
{
char c;
while(1)
{
printf("What would you like to do\? (e = encrypt, d = decrypt)\n");
if ((c = getchar()) == 'e') return 'e';
else if (c == 'd') return 'd';
else printf("Invalid operation\n");
}
}
/* Open the file to be read. returns 1 if successful and NULL if unsuccsessful */
int readFile(char determin, FILE *rfile)
{
if (determin == 'e')
{
rfile = fopen("D:\\A and A\\Programming\\Decrypted.txt", "r");
if (rfile != NULL)
{
printf("\"Decrypted.txt\" opened successfully\n");
return 1;
}
else
{
printf("Error openning file\"Decrypted.txt\"\n");
return '\0';
}
}
else
{
rfile = fopen("D:\\A and A\\Programming\\Encrypted.txt", "r");
if (rfile != NULL)
{
printf("\"Encrypted.txt\" opened successfully\n");
return 1;
}
else
{
printf("Error openning file\"Encrypted.txt\"\n");
return '\0';
}
}
}
/* Open the file to be written to. returns 1 if successful and NULL if unsuccessful */
int writeFile(char determin, FILE *wfile)
{
if (determin == 'e')
{
wfile = fopen("D:\\A and A\\Programming\\Encrypted.txt", "w");
if (wfile != NULL)
{
printf("\"Encrypted.txt\" created successfully\n");
return 1;
}
else
{
printf("Error creating file \"Encrypted.txt\"\n");
return '\0';
}
}
else
{
wfile = fopen("D:\\A and A\\Programming\\Decrypted.txt", "w");
if (wfile != NULL)
{
printf("\"Decrypted.txt\" created successfully\n");
return 1;
}
else
{
printf("Error creating file \"Decrypted.txt\"\n");
return '\0';
}
}
}
I see no problems with this code. Can someone (or some people) please help me identify this problem? I will be really thankful.