CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    Join Date
    Oct 2010
    Posts
    106

    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

    Code:
    ./runApp > abc.log
    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?

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: using linux commands in C++ code

    Why don't you just open a file, and write all your output to the file?

    Viggy

  3. #3
    Join Date
    Mar 2009
    Location
    Riga, Latvia
    Posts
    128

    Re: using linux commands in C++ code


  4. #4
    Join Date
    Oct 2010
    Posts
    106

    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.

  5. #5
    Join Date
    Oct 2010
    Posts
    106

    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/

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  7. #7
    Join Date
    Oct 2010
    Posts
    106

    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.

  8. #8
    Join Date
    Mar 2009
    Location
    Riga, Latvia
    Posts
    128

    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!

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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?

  10. #10
    Join Date
    Mar 2009
    Location
    Riga, Latvia
    Posts
    128

    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.

  11. #11
    Join Date
    Oct 2010
    Posts
    106

    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

  12. #12
    Join Date
    Mar 2009
    Location
    Riga, Latvia
    Posts
    128

    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.

  13. #13
    Join Date
    Mar 2009
    Location
    Riga, Latvia
    Posts
    128

    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...

  14. #14
    Join Date
    Oct 2010
    Posts
    106

    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 ;'(

  15. #15
    Join Date
    Oct 2010
    Posts
    106

    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?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured