Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

perl script to execute a c programe

Posted on 2008-06-19
7
Medium Priority
?
190 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 85

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 85

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 85

Accepted Solution

by:
ozo earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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 …
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Six Sigma Control Plans

963 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