LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-17-2013, 12:57 PM   #1
anuRV
LQ Newbie
 
Registered: Jul 2013
Posts: 7

Rep: Reputation: Disabled
Redirect the system() output to a file


Hi,
I am trying to redirect the output by system() on the terminal to a file.
========My code=========
stringstream stream;
ofstream myoutfile ("outFile.txt");
if (myoutfile.is_open())
{
stream << "echo -n example | openssl sha1";
system("stream.str().c_str() >> outFile.txt"); // does not work
}
=====Error====
sh: -c: line 0: syntax error near unexpected token `.c_str'
sh: -c: line 0: `stream.str().c_str() >> outFile.txt'
=========
If if replace the statement by the below, it works
system(stream.str().c_str()); //output to terminal
system("ls >> outFile.txt"); // ls output to the file

Any help would be appreciated.
Thanks!!
 
Old 10-17-2013, 03:15 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Really struggling here to find out what you're doing.

My instincts tell me that you're writing a shell script.

So you can echo data to a file, and have it create new or append.

Code:
echo "The first argument is $1" > new-file.txt
echo "The first argument is $1" >> append-file.txt
If you're writing C or C++ code and have data you wish to write to a file, then why don't you use open(2), write(2), and close(2)?

Code:
int handle;
int ret;
char data[8] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 };

handle = open("file-path-and-name", "a");
if(handle < 0) {
    // error, condition, exit
}

ret = write(handle, data, sizeof(data));
if(ret != sizeof(data)) {
    // check errno for RETRY if 0 <= ret < sizeof(data) - otherwise if ret < 0, true error
}

close(handle);
That writes data to the file using a C program.

Many, many people will tell you to not use the system() command EVER. It's a security breach. I learned it long ago and understood the problem, but so long ago that I've forgotten it and instead just know that you never use it. This prompted me to wonder whether or not the command is deprecated or something, so I took a look at the man page and it at least says this:

Quote:
Do not use system() from a program with set-user-ID or set-group-ID privileges, because strange values for some environment variables might be used to subvert system integrity.
 
1 members found this post helpful.
Old 10-17-2013, 03:37 PM   #3
anuRV
LQ Newbie
 
Registered: Jul 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Hi,

Thanks for your valuable inputs.

I am trying to develop a tool(executable) in c++, which reads a file and does the SHA1 encoding to each line and writes the output to another file.
If I refrain from using system(), what else can I consider using to run a terminal command from my c++ program?

Thanks again!!
 
Old 10-18-2013, 07:52 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
I've not done much building of a terminal emulator beyond simple fgets(). If I've been in a programming environment where there are graphical entry fields which are pre-defineable by the tools, then I've been able to do "nice" looking stuff. For instance designing a Windows Form using Visual Studio. Or in Qt where you can use a Widget where you get an entry field and it manages things like the cursor or other.

My point there being, what I'm showing here is very basic, limited to the command prompt or terminal in which you are running this program and if you desire something more graphical, then you'd need to use a graphical development environment such as one of the one's I've cited above; obviously a correct one for the target OS you're working in.

The most basic thing you can do is the following:

term.c sample:

Code:
#include <stdio.h>

void main(void)
{
    char inStr[256];

    printf("Enter your string: ");
    if(fgets(inStr, sizeof(inStr), stdin)) {
        printf("You entered: %s", inStr);
    }
    return;
}
Compile and run this via the following example:

Code:
~/testcode$ gcc -o term term.c
~/testcode$ ./term
Enter your string: This is my test text, and I intentionally made a mistake and used backspace.  See, it's still correct and includes the carriage return.
You entered: This is my test text, and I intentionally made a mistake and used backspace.  See, it's still correct and includes the carriage return.
~/testcode$
Explanation:

gcc is the gnu C compiler, you either have it or you don't, it will compile C and C++ code.

-o means redirect the output to the following name "term" because if you don't do that, then it makes the output result be "a.out"

The result is therefore an executable named "term", I ran it and it did what I expected. It printed my prompt and allowed me to enter a line of text, up to 256 characters.

I did not try to over-run that and see whether or not it worked; you can explore that and make input foolproof. But if you read the text, I intentionally typed wrong. OK I "really" did make a mistake because I always tend to type "test" instead of "text", so I backspaced to correct it, wondering "Hmm ... is this gonna show up weird when it prints, or will it interpret the backspaces correctly?" Well, it got it right, but that may be a function of my terminal's settings. The points there are, you can be totally simple like that example; however you ought to test and see if you can detect and adjust, correct, or warn the user if they do something wrong. Like, typing beyond your limits, you'll likely take only 256 characters here, so let them know they exceeded your max, or consider a higher max.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] cannot redirect output to file please help figure20012 Linux - Newbie 9 07-22-2012 07:09 PM
output redirect back to file mathfeel Linux - Software 1 03-21-2007 12:28 PM
How to redirect output to a file? mus1402 Linux - Newbie 2 02-05-2006 09:42 AM
redirect screen output to file timbuck Linux - Software 5 12-09-2005 06:57 PM
how to redirect the soundcard output to a file? jr0 Linux - General 2 10-31-2005 06:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration