简体   繁体   English

C - 将密钥从 kbhit 写入文件会导致程序崩溃

[英]C - Writing key from kbhit to a file crashes the program

Writing the key value to the file is making the program crash.key写入文件会使程序崩溃。 I know this because I substituted the key value for a normal String and the program worked fine.我知道这一点是因为我用普通String替换了key并且程序运行良好。 Why is the key value making the program crash?为什么key导致程序崩溃?

#include <stdio.h>
#include <conio.h>

int main() {

  FILE *fp;
  fp = fopen("C:\\Users\\Francisco\\Documents\\C programs\\log.txt", "w");


  int key;

  while(1) {

    if(_kbhit()) {

     key = _getch();

      fprintf(fp, (const char*) key);

    }

  }

  fclose(fp);

  return 0;

}
  fprintf(fp, (const char*) key);

应该

  fprintf(fp,"%c ",key);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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