文件是记录的集合(或者)硬盘上永久存储数据的地方。
通过使用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
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:-
"); for(d=readdir(dr); d!=null; d=readdir(dr)){ printf("%s
", d->d_name); } closedir(dr);}
examplefollowing 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:-
"); 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.caccessing array.exeaccessing array.obhanu.txtc programsconvert 2 digit no into english word.cconvert 2 digit no into english word.execonvert 2 digit no into english word.odatadelete vowels in string.cdelete vowels in string.exedelete vowels in string.oemp.txtevenex.cex.exeex.oexample pro.cexample pro.exeexample pro.ofibbinoci serie.cfibbinoci serie.exefibbinoci serie.ofilefile example1.cfile example1.exefile example1.ofile example2.cfile example2.exefile example2.oimplicit conversion.cimplicit conversion.exeimplicit conversion.oleap year.cleap year.exeleap year.olittle n big endian.clittle n big endian.exelittle n big endian.owork out examples
以上就是编写一个c程序来打印所有文件和文件夹的详细内容。