// program7_4.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

// program 7.3

#include	"stdio.h"
#include	"stdlib.h"

#define		FALSE	0
#define 	TRUE		!FALSE

char		buffer[10];
FILE		*fp;
int		i;
int		icode;
char		code;

void
main(	int	argc,
	char	*argv[])
	{
	if (argc < 3)
		{
		printf("format: code bill.dat 100 \n");
		printf("Where bill.dat is the file to code and 100 is the code (1 to 126) \n");
		return;
        		}

	code = (char)atoi(argv[2]);

	if ( (code > 126) || (code < 1))
		{
        		printf("code is out of range\n");
		printf("format: code bill.dat 100 \n");
		printf("Where bill.dat is the file to code and 100 is the code (1 to 126) \n");
        		return;
		}

	fp = fopen(argv[1],"r+");
	if (fp == NULL)
        		printf("Error reading %s \n",argv[1]);
	else
		{
		fseek(fp,0,SEEK_SET);
		fread(buffer,sizeof(buffer),1,fp);

		for (i = 0; i < sizeof(buffer) - 1; i++)
			buffer[i] = buffer[i] ^ code;

		fseek(fp,0,SEEK_SET);
		fwrite(buffer,sizeof(buffer),1,fp);
		printf("%s was modefied!\n",argv[1]);
		}

    	fclose(fp);
	}

