i want to write a programm such this programm will print the same code written on the editor.for eg #include<stdio.h> #include<conio.h> void main() { int i; for(i=0;i<5;i++) printf("MY NAME IS SUJEET"); } OUTPUT: #include<stdio.h> #include<conio.h> void main() { int i; for(i=0;i<5;i++) printf("MY NAME IS SUJEET"); }
see this:- Code: #include<stdio.h> #include<conio.h> void main() { int i; FILE *fp; int c; fp = fopen("prog.c","r"); //open the same program source code file c = getc(fp) ; while (c!= EOF) { putchar(c); c = getc(fp); } fclose(fp); getch(); }
Code: #include<stdio.h> #include<conio.h> void main() { int i; FILE *fp; int c; fp = fopen(__FILE__,"r"); //use preprocessor to know the file name c = getc(fp) ; while (c!= EOF) { putchar(c); c = getc(fp); } fclose(fp); getch(); }
Use the following program.It will be give the correct ouput what u have expected. Code: #include <stdio.h> int main ( void ) { char filename[] = "file.txt"; FILE *file = fopen ( filename, "r" ); if (file != NULL) { char line [1024]; while(fgets(line,sizeof line,file)!= NULL) /* read a line from a file */ { fprintf(stdout,"%s",line); //print the file contents on stdout. } fclose(file); } else { perror(filename); //print the error message on stderr. } return 0; }