code.club

 找回密碼
 立即註冊
搜索
查看: 7820|回復: 1

有關 getchar() getche() getch()的異同

[複製鏈接]
發表於 2015-1-16 20:57:59 | 顯示全部樓層 |閱讀模式
本帖最後由 enter 於 2015-1-18 22:19 編輯

#include <stdio.h>

int main(void)
{
char x,y,z;

printf("enter a letter:\n");
x=getchar(); //隨打隨時顯示在螢幕上,就算打一串字元也顯示一串字元,但按enter之後依然只出現第一個字元。按enter的回傳值是'\n'。
printf("%c\n",x);
printf("enter a letter:\n");
y=getche(); //一打也是立即顯示在螢幕上,但無法連打,因為不用按enter它就立即賦值給y。
printf("%c\n",y);
printf("enter a letter:\n");
z=getch(); //輸入時不會顯示字元,但一輸入完後就已直接賦值給y,不用按enter。按enter的回傳值是'\r'。
printf("%c\n",z);
return 0;
}
回復

使用道具 舉報

 樓主| 發表於 2015-3-23 22:46:44 | 顯示全部樓層
將某一檔案的內容一行一行的顯示在螢幕上,並且問是否要繼續顯示下一行。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
FILE *fp;
char str[80],ch;

if((fp=fopen(argv[1],"r"))==NULL)
{
printf("cannot open the file.\n");
exit(1);
}

do
{
  fgets(str,70,fp);
  printf(str);
  printf("\nDo you want to read more?Y/N\n");
  ch=getch(); //這裡只能用getch() or getche(),一旦用getchar()則只會繼續執行一次,之後竟會自動結束程式。不知為何?
  if(ch!='y') break;
   }
   while(!feof(fp));
   
  fclose(fp);
  
  return 0;
  }
  
回復 支持 反對

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

小黑屋|手機版|Archiver|code.club  

GMT+8, 2024-3-28 17:08 , Processed in 0.086330 second(s), 19 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表