Thread: sprintf f open pattern

  1. #1
    Registered User
    Join Date
    Jun 2018
    Posts
    10

    Post sprintf f open pattern

    Greetings.

    I'm writing a program that opens files given by /route/file_name. Such program receives the file_name via stdout from bash, i.e ./main < <(ls ../script/) where ls ../script/ gives the output dummy0.log, dummy1.log ... dummyn.log

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #include "search.h"
    #include "dbg.h"
    
    int search_line(void){
        char file_name[BUFSIZE];
        char route_name[BUFSIZE];
        int c;
    
        FILE *log;
    
        /* Reads file_name from stdout */
        while (fgets(file_name, BUFSIZE, stdin) != NULL){
            
            /* Only read file if such has *.log filetype */
            if ( is_approved_type(file_name) ){
               
                /* Variable subtitution pattern */
                sprintf(route_name, "../script/%s", file_name);
                
                /* Does not work, 
                 * in gdb, command 'printf "%s", route_name" gives       
                 * "../script/dummy0.log" */
                log = fopen(route_name, "r");
               
               /* The following does work and opens file
                * log = fopen(../script/dummy0.log */            
    
                /* Checks stream related errors*/
                check(log, "Error opening %s", route_name);
    
                /*  Checks all file's content */
                while(1){
                    c = fgetc(log);
    
                    if ( feof(log) )
                        break;
    
                    printf("%c", c);
                }
    
                /* Close stream */
                fclose(log);
    
                /* Error code */
                error:
                    if (log)
                        fclose(log);
                    return -1;
            }
        }
    
        return 0;
    }
    Being more specific, every time I use sprintf to make the variable substitution I get the following error: "errno: No such file or directory" whereas using hard-coded fopen filename executes the program correctly.

    As far I see the string "route_name" is identical to ../script/dummy0.log so, why is this program complaining once I'm using variable substitution?

    Any help is greatly appreciated.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Well you probably want to strip the \n off the end of the line returned by fgets().

    See the FAQ.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2018
    Posts
    10
    You're right, That was the problem. Thanks to you good sir

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-12-2010, 08:02 PM
  2. sprintf()
    By reza_45 in forum C Programming
    Replies: 7
    Last Post: 08-15-2008, 04:40 PM
  3. Open source, easy-to-use XMPP messenger: Open-IM
    By mkruk in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 06-22-2008, 04:10 PM
  4. Open Source / Semi Open source game idea. Help needed
    By CaptainPatent in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 05-16-2007, 10:44 AM
  5. Command to open the 'Open Dialog Box' found in most programs.
    By OmegaFirebolt in forum Windows Programming
    Replies: 5
    Last Post: 03-16-2003, 08:58 PM

Tags for this Thread