编写一个 C 程序来打印所有文件和文件夹

文件是记录的集合(或)硬盘上永久存储数据的位置。

通过使用 C 命令,我们可以以不同的方式访问文件。

对文件的操作

下面给出的是可以在 C 编程语言中对文件执行的操作 -

  • 命名文件
  • 打开文件
  • 从文件中读取
  • 写入文件
  • 关闭文件

语法

分别打开和命名文件的语法如下 −

FILE *File pointer;

例如,FILE * fptr;

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

例如,fptr = fopen (“sample.txt”, “r”);

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

从文件中读取的语法如下 -

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

写入文件的语法如下 −

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

我们用来显示当前目录中的文件和文件夹的逻辑,保存的程序如下所述 -

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

以下是用于打印目录中的文件和文件夹的 C 程序 −

#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:-
");
      for(d=readdir(dr); d!=NULL; d=readdir(dr)) {
         printf("%s
", d->d_name);
      }
      closedir(dr);
   }
   else
   printf("
error while opening the directory!");
   getch();
   return 0;
}

输出

当上述程序被执行时,它会产生以下输出 -

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
  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

新华

感谢打赏,我会继续努力原创。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值