Last active
August 29, 2015 14:05
-
-
Save BastinRobin/439ae2f4dad1927e57ab to your computer and use it in GitHub Desktop.
Run C Program using Filename as argument
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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