INTERVIEW QUESTIONS
C++
INPUT AND OUTPUT OPERATIONS IN C++
DETAILS
Question: How do you write a program which produces its own source code as its output?
Answer: 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...................<br><br>#include<stdio.h><br>void main()<br>{<br> FILE *fp,*ft;<br> char buff[100];<br> fp=fopen("pro.c","r");<br> if(fp==NULL)<br> {<br> printf("ERROR");<br> }<br> ft=fopen("data.txt","w+");<br> if(ft==NULL)<br> {<br> printf("ERROR");<br> }<br> while(getc(fp)!=EOF)<br> {<br> <br> fscanf(fp,"%s",buff);<br> printf("%s ",buff);<br> fprintf(ft,"%s ",buff);<br> }<br>}<br>
|
Question:
How do you write a program which produces its own source code as its output?
Answer:
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...................<br><br>#include<stdio.h><br>void main()<br>{<br> FILE *fp,*ft;<br> char buff[100];<br> fp=fopen("pro.c","r");<br> if(fp==NULL)<br> {<br> printf("ERROR");<br> }<br> ft=fopen("data.txt","w+");<br> if(ft==NULL)<br> {<br> printf("ERROR");<br> }<br> while(getc(fp)!=EOF)<br> {<br> <br> fscanf(fp,"%s",buff);<br> printf("%s ",buff);<br> fprintf(ft,"%s ",buff);<br> }<br>}<br> Source: CoolInterview.com
This seems to be taking longer than usual. Source: CoolInterview.com
Answered by: Ramesh | Date:
| Contact Ramesh
#include <iostream><br>#include <fstream><br>#include <string><br>using namespace std;<br><br>int main () {<br> string line;<br> <br> fstream myfile;<br> myfile.open("test.cpp",ios::in);<br> /* or use this: <br> ifstream myfile;<br> myfile.open("test.cpp");<br> */<br> <br> if(myfile.is_open()){<br> while(! myfile.eof()){<br> getline(myfile,line);<br> cout << line <<endl;<br> }<br> myfile.close();<br> }else{<br> cout<<"unable to open the file";<br> }<br> return 0;<br>} Source: CoolInterview.com
Answered by: Min | Date: 3/9/2009
| Contact Min
A compiled program cannot generate its sourece code. A compiled program does not know where its source lie. Source: CoolInterview.com
Answered by: Shantanu | Date: 4/23/2009
| Contact Shantanu
#include "stdafx.h"<br>#include "stdio.h"<br>#include "stdlib.h"<br>#include "iostream"<br>using namespace std;<br><br>class abc<br>{<br>public :<br>int main()<br>{<br>cout<<" Its main function "<<endl;<br>return 0;<br>}<br>};<br><br>int main(int c, char **v)<br>{<br>abc a;<br>/******************That will print the output**********/<br>char buff[100];<br>strcpy(buff, "type ");<br>strcat(buff, __FILE__);<br>system(buff);<br>/******************************************/<br>printf(" Address of main Function = %u ",main);<br>if(c<1)<br>{ cout<<" Error can not accept it "; }<br>cout<<" its in main program "<<a.main()<<endl;<br><br>return 0;<br>} Source: CoolInterview.com
Answered by: Rahul Gupta | Date: 4/24/2009
| Contact Rahul Gupta
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|