Question:
Programming in Pure C?
forsaken_sorceress
2011-12-08 01:33:57 UTC
I am supposed to be writing a program that prompts the user to type in the name of a plain simple text file and then it reads that text file and displays to screen. I have that part working so far. Then I am supposed to prompt the user for an alphabetic offset key in order to encrypt the text file using ceaser cifer. Like if i type in 1, A becomes B and B becomes C and C becomes D etc. EG. the word "dog" would encrypt to become "eph". Like wise if the user typed in 2 then A becomes C and B becomes D etc. you get the idea. the alphabetic offset key is numbers between 1-25. Once the user has typed in the offset number, the code needs to display the encrypted text to screen then produce a new text file with the encrypted text. I Think i have done the code for the ceaser cifer correctly but I am having trouble getting the the first part of the code that displays the text file to scree and the second part of the code which is the ceaser cifer and generating new text file with the encrypted text to tie together!

Any help would be appreciated. This is what I have so far.




#include
#include
#include
#include
#include


#define ELEMENT 500 // defines the elements in an array to 500
#define LENGTH 100 // defines the length in an array to 100

void loadfile(); // defining the prototype

void loadfile() // defining the program

{
FILE *filepointer;
int wordnum, is_end, poswithin, shiftletters; //initialising the variables as integers
char read_char; // initialising a variable called read_char
char filename[50]; // defining the name of file and limiting to 50 characters
char str[ELEMENT][LENGTH],messege[999]; // initialising the parameters and limits of the string

printf("Please type in the name of the file you want to have encrypted \n"); // promting the user to input the filename
scanf("%s", filename); // assigning the text inputed to variable filename

filepointer=fopen(filename,"r"); //search for file name inputted by user
poswithin=0; wordnum=0;

is_end=fscanf(filepointer,"%c",&read_char); //reading the found text file

while (is_end!=EOF) // while the file is at end
{

if (isupper(read_char) || islower(read_char))
{
printf("%c", read_char); //print out the document to screen
str[wordnum][poswithin]=read_char; poswithin++;
}
else
{

str[wordnum][poswithin]=(char)'\n';
poswithin=0; wordnum++;

printf(" ");

}

scanf("%c", &messege);

is_end=fscanf(filepointer,"%c",&read_char);

printf("\n\n\nPlease type in the alphabetical offset key. (1-25)\n");
fflush(stdin);
scanf("%d", &shiftletters);

for(int i=0;i {
// checking for upper case
if((messege[i]>='A')&&
(messege[i]<='Z'))
messege[i]=((messege[i]-'A') + shiftletters) % 26 + 'A';
else
//checking for lower case
if((messege[i]>='a')&&
(messege[i]<='z'))
messege[i]=((messege[i]-'a') + shiftletters) % 26 + 'a';
}

printf("%d",messege);
}
fclose(filepointer);


}

void main()

{

loadfile();

}
Three answers:
roger
2011-12-08 09:46:14 UTC
you are making this entirely too complicated

easier to read the input file character by character



FILE *filepointer;

FILE *cipherfile;

char * filename[100];

int c;

int key;

int off;

printf("Please type in the name of the file you want to have encrypted \n"); // promting the user to input the filename

scanf("%s", filename); // assigning the text inputed to variable filename

printf("enter key ");

scanf("%d ",&key);



filepointer=fopen(filename,"r"); //… for file name inputted by user



while((c=fgetc(filepointer))!=EOF){

putchar(c); // print the char to stdout

}

rewind(filepointer); // rewind the input file



cipherfile=fopen("output","w");



while((c=fgetc(filepointer))!=EOF){ // read character by character til we get to EOF

if(isupper(c))

c='A'+(c-'A'+key)%26;

if(islower(c))

c='a'+(c-'a'+key)%26;

putchar(c); // print the encrypted char to stdout

fputc(c,cipherfile);// print the encripted char to output file

}
Sadsongs
2011-12-08 10:42:02 UTC
I'd do this by reading the file a line at a time (fread) and then processing each line (simple pointer arithmetic to advance char by char within the buffer).



You need two file pointers, one for input and one for output. Make sure you test each fopen, by the way, in case either is unsuccessful - always handle error situations,.



char buff[4096], *ptr;



while(!feof(in_ptr))

{

fread(buff, in_ptr);

ptr = buff;

while(*ptr != '\0')

{

/* test char value and increment it by one if it's in your ranges: *ptr++; */



ptr++; /* go to next char in buff */

}

fputs(out_ptr, buff);

/* and print the op line to screen here */

}

fclose(in_ptr);

fclose(out_ptr);





That's off the top of my head so I won't swear I've typed it correctly. You can make your char handling simpler by testing on ASCII values, by the way.
anonymous
2011-12-08 10:28:43 UTC
Which version of C defines main with a return type of void and has a header called conio.h?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...