简体   繁体   English

打印目录C程序中所有文件的内容

[英]Printing contents of all files in a directory C programming

Hello and good evening, 你好,晚上好,

I'm making a program that will gather all the files in a given directory and print out all of the integers inside them. 我正在编写一个程序,该程序将收集给定目录中的所有文件并打印出其中的所有整数。

This is homework but not THE homework. 这是家庭作业,而不是家庭作业。 This is a small part of it that I can't figure out. 这只是我无法弄清的一小部分。

Here's the code: 这是代码:

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <stdlib.h>
#include "mergefiles.c"

int main(int argc, char **argv) {

if(2 != argc)
{
    printf("Directory name missing!\n");
    return 0;
}
DIR *dp = NULL;
struct dirent *dptr = NULL;
FILE *input_file;
FILE *next_file;
char c;

if (NULL == (dp = opendir(argv[1])))
{
    printf("Cannot open the given directory %s", argv[1]);
    exit(1);
}
else
{
    while(NULL != (dptr = readdir(dp)))
    {
        if(!strcmp (dptr->d_name, "."))
        {
            continue;
        }
        if(!strcmp (dptr->d_name, ".."))
        {
            continue;
        }

        input_file = fopen(dptr->d_name, "r");
        if(input_file == NULL)
        {
            perror("Cannot open file or no file exists\n");
            fclose(input_file);

            return 0;
        }
        c = fgetc(input_file);
        while(c != EOF)
        {
            printf("%d", c);
        }
        printf(%s\n", dptr->d_name);
    }
    closedir(dp);
}
return 0;
}

I started with this slowly. 我慢慢开始。 First, I read a directory. 首先,我阅读目录。 Then I printed the names of all the files within the directory. 然后,我打印了目录中所有文件的名称。 All of that works just fine (which is really cool!). 所有这些都很好(真的很棒!)。

However, i'm getting a segfault whenever I try to do the fclose(input_file) within the first while loop . 不过,我得到一个segfault ,每当我尝试做fclose(input_file)中的第一个while loop At first, I thought it was because there was nothing in the file (I fixed this by putting something in the file), but that doesn't work. 起初,我认为这是因为文件中什么都没有(我通过在文件中放入内容来解决此问题),但这是行不通的。

Strangely, when I take out the fclose(input_file) , it will compile, but throw out the second error: Cannot open file or file doesn't exist . 奇怪的是,当我取出fclose(input_file) ,它将编译,但抛出第二个错误: Cannot open file or file doesn't exist

All the files contain integers, one on each line, of varying depth. 所有文件都包含整数,每行一个,深度不同。 I did my best to proofread the code so if there are any errors (typo's and such) let me know and I'll edit it right away. 我已尽力对代码进行校对,所以如果有任何错误(典型错误),请告诉我,我将立即对其进行编辑。

Thanks for any all words of wisdom. 感谢所有智慧的话。 :D :d

Update: 更新:

So I get a segfault because the input_file is NULL and I'm trying to close it...which makes sense and I can't believe I missed it. 所以,我得到一个segfault ,因为input_fileNULL ,我试图关闭它...这是有道理的,我不能相信我错过了。

What I don't understand is why the input_file is NULL in the first place. 我不明白的是为什么input_fileNULL

The reason why you get input_file equal to NULL is obvious, because there is no such file that you want to open is in the current directory. 之所以知道input_file等于NULL的原因是显而易见的,因为在当前目录中没有要打开的文件。

Suppose for example consider your working directory as (this is where you are compiling your C program) 例如,假设您的工作目录为(在此处编译C程序)

/home/student/Myfolder /家庭/学生/ MyFolder文件

Let us assume you want to open "Input" folder and there are two files 1.txt and 2.txt. 让我们假设您要打开“输入”文件夹,并且有两个文件1.txt和2.txt。

After compiling what compiler does for fopen() is it searches for file as 编译后,编译器对fopen()的作用是搜索文件为

/home/student/Myfolder/1.txt /home/student/Myfolder/1.txt

But the actual path is 但是实际路径是

/home/student/Myfolder/Input/1.txt /home/student/Myfolder/Input/1.txt

I hope you understand that. 希望您能理解。 To overcome this you have to get the current working directory cwd() function and then append the directory name and file name. 为了克服这个问题,您必须获取当前的工作目录cwd()函数,然后附加目录名和文件名。 Below is the complete program. 下面是完整的程序。 Please do note the commands I have made in the program. 请注意我在程序中所做的命令。

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char **argv) {

if(2 != argc)
{
    printf("Directory name missing!\n");
    return 0;
}
DIR *dp = NULL;
struct dirent *dptr = NULL;
FILE *input_file;
FILE *next_file;
char c,cwd[256]; //created new character array

if (NULL == (dp = opendir(argv[1])))
{
    printf("Cannot open the given directory %s", argv[1]);
    exit(1);
}
else
{
    while((dptr = readdir(dp))!=NULL)
    {
        if(!strcmp (dptr->d_name, "."))
        {
            continue;
        }
        if(!strcmp (dptr->d_name, ".."))
        {
            continue;
        }
    if(getcwd(cwd, sizeof(cwd)) == NULL) //this is important
    {
        printf("No such file or directory");
        continue;
    }
    if(dptr->d_name[0]=='.')  //Files never begin with '.' 
        continue;
    strcat(cwd,"/");   //For windows "\"
    strcat(cwd,argv[1]);
    strcat(cwd,"/");   //For windows "\"
    strcat(cwd,dptr->d_name);
    printf("%s",cwd);  //check for working directory
        input_file = fopen(cwd, "r");
    printf("%s\n",dptr->d_name); //validation check
        if(input_file == NULL)
        {
            perror("Cannot open file or no file exists\n");
            fclose(input_file);
            return 0;
        }
        c = fgetc(input_file);
        while(c != EOF)
        {
            printf("%d", c);
        }
        printf("%s\n", dptr->d_name);
    }
    closedir(dp);
}
return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM