繁体   English   中英

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

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

key写入文件会使程序崩溃。 我知道这一点是因为我用普通String替换了key并且程序运行良好。 为什么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