Comment rediriger l'entrée ou la sortie standard ?
Bonjour à tous,
nouveau en C++ j'aimerais me servir d'une idée de la FAQ pour rediriger l'entrée/sortie standard 'cin'/'cout'. J'ai donc repris ce qui se trouve dans la FAQ ( http://cpp.developpez.com/faq/cpp/?p...SL_redirection) pour définir à partir de la console les fichiers entrée/sortie. J'obtiens alors une erreur à la compilation que je ne comprends pas.
Voilà pour le bout de code incriminé, avec pour exemple le même code que dans la FAQ pour la sortie 'cout':
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
int CommandShell::startShell(int argc, char* argv[]){
// Reading the options
int fileopt;
int EOO = -1;
while((fileopt = getopt(argc, argv, "i:o:")) != EOO) {
switch(fileopt) {
case 'i':
// the commands are read from a defined file
// redirecting the std input
break;
case 'o':
// the commands are written into a defined file
// redirecting the std output
std::ofstream Out("Toto.txt");
std::streambuf* OldBuf = std::cout.rdbuf(Out.rdbuf());
break;
default:
usage(argv[0]);
break;
}
}
} |
et l'erreur incriminée:
Citation:
Envoyé par g++
..\CommandShell.cpp: In member function 'int libforefire::CommandShell::startShell(int, char**)':
..\CommandShell.cpp:48: error: jump to case label
..\CommandShell.cpp:43: error: crosses initialization of 'std::streambuf* OldBuf'
..\CommandShell.cpp:42: error: crosses initialization of 'std::ofstream Out'
..\CommandShell.cpp:43: warning: unused variable 'OldBuf'
Build error occurred, build is stopped
Merci pour votre aide !!