Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

running c++ program from unix command line with input and output files

Status
Not open for further replies.

tem80porary

Programmer
Jan 29, 2005
1
US
i have a project in which i need to run a c++ program from the command line in unix along with an input file and an output file. such as :

histogram1 < scores.txt > histogramOutput.txt

i want to use this line in unix, thus running a c++ file called histogram1 that will read in numbers from scores.txt and put all my output in histogramOutput.txt.

as far as i know, using this command runs the program, and anything i cout is put into histogramOutput.txt, but i am unable/unsure how to get anything from scores.txt.

if i was able to use the this command in unix :

histogram1 scores.txt histogramOutput.txt

then my problems would be over cause i could just get the names for the files from argc in main. but unfortunately i am not allowed to do that. if anyone can shed any light on this for me, i will be eternally grateful...

~tem80~
 
I do not understand why you aren't allowed to use your command line "histogram1 scores.txt histogramOutput.txt", at least AIX allows such a command line.

Two solutions occur to me:
1. Change your c++ program to read scores from stdin [use gets function]. The command you gave would redirect scores.txt to stdin.
2. write a shell script to invoke your program:
#!/usr/bin/ksh
#scoretests - display test scores a histogram
# parameters:
# score_file - name test score file
# result_file - name of output data set
## next line allows you to run graphing pgm of choice
pgmname='histogram1'
outfile=$2
rm $outfile #remove old output file if present
touch $outfile # create empty output file
$pgmname $1 >>$outfile
rc=$? # get return code from C++
exit $rc

You command would be:
scoretests scores.txt histogram.txt
(be sure to give the shell script the executable attribute)
 
I am not sure why you can't use command line arguements. scores.txt would be argv[1] and histogramOutput.txt would be argv[2] then just

FILE infile; //for reading

infile = fopen( argv[1], "r" );
fscanf( infile, (format) );

FILE outfile; //for writing

outfile = fopen( argv[2], "w+" );
fprintf( outfile, (format) );

Doing it with the redirects I think you could fscanf(stdin , (format) )
 
< is a redirect... This means that whatever follows is fed to STDIN like it was typed on the command line.

> is a redirect of output.... This means that what ever follows will catch all output that would normally go to the console.

"but unfortunately i am not allowed to do that"
Why are you not allowed from doing that?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top