Home > Backend Development > C++ > body text

How to clear console in C language?

王林
Release: 2023-09-23 21:57:04
forward
1614 people have browsed it

How to clear console in C language?

There are several ways to clear the console or output screen, one of which is the clrscr() function. It clears the screen when the function is called. It is declared in the "conio.h" header file. There are some other methods such as system("cls") and system("clear"), which are declared in the "stdlib.h" header file.

The following is the syntax to clear the console in C language:

clrscr();
OR
system(“cls”);
OR
system(“clear”);
Copy after login

The following is an example of clearing the console in C language:

Suppose we have a file called " new.txt" file with the following content:

0,hell!o
1,hello!
2,gfdtrhtrhrt
3,demo
Copy after login
Copy after login

Now, let us look at an example.

Example

#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();
}
Copy after login

Output

0,hell!o
1,hello!
2,gfdtrhtrhrt
3,demo
Copy after login
Copy after login
In the above program, we have a text file "new.txt". Open and read the file using the file pointer. It displays the contents of the file. To clear the console, use clrscr()
clrscr();
f=fopen("new.txt","r");
while((s=fgetc(f))!=EOF) {
   printf("%c",s);
}
Copy after login

The above is the detailed content of How to clear console in C language?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!