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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
07-18-2012, 10:09 AM
|
#1
|
LQ Newbie
Registered: Sep 2010
Posts: 11
Rep:
|
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?
|
|
|
07-18-2012, 10:18 AM
|
#2
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,637
|
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.
|
|
|
07-18-2012, 11:46 AM
|
#3
|
Member
Registered: Jul 2007
Location: Germany
Distribution: Debian
Posts: 45
Rep:
|
Quote:
Originally Posted by emaritza
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.
|
|
|
07-18-2012, 12:31 PM
|
#4
|
LQ Newbie
Registered: Sep 2010
Posts: 11
Original Poster
Rep:
|
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.
|
|
|
07-18-2012, 12:54 PM
|
#5
|
Member
Registered: Jul 2007
Location: Germany
Distribution: Debian
Posts: 45
Rep:
|
Quote:
Originally Posted by emaritza
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...
|
|
|
07-18-2012, 01:05 PM
|
#6
|
LQ Newbie
Registered: Sep 2010
Posts: 11
Original Poster
Rep:
|
Quote:
Originally Posted by dmdeb
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.
|
|
|
07-18-2012, 01:12 PM
|
#7
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,637
|
probably you can configure your "another program" to specify the location of the log files
|
|
|
07-18-2012, 05:08 PM
|
#8
|
LQ Newbie
Registered: Sep 2010
Posts: 11
Original Poster
Rep:
|
Quote:
Originally Posted by pan64
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.
|
|
|
07-19-2012, 01:24 AM
|
#9
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,637
|
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...
|
|
|
07-19-2012, 09:14 AM
|
#10
|
LQ Newbie
Registered: Sep 2010
Posts: 11
Original Poster
Rep:
|
Quote:
Originally Posted by pan64
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?
|
|
|
07-19-2012, 09:19 AM
|
#11
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
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.
|
|
|
07-19-2012, 09:51 AM
|
#12
|
LQ Newbie
Registered: Sep 2010
Posts: 11
Original Poster
Rep:
|
Quote:
Originally Posted by suicidaleggroll
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!
|
|
|
07-19-2012, 10:53 AM
|
#13
|
LQ Newbie
Registered: Sep 2010
Posts: 11
Original Poster
Rep:
|
Quote:
Originally Posted by suicidaleggroll
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.
|
|
|
07-19-2012, 01:24 PM
|
#14
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,637
|
? 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.
|
07-19-2012, 01:39 PM
|
#15
|
LQ Newbie
Registered: Sep 2010
Posts: 11
Original Poster
Rep:
|
Quote:
Originally Posted by pan64
? 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.
|
|
|
All times are GMT -5. The time now is 08:03 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|