Solved

perl script to execute a c programe

Posted on 2008-06-19
7
187 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

729 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