Friday, 26 January 2018

Write a program to strip comments from an input “C” program file and store in output file. System Programming GTU 2150708


Program:-
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
            fstream f1,f2;
            char ch;
            int c=0;
            f1.open("Sample.txt",ios::in);
            f2.open("Sample2.txt",ios::app);
            while(!f1.eof())
            {
                        f1.get(ch);
                        if(ch=='/')
                        {
                                    c++;
                        }
                        f2<<ch;
                        cout<<ch;
                        if(ch=='/' && c==2)
                        {
                                    while(ch!='\n')
                                    {
                                                f1.get(ch);
                                    }
                                    f2<<ch;
                                    cout<<ch;
                                    c=0;
                        }
            }
            f1.close();
            f2.close();
            return 0;
}




Output:-


[root@SAR117 Desktop]# gcc comment.c
[root@SAR117 Desktop]# ./a.out
[root@SAR117 Desktop]# cat Sample.txt
void main()
{
// body of program
   int a;
/*
enter some value for a.
print the value of a. */
printf(“enter the value of a:");
scanf(“%d”,&a);
printf(“The value of a is:%d”,a);
getch();
}
[root@SAR117 Desktop]# cat Sample2.txt
void main()
{
   int a;
printf(“enter the value of a:");
scanf(“%d”,&a);
printf(“The value of a is:%d”,a);
getch();
}
[root@SAR117 Desktop]#

No comments:

Post a Comment

It's time To increase blogging capability. To have a chance to contribute in digital world. Any Interested People who want to make t...