Có عدة cách xóa màn hình console:
clrscr()– Hàm cũ, trong<conio.h>(Turbo C, Windows cũ).system("cls")– Gọi lệnh shell Windows, trong<stdlib.h>.system("clear")– Gọi lệnh shell Linux/Unix, trong<stdlib.h>.
Cú pháp
clrscr();
// HOẶC
system("cls"); // Windows
system("clear"); // Linux/Unix
Ví dụ (Windows, dùng conio.h)
#include <stdio.h>
#include <conio.h>
void main() {
FILE *f;
char ch;
clrscr(); // Xoa man hinh
f = fopen("new.txt", "r");
while ((ch = fgetc(f)) != EOF)
printf("%c", ch);
fclose(f);
getch();
}
Giả sử file new.txt có nội dung:
0,hell!o 1,hello! 2,gfdtrhtrhrt 3,demo
Kết quả
0,hell!o 1,hello! 2,gfdtrhtrhrt 3,demo
Lưu ý:
conio.hvàclrscr()không chuẩn, chỉ hoạt động trên một số compiler cũ (Turbo C, một số Windows).system("cls")/system("clear")phụ thuộc OS, chậm (tạo process con), không an toàn (injection).- Cách hiện đại, cross-platform: In ANSI escape code
printf("\033[2J\033[H")(hoạt động trên terminal hỗ trợ ANSI). - Trong C++ GUI/Modern: Dùng thư viện như ncurses, hoặc clear screen của IDE.