LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-18-2012, 10:09 AM   #1
emaritza
LQ Newbie
 
Registered: Sep 2010
Posts: 11

Rep: Reputation: 0
directing output from an executable program into a specific folder


I am running an executable program that produces "output" and sends it to the "Current Directory" at the end of the program.

I would like the output to go into another folder that exists within the "Current Directory."

I've tried:

sprintf("/output > %s", outfile;

Where "output" is the folder and "outfile" is the product produced by the executable program.

This causes a segmentation fault.

What should I do?
 
Old 07-18-2012, 10:18 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,110

Rep: Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268
I assume it is written in c, but not really sure. Anyway, the line you wrote is incorrect.
Please give us more info: what language is it, how is the output currently stored. Probably you only need to change the filename to dir/filename.
 
Old 07-18-2012, 11:46 AM   #3
dmdeb
Member
 
Registered: Jul 2007
Location: Germany
Distribution: Debian
Posts: 45

Rep: Reputation: 6
Quote:
Originally Posted by emaritza View Post
I am running an executable program that produces "output" and sends it to the "Current Directory" at the end of the program.

I would like the output to go into another folder that exists within the "Current Directory."

I've tried:

sprintf("/output > %s", outfile;

Where "output" is the folder and "outfile" is the product produced by the executable program.

This causes a segmentation fault.

What should I do?
Hi emaritza,

first of all, have a look at "man sprintf" (http://linux.die.net/man/3/sprintf). You're actually trying to print into a string constant there, namely into "/output > %s"! If you asked me to do that, I'd seg-fault as well.

In general, you might want to add "-Wall -Werror -pedantic" to your compiler options (I assume you're using gcc) to be warned about similar type errors in the future.

Secondly, I don't really get your question. What is "output"? Is it a string containing ASCII output you want to save in a text file by the name stored in "outfile"? Or is "output" a program you want to run in order to redirect its output somewhere? And how does one "send output to a directory"? Or is "output" the name of a file that you want to copy to some directory? I'm lost!

If - at the end of the day - you're just trying to write strings into a newly created text file, you should have a look at fopen (http://linux.die.net/man/3/fopen), fprintf, and fclose. In all other cases: Can you explain what type of data you want to store where, in which format?

-----------
EDIT: Oh man, maybe I should have read the subject. Sorry for that. You're trying to run some program from inside your program and to redirect its output the way you know it from shell commands, right? You might try system (http://linux.die.net/man/3/system). Did you try to use sprintf() in order to construct the argument for the call to system?

Quick shot:

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char sbuf[128];
char *outfile = "subdir/destination";
int ret;

ret = snprintf( sbuf, sizeof(sbuf), "./output > %s", outfile );
assert( ret < sizeof(sbuf) ); /* String fits into sbuf. */

ret = system( sbuf );
printf( "system(\"%s\") returned %d\n", sbuf, ret );

return EXIT_SUCCESS;
}
-----------

Kind regards
Daniel

Last edited by dmdeb; 07-18-2012 at 12:05 PM.
 
Old 07-18-2012, 12:31 PM   #4
emaritza
LQ Newbie
 
Registered: Sep 2010
Posts: 11

Original Poster
Rep: Reputation: 0
I'm sorry my question was so vague.

The program is written in C and output is a *.log file.

After the program executes the *.log file is stored in the current directory.
 
Old 07-18-2012, 12:54 PM   #5
dmdeb
Member
 
Registered: Jul 2007
Location: Germany
Distribution: Debian
Posts: 45

Rep: Reputation: 6
Quote:
Originally Posted by emaritza View Post
I'm sorry my question was so vague.

The program is written in C and output is a *.log file.

After the program executes the *.log file is stored in the current directory.
Yikes - I thought I had guessed what you asked for (see EDIT above), but now I'm pretty sure I did not.

Since we're talking about two programs, the phrase "The program" is ambiguous.

What exactly are you trying to do? Can we give names to the two programs?

My current guess is this:
(1) You have a foreign program, logger, that produces log files in the current directory.
(2) You want to write your own program, mover, that moves these log files to an existing subdirectory.

Do you want to run both programs in parallel? Or do you want to call mover after logger so mover can move the log files produced by logger? Or do you want mover to call logger and then move the log files? You see, there are still many possible interpretations...
 
Old 07-18-2012, 01:05 PM   #6
emaritza
LQ Newbie
 
Registered: Sep 2010
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by dmdeb View Post
Yikes - I thought I had guessed what you asked for (see EDIT above), but now I'm pretty sure I did not.

Since we're talking about two programs, the phrase "The program" is ambiguous.

What exactly are you trying to do? Can we give names to the two programs?

My current guess is this:
(1) You have a foreign program, logger, that produces log files in the current directory.
(2) You want to write your own program, mover, that moves these log files to an existing subdirectory.

Do you want to run both programs in parallel? Or do you want to call mover after logger so mover can move the log files produced by logger? Or do you want mover to call logger and then move the log files? You see, there are still many possible interpretations...
The program I have is WorkQueue. It takes a number of files compresses them into *.gz files and stores them on the local directory.

I'm using WorkQueue to execute another program which generates *.log files and stores them on the local directory. I just want to store them in a folder that is on the local directory.
 
Old 07-18-2012, 01:12 PM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,110

Rep: Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268
probably you can configure your "another program" to specify the location of the log files
 
Old 07-18-2012, 05:08 PM   #8
emaritza
LQ Newbie
 
Registered: Sep 2010
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pan64 View Post
probably you can configure your "another program" to specify the location of the log files
Sadly, in this case I cannot.

Any and all other suggestions are most welcome.
 
Old 07-19-2012, 01:24 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,110

Rep: Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268
try to create symbolic link before executing the program, something like this:
logfilename -> real/dir/logfilename.
probably your app will not remove the link and will write into the right file...
 
Old 07-19-2012, 09:14 AM   #10
emaritza
LQ Newbie
 
Registered: Sep 2010
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pan64 View Post
try to create symbolic link before executing the program, something like this:
logfilename -> real/dir/logfilename.
probably your app will not remove the link and will write into the right file...
I don't recognize "real/dir." Is that equivalent to the path where my folder is?
 
Old 07-19-2012, 09:19 AM   #11
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Say your program writes to "outfile.log", and you want it to go to "logfiles/outfile.log". Before running your program you would run "ln -s logfiles/outfile.log outfile.log". This will create a symbolic link that lets the program write to its normal location, without realizing that it has actually been redirected elsewhere. That's assuming the program doesn't remove the file first before writing to it, which would break this little workaround.

Last edited by suicidaleggroll; 07-19-2012 at 09:20 AM.
 
Old 07-19-2012, 09:51 AM   #12
emaritza
LQ Newbie
 
Registered: Sep 2010
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by suicidaleggroll View Post
Say your program writes to "outfile.log", and you want it to go to "logfiles/outfile.log". Before running your program you would run "ln -s logfiles/outfile.log outfile.log". This will create a symbolic link that lets the program write to its normal location, without realizing that it has actually been redirected elsewhere. That's assuming the program doesn't remove the file first before writing to it, which would break this little workaround.
Thank you so much for the info! I shall try to implement it right away and let you know what happens!
 
Old 07-19-2012, 10:53 AM   #13
emaritza
LQ Newbie
 
Registered: Sep 2010
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by suicidaleggroll View Post
Say your program writes to "outfile.log", and you want it to go to "logfiles/outfile.log". Before running your program you would run "ln -s logfiles/outfile.log outfile.log". This will create a symbolic link that lets the program write to its normal location, without realizing that it has actually been redirected elsewhere. That's assuming the program doesn't remove the file first before writing to it, which would break this little workaround.
It didn't work. The program put the logfile into the current directory as usual.

Here's how I wrote out your recommendation(output is my folder name):

ln -s output/outfile.log outfile.log

Here's how I wrote my command line:

./exe outfile > outfile.log

Did I do something wrong?

Last edited by emaritza; 07-19-2012 at 10:55 AM.
 
Old 07-19-2012, 01:24 PM   #14
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,110

Rep: Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268
? you would need to know the syntax of your exe. In the case you wrote in your example you ought to write:
./exe outfile > logfiles/outfile.log
But I have no any clue what does that outfile really do
 
1 members found this post helpful.
Old 07-19-2012, 01:39 PM   #15
emaritza
LQ Newbie
 
Registered: Sep 2010
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pan64 View Post
? you would need to know the syntax of your exe. In the case you wrote in your example you ought to write:
./exe outfile > logfiles/outfile.log
But I have no any clue what does that outfile really do
I rewrote the command line as you recommended and it worked!

outfile is a configuration file that that is "processed" and generates several other files. Unfortunately the command that controls where the log file is printed is not inside the configuration file. My life would be easier if it were.

Thank you for your help. You have no idea how MUCH it is appreciated! It really is!

Last edited by emaritza; 07-19-2012 at 01:53 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Directing Script output to Memory but without using a pipe | telecom_is_me Programming 4 06-29-2008 01:17 AM
Directing sound output to microphone Night Ink Linux - Software 2 12-28-2007 01:52 AM
directing apache server to a folder klmbrt Linux - Networking 7 01-25-2006 08:55 PM
Directing xosd output to X display from crontab BWebb Linux - General 3 06-30-2005 02:09 PM


All times are GMT -5. The time now is 02:53 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration