Tuesday, 15 April 2014

C++ start programm with output file -



C++ start programm with < input file and > output file -

i need know how work in c++ unix parameter < , > told me if run programme ./program < input file > output file programme read file declared behind < , write out file declared after >

actual code looks this

int main(int argc, char** argv) {

file* filein = fopen(argv[1], "r"); file* fileout = fopen(argv[2], "w");

...

so want set files wich declare @ programme start handle here because going utilize filein , fileoutlater in program.

i'm allowed work stdio.h please maintain basic.

thanks in advance

this shell cmd syntax

./program < input_file > output_file

doesn't have parameters passed main

int main(int argc, char** argv) {

you can refer std::cin , std::cout mentioned input , output files.

it's shell feature , called standard input/output stream redirection. if want pass parameters programme via argv you'll utilize next syntax

./program -x --opt1 < input_file > output_file

as state

"i'm allowed work stdio.h please maintain basic."

in case can utilize predefined stdin/stdout macros.

file* filein = stdin; file* fileout = stdout;

this should utilize default. if want have additional feature, user specifies particular input/output file names programme arguments, should check command line parameters passed main:

int main(int argc, char* argv[]) { file* filein = stdin; file* fileout = stdout; if(argc > 1) { filein = fopen(argv[1], "r"); } if(argc > 2) { fileout = fopen(argv[2], "w"); } }

by default programme reads standard input , writes standard output, if done above. calling programme without parameters, leaves prompt user input something.

this preferred, , flexible style implementing console programs, should process streamed input , transform (process) output.

c++ file input command-line output

No comments:

Post a Comment