Skip to content

Instantly share code, notes, and snippets.

@ariscop ariscop/with_macro.c
Created Sep 27, 2018

Embed
What would you like to do?
with macro in c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define with(_init, _fini) for(int _count = ((_init), 1); _count--; (_fini))
#define with_file(var, name, access) with(var = fopen(name, access), (var ? fclose(var) : 0 ))
void
test_macro(char *file)
{
FILE *fp;
with_file(fp, file, "rb") {
if(!fp)
printf("Error opening %s: %s\n", file, strerror(errno));
else
printf("Opened file %s\n", file);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.