-
November 4th, 2010, 11:30 AM
#1
using linux commands in C++ code
Hello Everyone.
I wanted to know if there is any way I can use linux commands in C++. I want to store the output of a program in a file which is otherwise displayed on the screen using cout. I want that output to be stored in a file instead of displaying it on the screen. For example I write
on the command line and it writes the output in a file called abc.log. So is there any way I can use this command inside C++ code?
-
November 4th, 2010, 11:52 AM
#2
Re: using linux commands in C++ code
Why don't you just open a file, and write all your output to the file?
Viggy
-
November 4th, 2010, 11:53 AM
#3
Re: using linux commands in C++ code
-
November 4th, 2010, 12:05 PM
#4
Re: using linux commands in C++ code
MrViggy I want to avoid file opening closing process so I needed a way we could use the linux command inside c++ code.
Thanks Andrey but I did not understand that example. I am new to linux actually.
-
November 4th, 2010, 12:06 PM
#5
Re: using linux commands in C++ code
how can I use this example when the cout statement is written in more than just one function? Could you please help.
http://www.cplusplus.com/reference/iostream/ios/rdbuf/
-
November 4th, 2010, 12:10 PM
#6
Re: using linux commands in C++ code
I'm confused. Are you trying to write a program which calls another program and causes the second one to be redirected to a file? Or are you attempting to redirect the output of your own program to a file?
In the first case, system() is exactly what you want.
In the second case, it's easiest if your code has been written so that it outputs to a generic ostream rather than cout specifically----then you can just pass in an ofstream instead of cout. If you actually need to redirect cout, that's a bit more involved but also doable.
-
November 4th, 2010, 12:17 PM
#7
Re: using linux commands in C++ code
Lindley! I want to redirect the output of my own program to a file. The application also requires the output to be displayed on the screen. I have to compare the output of one program to the output of another program before the output is actually displayed on the screen and for that I have to save the two outputs from two different programs to two different files and I am currently doing it manually through line. Could you please help.
-
November 4th, 2010, 12:20 PM
#8
Re: using linux commands in C++ code
I'm confused too.
If you just need to run a program from your code form a command and pass it to system():
Code:
int main()
{
system( "./runApp > abc.log" );
}
Describe what are you going to do otherwise!
-
November 4th, 2010, 12:24 PM
#9
Re: using linux commands in C++ code
I'm not sure I have a firm enough grasp on what you need to do yet. Can you post the instructions?
-
November 4th, 2010, 12:26 PM
#10
Re: using linux commands in C++ code
It's quite easy to do this without any redirection of streams.
How are you going to call your program?
P.S. You may use 'cat' command to print a file to console.
-
November 4th, 2010, 12:29 PM
#11
Re: using linux commands in C++ code
Andrey!
Actually there is an application that uses vectors having duplicate values in it. I had to store those duplicates in another vector keeping the unique ones in the original vector. Then I selected one of the duplicate values out of many based on some condition and put it back to the original vector so that the original vector now contains only unique values based on that condition.
I want to compare the output of the two programs. One that is having duplicate values and one that is not having duplicates
-
November 4th, 2010, 12:30 PM
#12
Re: using linux commands in C++ code
Well, run your program like "./myprogram >file1.txt".
From your program start another program with system( "./otherprogram >file2.txt" );
Compare file1.txt and file2.txt somehow.
Execute system( "cat file1.txt" ).
But this solution seems me strange.
I'd use scripts (not C++) for this kind of stuff...
Last edited by andrey_zh; November 4th, 2010 at 12:49 PM.
-
November 4th, 2010, 12:47 PM
#13
Re: using linux commands in C++ code
That's my advice related to your post from 07:29 PM.
First, use batch (seems this is bash .sh in Linux) files.
Let your programs are named getUnique and getDuplicates.
Run 'em and stream into 2 different files somehow.
./getUnique >f1.txt
./getDuplicates >f2.txt
Compare f1 with f2 in any desired way. If you have no clue how to open f1 and f2 ask this directly now!
React on the result on comparison!
But, If you need to exchange data between two constantly running programs, you are facing a much more complex problem...
-
November 5th, 2010, 04:14 AM
#14
Re: using linux commands in C++ code
Good Morning Andrey
No I am not exchanging data between two programs. I just want to compare if the file without duplicates has missed any data from the original file to make sure that unique file contains all data from the duplicate file but only one copy of each value.
I am a recent graduate and have recently started my job so I dont know much about linux/scritping. My boss was also suggesting some kind of perl scripting for comparison. Do you have any idea how to do it?
More over this command ./getUnique >f1.txt takes a LOT of time because the duplicate file contains almost 70000 lines ;'(
-
November 5th, 2010, 04:17 AM
#15
Re: using linux commands in C++ code
some one has suggested me to use this example
http://www.cplusplus.com/reference/iostream/ios/rdbuf/
but I dont know where to include these lines in my program. I have many classes in this program which are printing out the output to the screen while main() is written in a separate .cpp file. So I dont know where to add these lines in my program. And will this process be faster then what doing it from the command line?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|