Sep 1, 2012 at 5:17am UTC
Hi
so this is what i would like to do is use this function OpenTextInputFileAndOpenTextOutputFile to open text files
but i get am error in man for key, argc, argv[] in my functions that i an calling, also do need char*argv[] in my main function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
int OpenTextInputFileAndOpenTextOutputFile(int argc, char * argv[] )
{
if ( argc < 4 )
{
cerr << "Arguments missing" << endl;
cerr << "Usage: ps1 infile outfile" << endl;
return 1; // program failed
}
int Key = atoi(argv[3]);
ifstream InPutFile; // declare an input file variable (object)
InPutFile.open( argv[1], ifstream::binary ); // open an input text file
if ( !InPutFile.good() ) // 2
{
// operation failed
cerr << "Cannot open input file " << argv[1] << endl;
return 2; // program failed (input)
}
ofstream OutPutFile; // declare an output file variable (object)
OutPutFile.open( argv[2], ofstream::binary); // open an output text file
if ( !OutPutFile.good() )
{
// operation failed
cerr << "Cannot open output file " << argv[2] << endl;
InPutFile.close(); // never forget, we must close input file
return 3; // program failed (output)
}
return Key;
}
int main(ifstream& InPutFile, ofstream& OutPutFile, char * argv[])
{
OpenTextInputFileAndOpenTextOutputFile(argc, argv[]);
// data processing
DecodesAndOutPutsToFile(InPutFile, OutPutFile, Key);
ProductionMaker(OutPutFile);
// close all file streams
InPutFile.close();
OutPutFile.close();
return 0;
}
Last edited on Sep 1, 2012 at 7:37am UTC
Sep 1, 2012 at 6:44am UTC
Hi ! Code tags plsssss :)
I don't see 'argc' declared anywhere...
Sep 1, 2012 at 7:16am UTC
this is what i had that worked
how do you print this cod in that nice box as others do.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
int main( int argc, char * argv[] )
{
if ( argc < 4 )
{
cerr << "Arguments missing" << endl;
cerr << "Usage: ps1 infile outfile" << endl;
return 1; // program failed
}
int Key = atoi(argv[3]);
ifstream InPutFile; // declare an input file variable (object)
InPutFile.open( argv[1], ifstream::binary ); // open an input text file
if ( !InPutFile.good() ) // 2
{
// operation failed
cerr << "Cannot open input file " << argv[1] << endl;
return 2; // program failed (input)
}
ofstream OutPutFile; // declare an output file variable (object)
OutPutFile.open( argv[2], ofstream::binary); // open an output text file
if ( !OutPutFile.good() )
{
// operation failed
cerr << "Cannot open output file " << argv[2] << endl;
InPutFile.close(); // never forget, we must close input file
return 3; // program failed (output)
}
// data processing
DecodesAndOutPutsToFile(InPutFile, OutPutFile, Key);
ProductionMaker(OutPutFile);
// close all file streams
InPutFile.close();
OutPutFile.close();
return 0;
}
Last edited on Sep 1, 2012 at 7:37am UTC
Sep 1, 2012 at 7:19am UTC
hi Blackseep
dose this not declare the key
int Key = atoi(argv[3]);
Last edited on Sep 1, 2012 at 7:21am UTC
Sep 1, 2012 at 7:22am UTC
Yeah, this should work, you're declaring all your variables and handle the args to main() correctly.
The only thing is that I believe fstreams are non-copyable, so DecodesAndOutputsToFile must take them by reference.
To use codeboxes, either use the <> button on the right of the input box, or do this:
EDIT: That declaration is only inside the scope of the " OpenTextInputFileAndOpenTextOutputFile" function, so main() won't have access to it. Also, the function won't have access to argv, since this is inside main()'s scope. You should go over this:
http://www.cplusplus.com/doc/tutorial/functions/
Also, get used to using concise function names. Holy crap.
Last edited on Sep 1, 2012 at 7:26am UTC