Program to display its own source code as output using command-line argument(s) in C

Source Code

// 64. Program to display its own source code as output using command-line argument(s)
// Arpan Das - CST 2nd Year (VOCLET)

#include <stdio.h>
#include <string.h>
#include <malloc.h>
//#include <conio.h>

int main (int **argc, char **argv) {
	int i, maxStr = 1024;
	char filename [64];
	//clrscr();

	printf("\t\t..:: display its own source code as output using command-line argument(s) ::..\n\n");

	strcpy(filename, argv[0]);
	strcat(filename, ".c");

	FILE * handler = fopen(filename, "r");
	//char buf[maxStr];
	char * buf = (char *) malloc(maxStr);

	while (fgets(buf, maxStr, handler))
		printf("%s", buf);

	fclose(handler);

	//getch();
	return 0;
}

Output

..:: display its own source code as output using command-line argument(s) ::..

// 64. Program to display its own source code as output using command-line argument(s)
// Arpan Das - CST 2nd Year (VOCLET)

#include <stdio.h>
#include <string.h>
#include <malloc.h>
//#include <conio.h>

int main (int **argc, char **argv) {
	int i, maxStr = 1024;
	char filename [64];
	//clrscr();

	printf("\t\t..:: display its own source code as output using command-line argument(s) ::..\n\n");

	strcpy(filename, argv[0]);
	strcat(filename, ".c");

	FILE * handler = fopen(filename, "r");
	//char buf[maxStr];
	char * buf = (char *) malloc(maxStr);

	while (fgets(buf, maxStr, handler))
		printf("%s", buf);

	fclose(handler);

	//getch();
	return 0;
}

Leave a Reply

Note: Your email address will not be published. Required fields are marked *