该程序自动换行文件,想要添加一个功能,如果我提供一个文件目录,并且其中有另一个文件目录也可以换行这些文件
所以基本上当用户在命令行中输入 ./ww -r 10 (目录名称) 我希望它对指定目录中的任何内容进行自动换行,并检查该目录中是否有子目录并对其进行换行...我卡住了...我知道 de-type==8 给了我一个文件键入但我不确定在此之后要做什么,....我试图使其递归,现在它与第一部分完美配合,如果我提供一个目录或文件,它将自动换行其中的所有这些文件,但是我似乎无法弄清楚如何检查文件夹是否有子目录,我认为我编码的方式只会真正工作一次来检查它是否是一个子目录,因为如果该子目录有一个包含更多文件等的子目录,我会做一个函数吗?我要启动一个pthread吗?是否可以在 if 语句中创建 pthread? C语言编程
if(argc == 3 || argc==4) {
if(argv[1][0]=='-'&& argv[1][1]=='r'){
file_width=atoi(argv[2]);
printf("Entered file with with the speical 'R' command is: %d\n",file_width);
dir = opendir(argv[3]);
while ((de = readdir(dir))) {
if(de->d_name[0] == '.' || (strncmp(de->d_name,"wrap",4) == 0)) {
if (DEBUG) printf("File ignored.\n");
continue; // ignore (.) and already wrapped files
}
if(de->d_type==8){
}
char s[270] = "wrap."; // temporary string for output file
chdir(argv[3]);
fd_in = open(de->d_name, O_RDONLY); // read current file in the directory
fd_out = open(strcat(s,de->d_name),O_RDWR|O_CREAT|O_APPEND | O_TRUNC, 0600); // create output file
if (DEBUG) printf("Current file name is: %s\n", de->d_name);
word_wrap(fd_in,fd_out);
close(fd_in);
close(fd_out);
}
}
so basically when the user enters in the command line ./ww -r 10 (name of directory)
i want it to word wrap whatever is in the specified directory and check if it has a sub directory within that directory and wrap that as well.... im stuck... i know that de-type==8 gives me a file type but im not sure what to do after this,.... im trying to make this recursive, right now it works perfectly with the first part if i supply a directory or file it will word wrap all those files in there, however i cant seem to figuire out how to check if the folder has subdirectories and i think the way im coding it would only really work once to check if its a subdirectory, because what if that subdirectory has a subdirectory that contains more files etc etc, do i make a function? do i start a pthread? is it possible to create a pthread in a if statement? C programming
if(argc == 3 || argc==4) {
if(argv[1][0]=='-'&& argv[1][1]=='r'){
file_width=atoi(argv[2]);
printf("Entered file with with the speical 'R' command is: %d\n",file_width);
dir = opendir(argv[3]);
while ((de = readdir(dir))) {
if(de->d_name[0] == '.' || (strncmp(de->d_name,"wrap",4) == 0)) {
if (DEBUG) printf("File ignored.\n");
continue; // ignore (.) and already wrapped files
}
if(de->d_type==8){
}
char s[270] = "wrap."; // temporary string for output file
chdir(argv[3]);
fd_in = open(de->d_name, O_RDONLY); // read current file in the directory
fd_out = open(strcat(s,de->d_name),O_RDWR|O_CREAT|O_APPEND | O_TRUNC, 0600); // create output file
if (DEBUG) printf("Current file name is: %s\n", de->d_name);
word_wrap(fd_in,fd_out);
close(fd_in);
close(fd_out);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经说过是试图制作此递归的,所以“我可以函数?”的简单答案是“是”。此函数从本质上包含您的代码从
opendir
行到块的末端,并进行了一些调整,尤其是当前工作目录的更改和递归函数调用,例如:
d_type
具有已知值的成员,您显然对可用此扩展名的系统(例如GNU/Linux)的代码感到满意,但请注意,只有某些文件系统对其有全面支持。)最初可以通过用
main
替换移动的代码来调用上述函数You already say you're trying to make this recursive, so the simple answer to "do i make a function?" is "yes". This function essentially contains your code from the
opendir
line to the end of the block, with some adjustments, especially the changes of the current working directory and the recursive function call, e. g.:(Since you already used the
d_type
member with a known value, you're obviously satisfied with the code working on systems such as GNU/Linux where this extension is available, but note that only some filesystems have full support for it.)The above function can be initially called by replacing the moved code in
main
with