Skip to content

Instantly share code, notes, and snippets.

@hygull
Last active December 20, 2016 09:41
Show Gist options
  • Select an option

  • Save hygull/bae8720b09903a1844a964001465af53 to your computer and use it in GitHub Desktop.

Select an option

Save hygull/bae8720b09903a1844a964001465af53 to your computer and use it in GitHub Desktop.
To display the source code of executing C program created by hygull - https://repl.it/EuAM/3
// {
// "created_before" => "Tues Dec 20 2016",
// "aim_of_program" => "To display the source code of executing C program",
// "memory" => "A modified/corrected version of C program located at https://www.programiz.com/c-programming/examples/source-code-output"
// "coded_by" => "Rishikesh Agrawani",
// }
#include<stdio.h>
int main(){
FILE *fptr;
char ch;
printf("Reading the current source file named %s\n",__FILE__);
fptr=fopen(__FILE__,"r");// fptr=fopen("__FILE__","r"); --> Segmentation fault: 11
do{
ch=getc(fptr); //get a character located at fptr, and advance fptr by 1 character
if (ch==EOF){
break; //If EOF file reached then break, otherwise it will be printers as '?' character
}
putchar(ch);
}while(1);
fclose(fptr); //Close the file after successful reading operation
return 0;
}
/* COMMANDS TO RUN USING gcc(from current working directory):-
(1)gcc -o file_read file_read.C (2) ./file_read )........Linux based system's(Mac OS X, Unix, Linux etc) usres
(2)gcc -o file_read file_read.C (2) .\file_read.exe).....Windows users
*/
/* OUTPUT:-
Reading the current source file named file_read.C
// {
// "created_before" => "Thu Dec 20 2016",
// "aim_of_program" => "To display the source code of executing C program",
// "memory" => "A modified/corrected version of C program located at https://www.programiz.com/c-programming/examples/source-code-output"
// "coded_by" => "Rishikesh Agrawani",
// }
#include<stdio.h>
int main(){
FILE *fptr;
char ch;
printf("Reading the current source file named %s\n",__FILE__);
fptr=fopen(__FILE__,"r");// fptr=fopen("__FILE__","r"); --> Segmentation fault: 11
do{
ch=getc(fptr); //get a character located at fptr, and advance fptr by 1 character
if (ch==EOF){
break; //If EOF file reached then break, otherwise it will be printers as '?' character
}
putchar(ch);
}while(1);
fclose(fptr); //Close the file after successful reading operation
return 0;
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment