我希望读取特定文件夹中的所有文本文件。 文件的名字没有任何共同的模式,否则这个任务会变得更容易。
//read a file from the directory //Perform a common operation //write output to a common file //read the next file
如果我可以解决子文件夹问题,那将是一件好事,但即使是基本的实现也是足够的。
我试着查看以前提出的相关问题( 这里 , 这里和这里 ),但没有一个给出了我需要的C和Linux特定的答案。
编辑 :所以,这是我根据收到的答案写的 –
statvfs系统调用失败,错误值对于定义的数据types太大
seekg()读取文本文件时奇怪的偏移量
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <dirent.h> #include <unistd.h> #include <errno.h> int main(int argc,char **argv) { DIR* FD; struct dirent* in_file; FILE *output_file; FILE *entry_file; char buffer[BUFSIZ]; /* opening common file for writing */ output_file = fopen("/home/pnp/snort_rules_folder/rulesoutput.txt","a+"); if (output_file == NULL) { fprintf(stderr,"Error : Failed to open output_filen"); return 1; } /* Scanning the in directory */ if (NULL == (FD = opendir ("/home/pnp/snort_rules_folder/rules"))) { fprintf(stderr,"Error : Failed to open input directoryn"); fclose(output_file); return 1; } while ((in_file = readdir(FD))) { /* On linux/Unix we don't want current and parent directories * If you're on Windows machine remove this two lines */ if (!strcmp (in_file->d_name,".")) continue; if (!strcmp (in_file->d_name,"..")) continue; /* Open directory entry file for common operation */ /* Todo : change permissions to meet your need! */ entry_file = fopen(in_file->d_name,"r"); if (entry_file == NULL) { fprintf(stderr,"Error : Failed to open entry filen"); fclose(output_file); return 1; } /* Doing some stuff with entry_file : */ while (fgets(buffer,BUFSIZ,entry_file) != NULL) { /* Use fprintf or fwrite to write some stuff into common_file*/ } fprintf(output_file,"reading file %s",in_file->d_name); /* When you finish with the file,close it */ fclose(entry_file); } /* Don't forget to close common file before leaving */ fclose(output_file); return 0; }
收到的错误 –
pnp @ pnp-laptop:〜/ snort_rules_folder $ ./a.out
如何限制程序的文件权限
使用Java的文件/目录的networkingpath
寻找文本文件的编码
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <dirent.h> #include <unistd.h> #include <errno.h> /* This is just a sample code,modify it to meet your need */ int main(int argc,char **argv) { DIR* FD; struct dirent* in_file; FILE *common_file; FILE *entry_file; char buffer[BUFSIZ]; /* Openiing common file for writing */ common_file = fopen(path_to_your_common_file,"w"); if (common_file == NULL) { fprintf(stderr,"Error : Failed to open common_file - %sn",strerror(errno)); return 1; } /* Scanning the in directory */ if (NULL == (FD = opendir (in_dir))) { fprintf(stderr,"Error : Failed to open input directory - %sn",strerror(errno)); fclose(common_file); return 1; } while ((in_file = readdir(FD))) { /* On linux/Unix we don't want current and parent directories * On windows machine too,thanks Greg Hewgill */ if (!strcmp (in_file->d_name,"rw"); if (entry_file == NULL) { fprintf(stderr,"Error : Failed to open entry file - %sn",strerror(errno)); fclose(common_file); return 1; } /* Doing some struf with entry_file : */ /* For example use fgets */ while (fgets(buffer,entry_file) != NULL) { /* Use fprintf or fwrite to write some stuff into common_file*/ } /* When you finish with the file,close it */ fclose(entry_file); } /* Don't forget to close common file before leaving */ fclose(common_file); return 0; }
希望这个hellp。
问候。
这些应该让你开始:
opendir()
readdir()
closedir()
fwrite()
fclose()
请参阅其他C IO功能。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。