Skip to content

Instantly share code, notes, and snippets.

@tot0rokr
Created October 26, 2018 07:27
Show Gist options
  • Save tot0rokr/b62410dfed7d89668d162320e58637f2 to your computer and use it in GitHub Desktop.
Save tot0rokr/b62410dfed7d89668d162320e58637f2 to your computer and use it in GitHub Desktop.
logical operation trick
#include <stdio.h>
void file_open(FILE** fp)
{
char str[1024];
*fp || (((*fp = fopen("test.txt", "w"), fprintf(*fp, "hello!\n")), fclose(*fp)), *fp = fopen("test.txt", "r"));
fscanf(*fp, "%s", str);
printf("%s", str);
}
int main()
{
FILE *in;
in = fopen("test.txt", "r");
file_open(&in);
fclose(in);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment