Home > Backend Development > C++ > body text

Write a C program to print all files and folders

王林
Release: 2023-08-26 12:53:06
forward
1401 people have browsed it

Write a C program to print all files and folders

A file is a collection of records (or) a place on a hard disk where data is permanently stored.

By using C commands, we can access files in different ways.

File operations

The following are the file operations that can be performed in the C programming language:

  • Name the file
  • Open the file
  • Read from the file
  • Write to the file
  • Close the file

Syntax

The syntax for opening and naming a file is as follows:

FILE *File pointer;
Copy after login

For example, FILE * fptr;

File pointer = fopen (“File name”, “mode”);
Copy after login

For example, fptr = fopen("sample.txt", "r");

FILE *fp;
fp = fopen (“sample.txt”, “w”);
Copy after login

The syntax for reading a file is as follows −

int fgetc( FILE * fp );// read a single character from a file
Copy after login

The syntax for writing files is as follows−

int fputc( int c, FILE *fp ); // write individual characters to a stream
Copy after login

The logic that we use to display the files and folders in current directory, where the program saved is explained below −

dr = opendir(".");
if(dr!=NULL){
   printf("List of Files & Folders:-</p><p>");
   for(d=readdir(dr); d!=NULL; d=readdir(dr)){
      printf("%s</p><p>", d->d_name);
   }
   closedir(dr);
}
Copy after login

Example

Following is the C program for printing the files and folders in a directory −

#include<stdio.h>
#include<conio.h>
#include<dirent.h>
int main() {
   struct dirent *d;
   DIR *dr;
   dr = opendir(".");
   if(dr!=NULL) {
      printf("List of Files & Folders:-</p><p>");
      for(d=readdir(dr); d!=NULL; d=readdir(dr)) {
         printf("%s</p><p>", d->d_name);
      }
      closedir(dr);
   }
   else
   printf("</p><p>error while opening the directory!");
   getch();
   return 0;
}
Copy after login

Output

When the above program is executed, it produces the following output:

List of Files & Folders:-
.
..
accessing array.c
accessing array.exe
accessing array.o
bhanu.txt
C Programs
convert 2 digit no into english word.c
convert 2 digit no into english word.exe
convert 2 digit no into english word.o
DATA
delete vowels in string.c
delete vowels in string.exe
delete vowels in string.o
emp.txt
EVEN
ex.c
ex.exe
ex.o
example pro.c
example pro.exe
example pro.o
fibbinoci serie.c
fibbinoci serie.exe
fibbinoci serie.o
file
file example1.c
file example1.exe
file example1.o
file example2.c
file example2.exe
file example2.o
implicit conversion.c
implicit conversion.exe
implicit conversion.o
leap year.c
leap year.exe
leap year.o
little n big endian.c
little n big endian.exe
little n big endian.o
work out examples
Copy after login

The above is the detailed content of Write a C program to print all files and folders. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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!