Solved

perl script to execute a c programe

Posted on 2008-06-19
7
184 Views
Last Modified: 2010-04-21
hello experts,

i want to create a file file.cpp with with following contents

File: file.cpp
int main()
{
int t = 5;
for() {
t = t + 1;
}

for()
t = t +5;
}

and then modify the file (using search and replace) to
File: file.cpp
#include <iostream>
using namespace std;
int main()
{
int t = 5;
for() {
printf( line no. %d, t);
t = t + 1;
}

for(){
printf( line no. %d, t);
t = t + 5;
}
}

Compile this file. If compilation successful, execute generated executable, if compilation not successful, print corresponding message.

please help me in solving the above problem.
0
Comment
Question by:rakeshreddy42
  • 4
  • 3
7 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 21820495
#!/bin/perl
open F,">file.cpp" or die $!;
print F <<END;
int main()
{
int t = 5;
for() {
t = t + 1;
}

for()
t = t +5;
}
END
close F;
{local @ARGV=("file.cpp"); local $^I=".bak";
while( <> ){
  print <<END if 1..1;
#include <iostream>
using namespace std;
END

print <<END if /\+/;
printf("line no. %d", t);
END
s/(for[^{]*?)$/$1 {/;
print;
}
}
system("c++ file.cpp") || system("./a.out");
0
 

Author Comment

by:rakeshreddy42
ID: 21820548
Hi Genius,

thank you for the code.but it is showing the following error in the command promp.But file.cpp was created correctly.


"c++ is not recognized as an internal command,operable program or batch file".
and please could you explain from the following.

{local @ARGV=("file.cpp"); local $^I=".bak";
while( <> ){
  print <<END if 1..1;
#include <iostream>
using namespace std;
END

print <<END if /\+/;
printf("line no. %d", t);
END
s/(for[^{]*?)$/$1 {/;
print;
}
}
system("c++ file.cpp") || system("./a.out");


0
 
LVL 84

Expert Comment

by:ozo
ID: 21820579
What is the name of your c++ compiler?

the code modifies the created file to the modified file
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:rakeshreddy42
ID: 21820620
Hi Genius,
 My c++ compiler was Devcpp.
Sorry the first time i saw the code i did not understand.Now i understood some thing in it.Still i did not understand the following lines.

 the line               {local @ARGV=("file.cpp"); local $^I=".bak";

and line                s/(for[^{]*?)$/$1 {/;
                             print;


                           
and the line        system("c++ file.cpp") || system("./a.out"); i could not understand.

Please take some pain in helping me.I am very new to perl.I want to become familiar of perl.Sorry for the trouble i am giving  you.
Thank you.
Rakesh.
0
 

Author Comment

by:rakeshreddy42
ID: 21820694
Hi Genius,

Also tell me some good book's for perl.
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 21821222
{local @ARGV=("file.cpp"); #file to read with <> 
local $^I=".bak"; # in place edit
see
perldoc perlvar

 s/(for[^{]*?)$/$1 {/;  #adds { to the end of lines containing "for" not followed by {
print; # prints the line

system("c++ file.cpp") executes the command
I guess it should have been
system("Devcpp file.cpp")
if the system call returns 0 (success) then evaluate the right of the || which executes the file that was just compiled (I don't know if Devcpp names the compiles executable a.out, if not replace it with the appropriate name)

0
 

Author Closing Comment

by:rakeshreddy42
ID: 31468725
HI OZo,

This TIME I MADE IT.THANKYOU  for your HELP.
COULD YOU PLEASE SEE MY ANOTHER QUESTION WHICH I POSTED TODAY AND GIVE SOLUTION .IT WAS "PERL SCRIPT TO COUNT NUMBER OF PYSICAL LINES AND BLANK LINE,LOGICAL LINES AND COMMMENT LINES ".I WAS ABLE TO DO PARTIALLY.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
add a syntax to a csv file 8 98
How to jump to matching brace in eclipse editor ? 1 264
Perl modules on linux ec2 3 96
Line meaning 9 82
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now