文件是记录的集合(或者)硬盘上永久存储数据的地方。
通过使用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);}
登录后复制
Example
Following is the C program for printing the files and folders in a directory −
<!—
Live Demo
–>
#include#include#includeint 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程序来打印所有文件和文件夹的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2587166.html