用 C 清除控制檯的方法有哪些?
有幾種方法可以清除控制檯或輸出螢幕,clrscr() 函式就是其中一種。呼叫該函式後它會清除螢幕。它在 "conio.h" 標頭檔案中宣告。還有一些其他方法,比如 system("cls") 和 system("clear"), 這些方法在 "stdlib.h" 標頭檔案中宣告。
以下是用 C 語言清除控制檯的語法:
clrscr(); OR system(“cls”); OR system(“clear”);
以下是用 C 語言清除控制檯的示例:
假設我們有一個 "new.txt" 檔案,其中包含以下內容 −
0,hell!o 1,hello! 2,gfdtrhtrhrt 3,demo
現在,讓我們看這個示例。
示例
#include<stdio.h> #include<conio.h> void main() { FILE *f; char s; clrscr(); f=fopen("new.txt","r"); while((s=fgetc(f))!=EOF) { printf("%c",s); } fclose(f); getch(); }
輸出
0,hell!o 1,hello! 2,gfdtrhtrhrt 3,demo
在上面的程式中,我們有一個文字檔案 "new.txt"。檔案指標用於開啟和讀取檔案。它正在顯示檔案的內容。要清除控制檯,可以使用 clrscr()。
clrscr(); f=fopen("new.txt","r"); while((s=fgetc(f))!=EOF) { printf("%c",s); }
廣告