我正在尝试编写一个能够用另一个替换特定字符的函数。 但是..输出有点奇怪..我不明白。 我可以编译的代码没有错误:
#include <stdio.h>
#include <windows.h>
int main()
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
WORD saved_attributes;
char file_name[256];
FILE* fp;
int ch, i=0, l=0;
unsigned char buff[2048];
/* Save current attributes */
GetConsoleScreenBufferInfo(hConsole, &consoleInfo);
saved_attributes = consoleInfo.wAttributes;
printf("Enter File Name: ");
scanf("%s", &file_name);
fp = fopen(file_name, "r+a");
printf("\n Printing file character by character..\n");
for ( ; ; )
{
l++;
ch = fgetc(fp);
SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE);
printf("%c", ch); buff[l] = ch;
SetConsoleTextAttribute(hConsole, saved_attributes);
if(ch == EOF) break;
}
printf("\n\n Converting..\n");
for( ; i<ftell(fp); i++)
{
if(buff[i] == '1') buff[i] = '8';
if(buff[i] == '2') buff[i] = '9';
if(buff[i] == '3') buff[i] = '0';
if(buff[i] == '4') buff[i] = '-';
if(buff[i] == '5') buff[i] = '=';
if(buff[i] == '6') buff[i] = 'q';
if(buff[i] == '7') buff[i] = 'w';
if(buff[i] == '8') buff[i] = 'e';
if(buff[i] == '9') buff[i] = 'r';
if(buff[i] == '0') buff[i] = 't';
if(buff[i] == 'q') buff[i] = 'y';
if(buff[i] == 'w') buff[i] = 'u';
if(buff[i] == 'e') buff[i] = 'i';
if(buff[i] == 'r') buff[i] = 'o';
if(buff[i] == 't') buff[i] = 'p';
if(buff[i] == 'y') buff[i] = '[';
if(buff[i] == 'u') buff[i] = ']';
if(buff[i] == 'i') buff[i] = 'a';
if(buff[i] == 'o') buff[i] = 's';
if(buff[i] == 'p') buff[i] = 'd';
if(buff[i] == 'a') buff[i] = 'f';
if(buff[i] == 's') buff[i] = 'g';
if(buff[i] == 'd') buff[i] = 'h';
if(buff[i] == 'f') buff[i] = 'j';
if(buff[i] == 'g') buff[i] = 'k';
if(buff[i] == 'h') buff[i] = 'l';
if(buff[i] == 'j') buff[i] = ';';
if(buff[i] == 'k') buff[i] = '\'';
if(buff[i] == 'l') buff[i] = '\\';
if(buff[i] == 'z') buff[i] = 'z';
if(buff[i] == 'x') buff[i] = 'z';
if(buff[i] == 'c') buff[i] = 'x';
if(buff[i] == 'v') buff[i] = 'c';
if(buff[i] == 'b') buff[i] = 'v';
if(buff[i] == 'n') buff[i] = 'b';
if(buff[i] == 'm') buff[i] = 'n';
}
SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE);
printf("%s\n", buff);
SetConsoleTextAttribute(hConsole, saved_attributes);
printf("\n Saving..");
fclose(fp);
{
FILE* fp = fopen(file_name, "w");
fputs(buff, fp);
}
}
我正在使用以下文件对其进行测试:
f l k [j6] 0 e r k l x [k0] r u I o k v C [x8]
w t y u C v n C Q r y I z x [c9] e y x [x0]
r [zu] l k [l6] 0 e r j k [lq] t i z [kw] y [hi] [j6] 0 e r t
但我得到的结果是:
(; \ ' [;[] \ ; ' ' \ z ['\] ' ] I ' ' c C [z;]
] \ [ ] C c b C Q ' [ I z z [x'] ; [ z [z\]
' [z]] \ ' [\[] \ ; ' ; ' [\[] \ ; z [']] [ [\;] [;[] \ ; ' \я
这里发生了什么?
答案 0 :(得分:3)
您的角色在同一周期内被多次替换。考虑buff[i] == '1'
时的情况。让我们浏览循环中的语句,看看会发生什么。
if(buff[i] == '1') buff[i] = '8'; // Now, buff[i] is '8'
if(buff[i] == '2') buff[i] = '9'; // Nothing happens
if(buff[i] == '3') buff[i] = '0'; // Nothing happens
if(buff[i] == '4') buff[i] = '-'; // wait for it...
if(buff[i] == '5') buff[i] = '=';
if(buff[i] == '6') buff[i] = 'q';
if(buff[i] == '7') buff[i] = 'w';
if(buff[i] == '8') buff[i] = 'e'; // Now, buff[i] is 'e'
// ...
if(buff[i] == 'e') buff[i] = 'i'; // Now, buff[i] is 'i'
// ...
if(buff[i] == 'i') buff[i] = 'a'; // Now, buff[i] is 'a'
// ... and so on
请注意,这一切都发生在循环的一个循环内。所以'1'被编码为';'而不是'8',这可能是你想要的。
您可能希望将所有if
(除了第一个)加上else
作为前缀。