将key
值写入文件会导致程序崩溃。我知道这一点是因为我用 key
值替换了普通的 String
并且程序运行良好。为什么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);
关于C - 将 kbhit 中的 key 写入文件会使程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54316493/