Skip to content

Instantly share code, notes, and snippets.

@BastinRobin
Last active August 29, 2015 14:05
Show Gist options
  • Save BastinRobin/439ae2f4dad1927e57ab to your computer and use it in GitHub Desktop.
Save BastinRobin/439ae2f4dad1927e57ab to your computer and use it in GitHub Desktop.
Run C Program using Filename as argument
/**
compile: gcc execute.c
run : ./a.out "filename"
**/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char ch, file_name[25];
FILE *fp;
fp = fopen(argv[1],"r"); // read mode
if( fp == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
printf("%s", file_name);
while( ( ch = fgetc(fp) ) != EOF )
printf("%c",ch);
fclose(fp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment