简体   繁体   中英

Access violation reading location fprintf

So here is what the teacher asked us to do:

Create a file that store what the user is typing. The trouble im having is that and exception is thrown and no error is given to me. The first problem is the characters inside the text file are by default in CAPS (without the caps verification). The second The error happens as soon as i press a key (if theres a cap verification).

#include <direct.h>
#include <fstream>
#include <stdlib.h>


using namespace std;

//function that write to the file
int Save(int _key, char *file, bool caps);
int Backspace();
bool caps = false;

int main() {
    char i;

    while (true) {
        for (i = 8; i <= 255; i++) {
            // Checks if a key is currently up or down
            if (GetAsyncKeyState(i) == -32767) {
                //Get caps lock state
                if ((GetKeyState(VK_CAPITAL) & 0x0001) != 0) {
                    //Caps lock is on
                    caps = true;
                    Save(i, "C:/users/myComputer/documents/school/c++/input.txt", caps);
                }
                else {
                    //Caps lock is off
                    caps = false;
                    Save(i, "C:/users/myComputer/documents/school/c++/input.txt", caps);
                }
            }
        }
    }
    return 0;
}

int Save(int _key, char *file, bool caps) {
    Sleep(10);
    FILE *OUTPUT_FILE;
    OUTPUT_FILE = fopen(file, "a+");

 switch (_key) {

     case VK_RETURN:
         OUTPUT_FILE = fopen(file, "a+");
         fprintf(OUTPUT_FILE, "%s", "\n");
         break;

     case VK_LBUTTON || VK_RBUTTON:
         OUTPUT_FILE = fopen(file, "a+");
         fprintf(OUTPUT_FILE, "%s", "");
         break;

     default:
         if (caps == true) {
             char i = tolower(putchar(_key));
             OUTPUT_FILE = fopen(file, "a+");
             fprintf(OUTPUT_FILE, "%s", i);
         }
         else {
             char i = toupper(putchar(_key));
             OUTPUT_FILE = fopen(file, "a+");
             fprintf(OUTPUT_FILE, "%s", i);
         }
 }
 fclose(OUTPUT_FILE);

 return 0;
}

Segmentation fault

There are several problems with this code. But the main issue, causing the segmentation error is

        fprintf(OUTPUT_FILE, "%s", i);   // Ouch !!!

"%s" tells fprintf() that it will find as argument a pointer to an array of characters. Cross-check your format string here . Unfortunately, you provide an integer between 8 and 255. This causes undefined behavior.

To correct the error change it to:

        fprintf(OUTPUT_FILE, "%c", i);

Other issues

The fopen() call inside the switch is redundant: the file was already opened before. It's no use to open a file without having closed it before.

The function documentation tells about least and most significant bit, but nothing about the bits in-between. So the check should be:

 if (GetAsyncKeyState(i) & 0x8000 )   // but not strict equality

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