Hi, How would I go about using the Caesar Cipher technique on a .txt file? E.g. shifting each letter by 3?
Here is my code?
#include %26lt;stdio.h%26gt;
#include %26lt;conio.h%26gt;
#define CNTRLZ 0x1A
char sourceFileName[] = "H:\\text.txt";
char commStreamName[] = "COM1";
FILE *commStream;
FILE *SouceFile;
char charToSend;
char ReadAccess[] = "r";
char WriteAccess[] = "w";
char errorMsg[] = "fail to open data file\n";
char fileopenflag[] = "r";
char EndOfFile[] = "EOF";
int main(void) {
FILE *fp;
FILE *dp;
int c;
if ((dp = fopen("COM1", "r")) == NULL) {
printf("fail to open COM port\n"); return 1;
}
if ((fp = fopen( sourceFileName, "w")) == NULL) {
printf("fail to open data file\n"); return 1;
}
while ((c = fgetc(fp )) != EOF) {
fputc( c, dp);
putch(c);
}
fputc('*', dp);
fclose(dp);
fclose(fp);
}
Any ideas where to start?
C/C++ how would I use the Caesar Cipher on a .txt file? Shift each letter by 3?
Shifting by 3 is the same as multiplying (shift left) or dividing (shift right) by 8. The Caesar code, if wikipedia is to be believed, is just to add a constant (like 3) to each character before you write it and subtract the constant after you read it.
If you simply shift by 3, you could lose some characters that would no longer fit in your character data type (depending on localization). There's less risk if you just add 3. If you rotate instead of shift, there's no risk of it at all, but that might be beyond the scope of what you could call a Caesar cipher.
Edit:
I just had a "Duh" moment, when I realized that by "shift" you probably meant "add". Shift has a specific meaning in computer programming; it threw me off.
Anyway, yes, The program that writes it adds 3 to the character before it sends it, but only if the character is less than "x". If the character is "x", "y", or "z", then subtract 23 from it (23 is the number of letters between "a" and "x").
The program that reads subtracts 3 from each character, but only if the character is greater than "c". If the character is "a", "b", or "c", then add 23 to it.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment