Thread: Read a file with filenames

  1. #1
    Registered User
    Join Date
    Nov 2022
    Posts
    5

    Read a file with filenames

    Hi!

    I'm making a program which reads a file which contains names of files. Then, I have to call a function that opens these files.

    I made this piece of code (clarification: I put 3 in the looping considering that there are only 3 namefiles in the file).

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void read_file(char* name){  
        FILE *file;
        file=fopen(name,"r");
        if(file==NULL){
            printf("We couldn't open the file\n");
            exit(1);
        }   
    }
    
    int main(){
    
        char name[50];
        char aux[50];
        int i;
        FILE* file;
    
        printf("Insert the file with filenames: ");
        scanf("%s\n",name);
    
        file=fopen(name,"r");
        if(file==NULL){
            printf("We couldn't open the filee\n");
            exit(1);
        }  
    
        for(i=0;i<3;i++){
            fscanf(file,"%[^\n]\n",aux);
            read_file(aux);
        }  
    }
    
    
    However, I am still unable to open the files because the function doesn't works.

    I'd be grateful if someone could help me.

    Thank you very much,
    OlgaFontae.

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Several problems:

    I would use fgets() instead of scanf() to read in filenames, but you would need to remove the newline at the end of the string input.
    Code:
    // Instead of:
    scanf("%s\n",name);
    // Use:
    scanf(" %s", name);
    You assume there at least three filenames in the file entered. Could there be more or less than three filenames???


    In the function: "read_file(char* name)" you open the file but do nothing, and you do not close the file before exiting.

    Make these changes until the code works, then add the processing of the files in read_file().

  3. #3
    Registered User
    Join Date
    Nov 2022
    Posts
    5
    Thank you for answering my question.

    For this particular case, the file only contains three filenames.

    I make your suggested changes to my code and now the program opens the file with the filenames.
    However, the filenames contained do not open yet.

    Waiting to your reply,
    Thank you very much.,
    OlgaFontae.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Compare this with your code. I added printf() lines to see what has been executed. If you still have problems, please post your code so we can help debug.
    Also, post the contents of the file containing the filenames.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void read_file(char* name){
        FILE *file;
        file=fopen(name,"r");
        if(file==NULL){
            printf("We couldn't open the file\n");
            exit(1);
        }
        else
        {
          printf("The file has been opened\n");
        }
        // Code to read data from file
    
        fclose(file);
    }
    
    int main(){
    
        char name[50];
        char aux[50];
        int i;
        FILE* file;
    
        printf("Insert the file with filenames: ");
        scanf(" %s", name);
        printf("Filename: [%s]\n", name);
    
        file=fopen(name,"r");
        if(file==NULL){
            printf("We couldn't open the file\n");
            exit(1);
        }
    
        for(i=0;i<3;i++){
            fscanf(file,"%[^\n]\n",aux);
            printf("Filename %d: [%s]\n", i + 1, aux);
          read_file(aux);
        }
    }
    Last edited by rstanley; 12-26-2022 at 10:05 AM.

  5. #5
    Registered User
    Join Date
    Nov 2022
    Posts
    5
    Hi again,

    I copy your code and it doesn't work.

    I'll post the content of the file I tried with.

    mydog.txt
    mycat.txt
    myhorse.txt

    I think the error might have to do something with removing the end of line character ('\n'), but it shouldn't be read with fscanf, so I have no idea.

    Thank you again,
    OlgaFontae.

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Quote Originally Posted by OlgaFontae View Post
    Hi again,

    I copy your code and it doesn't work.

    I'll post the content of the file I tried with.

    mydog.txt
    mycat.txt
    myhorse.txt

    I think the error might have to do something with removing the end of line character ('\n'), but it shouldn't be read with fscanf, so I have no idea.

    Thank you again,
    OlgaFontae.
    My code works fine on Linux with gcc 12.

    What compiler & O/S are you using?

    As I said, you might want to switch to fgets() rather than scanf().

  7. #7
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    It's always better to break down a task into smaller tasks
    Maybe you can work with this skeleton

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void read_file(char* name){
        printf("Filename received: %s\n", name);
        // TO DO add your logic
    }
    
    void process_file(char *name)
    {
        printf("Processing: %s", name);
        // TO DO 
        // open file and read all filenames using read_line 
        // send each filename to read_file 
    }
    
    char *read_line(char *buffer, int buf_size, FILE *stream)
    {
        if(!fgets(buffer, buf_size, stream))
            return NULL;
        char *pos = strchr(buffer, '\n');
        if(pos)
            *pos = '\0';
            
        return buffer;
    }
    
    
    int main()
    {
        char name[256] = "";
        printf("Enter filename: ");
        read_line(name, 255, stdin);
        process_file(name);
    }



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-07-2014, 07:01 AM
  2. Replies: 7
    Last Post: 12-07-2012, 10:44 PM
  3. read from standard input a list of filenames?
    By Roger in forum C Programming
    Replies: 1
    Last Post: 10-27-2009, 06:13 PM
  4. How can I know the actual bytes read in a file read
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2005, 04:23 PM
  5. Getting filenames from a file
    By vinsta18 in forum C++ Programming
    Replies: 1
    Last Post: 01-12-2005, 11:52 AM

Tags for this Thread