#include <Windows.h>
#include <cstdio>
const int KEY=111;
void encryptStrA(char* sometext)
{
int length;
length=strlen(sometext);
for(int i=0; i<length;i++)
sometext[i]^=KEY;
}
int main(void)
{
FILE* pFile=fopen("pliczek","wb");
char sign;
char sampleString[]="Hello world!";
encryptStrA(sampleString);
fprintf(pFile,"%c%c%s%c%c",13^KEY,10^KEY,sampleString,13^KEY,10^KEY);
fclose(pFile);
pFile=fopen("pliczek","rb");
while(!feof(pFile))
{
fscanf(pFile,"%c",&sign);
printf("%c",sign^KEY);
}
fclose(pFile);
system("PAUSE");
return 0;
}
I evaded some tricky things
- File is opened in binary mode
- In encryptStrA strlen function isn’t placed directly in the loop condition
In spite of these, it still has been outputting “Hell” instead of “Hello World!”? More precisely, cuts everything after spotting the key character .What’s the reason? I use OS in which every line of text is ended with carriage return(ASCII 13) and line feed (10).
The code
fprintf("%s", s);expects s to be a zero-terminated string. When you reach'o'^111it gives a null character, so the rest of the string is not written to the file.You can use
fwriteinstead.