Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. I check, compile and run every program. Contact me at: aviboots(AT)netvision.net.il
Hostgator 1 Penny

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Distribute your Resume to 500+ Job Boards & Recruiters


6,849 questions

9,751 answers

573 users

How to Implement a function like printf() in C

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

void verrmsg(int errnum, const char *fmt, va_list va)
{
    if (fmt)
        vfprintf(stderr, fmt, va);
    if (errnum != 0)
        fprintf(stderr, ": %s", strerror(errnum));
    putc(' ', stderr);
}

void errmsg(int exitcode, int errnum, const char *fmt, ...)
{
    va_list va;
    
    va_start(va, fmt);
    verrmsg(errnum, fmt, va);
    va_end(va);
    exit(exitcode);
}


int main (void) 
{
    FILE *fp;
    const char *filename = "d:\\notexistingfile.txt";

    if ((fp = fopen(filename, "r")) == NULL)
        errmsg(EXIT_FAILURE, errno, "cannot open %s", filename);

  
    return 0;
}

    
/*
run:
 
cannot open d:\notexistingfile.txt: No such file or directory
   
*/

 




answered Aug 17, 2017 by avibootz
...