CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS LANGUAGES C++ DETAILS
Question :
How do you write a program which produces its own source code as its output?


Category C++ Interview Questions
Rating (5.0) By 8 users
Added on 7/19/2006
Views 2813
Rate it!
Answers:

write this program and save it as pro.c....run....it will show the program to the consol and write the source code into data.txt file...................

#include<stdio.h>
void main()
{
FILE *fp,*ft;
char buff[100];
fp=fopen("pro.c","r");
if(fp==NULL)
{
printf("ERROR");
}
ft=fopen("data.txt","w+");
if(ft==NULL)
{
printf("ERROR");
}
while(getc(fp)!=EOF)
{

fscanf(fp,"%s",buff);
printf("%s ",buff);
fprintf(ft,"%s ",buff);
}
}



This seems to be taking longer than usual.



 Posted by: Ramesh    

Contact Ramesh  Contact Ramesh

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
string line;

fstream myfile;
myfile.open("test.cpp",ios::in);
/* or use this:
ifstream myfile;
myfile.open("test.cpp");
*/

if(myfile.is_open()){
while(! myfile.eof()){
getline(myfile,line);
cout << line <<endl;
}
myfile.close();
}else{
cout<<"unable to open the file";
}
return 0;
}



 Posted by: Min    

Contact Min  Contact Min

A compiled program cannot generate its sourece code. A compiled program does not know where its source lie.



 Posted by: Shantanu    

Contact Shantanu  Contact Shantanu

#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "iostream"
using namespace std;

class abc
{
public :
int main()
{
cout<<" Its main function "<<endl;
return 0;
}
};

int main(int c, char **v)
{
abc a;
/******************That will print the output**********/
char buff[100];
strcpy(buff, "type ");
strcat(buff, __FILE__);
system(buff);
/******************************************/
printf(" Address of main Function = %u ",main);
if(c<1)
{ cout<<" Error can not accept it "; }
cout<<" its in main program "<<a.main()<<endl;

return 0;
}



 Posted by: Rahul Gupta    

Contact Rahul Gupta  Contact Rahul Gupta


If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
Define copy constructor? What is the use of copy constructor?

View Answer
What is a memory leak? How can we avoid it?
View Answer
What is the difference between Function and Member function?



View Answer
In C++, what is a constructor,destructor?
View Answer
What is a class?
View Answer
What is an object?
View Answer
What is public, protected, private?
View Answer
What is the difference between class and structure?
View Answer
What is friend function?
View Answer
What are virtual functions?
View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C++ Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/6080/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

1.2