简体   繁体   中英

Why is that function not working?

I am trying to write a function able to replace a specific character with another. However.. the output is kinda weird.. that i don't understand. The code i present can be compiled without errors:

#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);
    }
}

I am testing it with the following file:

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

But the result i get is:

(; \ ' [;[] \ ; ' ' \ z ['\] ' ] I ' ' c C [z;]
] \ [ ] C c b C Q ' [ I z z [x'] ; [ z [z\]
' [z]] \ ' [\[] \ ; ' ; ' [\[] \ ; z [']] [ [\;] [;[] \ ; ' \я

What is going on here?

Your characters are getting replaced multiple times in the same cycle. Consider the case when buff[i] == '1' . Let's go through the statements in your loop and see what happens.

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

Note that this all happens within one cycle of the loop. So a '1' gets encoded as ';' instead of '8', which is probably what you wanted.

You will probably want to prefix all of those if s (besides the first) with an else .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM