Solved

perl script to execute a c programe

Posted on 2008-06-19
7
183 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
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.

705 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

17 Experts available now in Live!

Get 1:1 Help Now