Hi I've been working on a basic encrypting program that will read a character of a file at a time, complement it using the '~' operator, and print it into the file. Here's the code:
Code:
/* EncodingTest.c

   A program that encodes the text file
   D:\A and A\Programming\EncodingTest.txt

   by complementing all its bytes.

   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 (by the user).

   21/7/2007		By Abda92

   NOTE: I've used the term "crypt" for both encryption and decryption.
*/
#include <stdio.h>

void crypt (FILE *file);

int main()
{
	char c;						/* character to be crypted */
	FILE *file;					/* pointer to the file */

	// Open the file
	file = fopen("D:\\A and A\\Programming\\EncodingTest.txt", "w+");
	if (fopen("D:\\A and A\\Programming\\EncodingTest.txt", "w+") != NULL)
	{
		printf("File opened successfully\n");
	}
	else
	{
		printf("Error openning file\n");
		return 0;					/* Exit program */
	}

	/* Check whether file is encrypted or decrypted and start crypt() */
	if ((c = getc(file)) == 'e')
	{
		printf("File is encrypted. would you like to decrypt\? (n = no, else yes)");
		if ((c = getchar()) == 'n') return 0;			/* Exit program */
		else
		{
			rewind(file);
			putc('d', file);
			crypt(file);
		}
	}
	else if (c == 'd')
	{
		printf("File is decrypted. would you like to encrypt\? (n = no, else yes)");
		if ((c = getchar()) == 'n') return 0;			/* Exit program */
		else
		{
			rewind(file);
			putc('d', file);
			crypt(file);
		}
	}
	else
	{
		printf("Error crypting file\n");
	}

	fclose(file);				/* close file */

	return 0;					/* Exit program */
}

void crypt (FILE *file)
{
	char c;

	c = getc(file);
	while (c != EOF)			/* there is still something to input */
	{
		char t;

		t = ~c;
		putc(t, file);
		c = getc(file);
	}

	printf("File cryption finished\n");
}
The problem is that the en/decrypting doesn't even start.
As soon as it gets to the 36th line, none of the If statements execute and the else statement executes which only contains an output to the screen telling the user what happened, then the program terminates.

I've tried forcing the en/decryption by removing the if statements. here's the new code:
Code:
/* EncodingTest.c

   A program that encodes the text file
   D:\A and A\Programming\EncodingTest.txt

   by complementing all its bytes.

   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 *file);

int main()
{
	char c;						/* character to be crypted */
	FILE *file;					/* pointer to the file */

	// Open the file
	file = fopen("D:\\A and A\\Programming\\EncodingTest.txt", "w+");
	if (fopen("D:\\A and A\\Programming\\EncodingTest.txt", "w+") != NULL)
	{
		printf("File opened successfully\n");
	}
	else
	{
		printf("Error openning file\n");
		return 0;					/* Exit program */
	}
	
	crypt(file);
	fclose(file);				/* close file */

	return 0;					/* Exit program */
}

void crypt (FILE *file)
{
	char c;

	c = getc(file);
	while (c != EOF)			/* there is still something to input */
	{
		char t;

		t = ~c;
		putc(t, file);
		c = getc(file);
	}

	printf("File cryption finished\n");
}
Now the problem is that the program is just erasing the contents of the file making it blank. I've also tried replacing all the getc() with fgetc() and putc() with fputc() but that didn't fix it. can someone please help me find my problem?