编写一个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 );//从文件中读取单个字符
写入文件的语法如下-
int fputc( int c, FILE *fp ); //将单个字符写入流
下面说明了用于在当前目录中显示文件和文件夹的逻辑,程序已保存在该目录中-
dr = opendir(".");
if(dr!=NULL){
printf("List of Files & Folders:-\n");
for(d=readdir(dr); d!=NULL; d=readdir(dr)){
printf("%s\n", d->d_name);
}
closedir(dr);
}示例
以下是用于打印目录中文件和文件夹的C程序-
#include#include #include int main() { struct dirent *d; DIR *dr; dr = opendir("."); if(dr!=NULL) { printf("List of Files & Folders:-\n"); for(d=readdir(dr); d!=NULL; d=readdir(dr)) { printf("%s\n", d->d_name); } closedir(dr); } else printf("\nerror 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
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短