-
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?
-
Re: using linux commands in C++ code
Why don't you just open a file, and write all your output to the file?
Viggy
-
Re: using linux commands in C++ code
-
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.
-
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/
-
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.
-
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.
-
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!
-
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?
-
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.
-
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
-
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...
-
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...
-
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 ;'(
-
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?
-
Re: using linux commands in C++ code
I don't have that much Linux experience but I doubt that it will be any significant change in speed. After all redirecting the output using the > is pretty much the same thing as redirecting it using code. Anyway, if you want to test it adding those lines in main() should be sufficient.
-
Re: using linux commands in C++ code
Alright. Thanks S_M_A. Let me try this.
-
Re: using linux commands in C++ code
S_M_A it worked in C++ code and was much faster than it was when written explicitly on command line :). Thanks
-
Re: using linux commands in C++ code
Someone suggested me this example "http://www.cplusplus.com/reference/iostream/ios/rdbuf/" which I found a bit easier and it worked as well but not when I tried to use it with the output of this command
Code:
system("diff /home/test1.txt /home/test2.txt");
Do you have any idea why? it still prints the output on to the screen and not to a file.
-
Re: using linux commands in C++ code
Hm sounds like a system call does not run in an exact copy of the parent process environment. Amazing also that there was a difference. You learn something new every day... :)
I guess you could change the diff call to redirect the output? Try that and I'll browse around abit to check if there's some other way to do it.
-
Re: using linux commands in C++ code
Thanks S_M_S
could you please write to me a line of code for changing the diff call to redirect the output?
-
Re: using linux commands in C++ code
Nope, I didn't find any new info regarding this. :(
You redirect just as you've done before by using > on the command line.
-
Re: using linux commands in C++ code
Alright Thanks a lot any way.
I have just found a code which is I think written in perl. Could you please tell me the meaning of each of these lines because I ran this code and it worked exactly what I needed
Code:
#!/bin/bash
counter=1
exec 3< /usr/local/test1.txt
while read -u3 line
do
grep "$line" /usr/local/test1.txt2>/dev/null
if [[ $? -eq 1 ]]
then
echo "Line $counter is not present in Naginas_Exp_0206_non_dupCME file!!!"
fi
(( counter += 1 ))
done
-
Re: using linux commands in C++ code
Quote:
Originally Posted by
heidiK
Alright Thanks a lot any way.
I have just found a code which is I think written in perl. Could you please tell me the meaning of each of these lines because I ran this code and it worked exactly what I needed
Code:
#!/bin/bash
counter=1
exec 3< /usr/local/test1.txt
while read -u3 line
do
grep "$line" /usr/local/test1.txt2>/dev/null
if [[ $? -eq 1 ]]
then
echo "Line $counter is not present in Naginas_Exp_0206_non_dupCME file!!!"
fi
(( counter += 1 ))
done
That's a bash script, not PERL. Just type 'man bash' on your *NIX terminal.
Viggy
-
Re: using linux commands in C++ code
-
Re: using linux commands in C++ code
Here you can check out the most used linux commands .