Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

perl script to execute a c programe

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

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

Technology Partners: 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!

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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 how to create, access, and change arrays in the C programming language.

730 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