-1

I need to write a function that will recursively display the contents of files whose names are contained in the first file. Additionally, I want the function to return the number of opened files.

eg.

first.txt:
    pull.txt
    predict.txt
    suppress.txt
    mount.txt
    transfer.txt
    
predict.txt:
    constitute.txt
    determine.txt
    swim.txt
    
Result:
    pull.txt
    predict.txt
        constitute.txt
        determine.txt
        swim.txt
    suppress.txt
    mount.txt
    transfer.txt
    
    Number of files: 2

So far I have managed to display the files, but I have no idea how to return the number of files.

int read_file(const char *filename){
    if(filename==NULL){
        return 0;
    }
    FILE *file;
    char tempName[31];
    if((file= fopen(filename,"r"))==NULL){
        return 0;
    }
    while (!feof(file)){
        fgets(tempName,31,file);
        tempName[strcspn(tempName, "\n")] = 0;
        printf("%s\n", tempName);
        read_file(tempName);
    }
    return 1;
}

P.S. I'm just starting to program, so please forgive me if my code is simple or poorly secured. This is part of an assignment that just needs an idea of how to execute and that's what I'm missing :) Thanks for any advice given in the comments.

I found the solution, thanks for all the hints :)

int read_file(const char* filename){
    if(filename==NULL){return -1;}
    FILE* file;
    char buffer[31];
    int counter=0, res;
    if((file= fopen(filename,"r"))==NULL){return -1;}
    while (!feof(file)){
        fgets(buffer,31,file);
        for (size_t i = 0; i < strlen(buffer); ++i) {
            if(*(buffer+i)=='\n'){*(buffer+i)=0;}
        }
        printf("%s\n", buffer);
        res= read_file(buffer);
        if(res!=-1){
            counter+=res;
        }
    }
    fclose(file);
    return counter;
}
  • What is the problem you have with the code you show? Do you get build errors? Wrong output? Crashes? Something else? Please read the [help pages](http://stackoverflow.com/help), take the [SO tour](https://stackoverflow.com/tour), read [How to Ask](https://stackoverflow.com/questions/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – fpiette Jul 09 '21 at 19:00
  • see https://stackoverflow.com/q/5431941/3476780 – yano Jul 09 '21 at 19:01
  • 1
    Obligatory: [Why is "while ( !feof( file ) )" always wrong?](https://stackoverflow.com/q/5431941/9952196) – Shawn Jul 09 '21 at 19:01
  • I see that predict.txt has other file names in it. How about the rest, are they simply empty files? Do you need to logically determine if each file contains file names or some other text data? – yano Jul 09 '21 at 19:06
  • 1
    Pay attention to infinite loop is a file already seen is seen in other. You should keep track of the list of seen files. – fpiette Jul 09 '21 at 19:06
  • @yano I only need to determine if any line of text is a filename of the existing file and if yes print its content. So far I got it more or less, my main concern is that function must return the number of files opened in the process. Also, I can't modify the function prototype :/ – Jakub Rejmak Jul 10 '21 at 13:59
  • I'm guessing `read_file` should return the number of files opened? You're using recursion, so you're going to want to add what each `read_file` returns to the one before it. I don't think you want to simply `return 1;` – yano Jul 10 '21 at 19:43

1 Answers1

1

I have no idea how to return the number of files.

Use a counter and pass a pointer to it as second argument of the function. Increment the counter each time a file is opened. Don't forget to initialize the counter to zero before calling the function at the top level.

fpiette
  • 11,983
  • 1
  • 24
  • 46
  • Any way to rewrite the function so it returns this value without modification to its declaration? Unfortunately, I have to do it that way. I've been returning to this problem for a month now and couldn't think of any more solutions than the second argument or global variable. – Jakub Rejmak Jul 10 '21 at 13:42
  • 1
    If you don't want (or can't) use the counter as an argument, you can use a global variable. – fpiette Jul 10 '21 at 16:33