I had a program which accepted input file on the command line and redirected output to file specified in the command line. So it was essentially
Code:$ ./name_of_program <input_file >output_fileNow I made some modifications to this program to get it working with Java Native Interface. Also I used freopen to redirect stdin and stdout to the files. One other change that I made was that my program made use of getopt() function to parse the command line arguments. Since now I am not running it on the command line I put the calculations that were being done before the getopt() part. So my modified code looks like this.Code:void main(int Argc, char *Argv[]) /* ---- */ { // Initialise the variables /* Process options */ while ( (o = getopt(Argc, Argv, "nNus:a:f:g:V:d:A:w:l:t:m:v:")) != EOF ) { if ( FirstTime ) { printf("\n Options:\n"); FirstTime = false; } switch (o) { case 'n': // Do some calculations break; case 'N': //Do some calculations break; } }
I got my executable and ran the program with just one input and output file and it ran smoothly. But when I ran it with two input and output files it crashes giving me a Segmentation Fault. If i put printf after I dont see the output being redirected to my output file.Code:void enter() { freopen("inputfile","r", stdin); freopen("outputfile","w", stdout); enter(); fclose(stdin); fclose(stdout); // Run again with a different input and output file freopen("inputfile","r", stdin); freopen("outputfile","w", stdout); enter(); fclose(stdin); fclose(stdout); } void main() /* ---- */ { // Initialise the variables // Do some calculations // Do some calculations /* Process options */ while ( (o = getopt(Argc, Argv, "nNus:a:f:g:V:d:A:w:l:t:m:v:")) != EOF ) { if ( FirstTime ) { printf("\n Options:\n"); FirstTime = false; } switch (o) { case 'n': // Do some calculations break; case 'N': //Do some calculations break; } }
Any clues on if there is an issue with using freopen multiple times.Code:// Run again with a different input and output file freopen("inputfile","r", stdin); freopen("outputfile","w", stdout); printf("\nI am here");



LinkBack URL
About LinkBacks



I used to be an adventurer like you... then I took an arrow to the knee.
