Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   give me the code (http://www.go4expert.com/forums/code-t21238/)

krazytechno 7Mar2010 15:41

give me the code
 
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");
}

techgeek.in 7Mar2010 15:59

Re: give me the code
 
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();
}


lipun4u 7Mar2010 16:57

Re: give me the code
 
Code: CPP

#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();
}


xpi0t0s 8Mar2010 13:54

Re: give me the code
 
The simplest program that will display its own source is an empty file.

virxen 8Mar2010 16:44

Re: give me the code
 
http://dwcope.freeshell.org/projects/quine/

rekha_sri 8Mar2010 17:56

Re: give me the code
 
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;
}



All times are GMT +5.5. The time now is 06:51.