LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   directing output from an executable program into a specific folder (https://www.linuxquestions.org/questions/linux-newbie-8/directing-output-from-an-executable-program-into-a-specific-folder-4175417390/)

emaritza 07-18-2012 09:09 AM

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?

pan64 07-18-2012 09:18 AM

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.

dmdeb 07-18-2012 10:46 AM

Quote:

Originally Posted by emaritza (Post 4731683)
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

emaritza 07-18-2012 11:31 AM

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.

dmdeb 07-18-2012 11:54 AM

Quote:

Originally Posted by emaritza (Post 4731807)
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...

emaritza 07-18-2012 12:05 PM

Quote:

Originally Posted by dmdeb (Post 4731827)
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.

pan64 07-18-2012 12:12 PM

probably you can configure your "another program" to specify the location of the log files

emaritza 07-18-2012 04:08 PM

Quote:

Originally Posted by pan64 (Post 4731843)
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.

pan64 07-19-2012 12:24 AM

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

emaritza 07-19-2012 08:14 AM

Quote:

Originally Posted by pan64 (Post 4732289)
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?

suicidaleggroll 07-19-2012 08:19 AM

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.

emaritza 07-19-2012 08:51 AM

Quote:

Originally Posted by suicidaleggroll (Post 4732675)
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!

emaritza 07-19-2012 09:53 AM

Quote:

Originally Posted by suicidaleggroll (Post 4732675)
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?

pan64 07-19-2012 12:24 PM

? 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

emaritza 07-19-2012 12:39 PM

Quote:

Originally Posted by pan64 (Post 4732890)
? 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!


All times are GMT -5. The time now is 10:35 PM.