vendettaaaaa
21-01-2012, 22:54
Stavo pensando ai vari modi per controllare se un file è vuoto, cioè non ci sono caratteri oppure ci sono solo whitespace (mi limito a spazio, newline, tab).
Ne sono venuto fuori con questo:
// Check for empty file
char c;
while (fileIn.get(c)) {
// If c is not a whitespace, go back 1 char and quit the loop
if (c != ' ' && c != '\n' && c != '\t') {
fileIn.unget();
break;
}
// If so far there were only whitespaces, and peek() generates eof() == 1, file is empty
fileIn.peek();
if (fileIn.eof()) {
cout << endl << "The file is empty, you monkey!!" << endl;
return 0;
}
}
che sembra funzionare per file che contengono almeno un whitespace. Se il file invece è proprio vuoto, il loop while non parte perchè fileIn.get(c) fallisce. Dovrei quindi aggiungere un paio di righe:
// Check for empty file
char c;
while (fileIn.get(c)) {
// If c is not a whitespace, go back 1 char and quit the loop
if (c != ' ' && c != '\n' && c != '\t') {
fileIn.unget();
break;
}
// If so far there were only whitespaces, and peek() generates eof() == 1, file is empty
fileIn.peek();
if (fileIn.eof()) {
cout << endl << "The file is empty, you monkey!!" << endl;
return 0;
}
}
if (fileIn.eof()) {
cout << endl << "The file is empty, you monkey!!" << endl;
return 0;
}
Un altro modo sarebbe controllare prima del loop:
// Check for empty file
fileIn.peek();
if (fileIn.eof()) {
cout << endl << "The file is empty, you monkey!!" << endl;
return 0;
}
char c;
while (fileIn.get(c)) {
// If c is not a whitespace, go back 1 char and quit the loop
if (c != ' ' && c != '\n' && c != '\t') {
fileIn.unget();
break;
}
// If so far there were only whitespaces, and peek() generates eof() == 1, file is empty
fileIn.peek();
if (fileIn.eof()) {
cout << endl << "The file is empty, you monkey!!" << endl;
return 0;
}
}
Ho sbagliato molto in passato perchè non sapevo che eof() è posto = 1 dalla precedente lettura del file, ma ora che lo so gli faccio precedere un peek() che, seppur non estrae alcun carattere, si accorge se la fine del file è arrivata.
A parte questo, che ne pensate di questo metodo? Ce ne sono di migliori (ed intendo più efficienti)? Questo (3° versione) mi pare carino: se il file è vuoto se ne accorge subito, se ci sono un sacco di spazi vuoti prima della prima lettera di testo li salta (il cursore del file torna indietro con unget() e da lì continua).
Ci sono bug che mi sfuggono?
Ne sono venuto fuori con questo:
// Check for empty file
char c;
while (fileIn.get(c)) {
// If c is not a whitespace, go back 1 char and quit the loop
if (c != ' ' && c != '\n' && c != '\t') {
fileIn.unget();
break;
}
// If so far there were only whitespaces, and peek() generates eof() == 1, file is empty
fileIn.peek();
if (fileIn.eof()) {
cout << endl << "The file is empty, you monkey!!" << endl;
return 0;
}
}
che sembra funzionare per file che contengono almeno un whitespace. Se il file invece è proprio vuoto, il loop while non parte perchè fileIn.get(c) fallisce. Dovrei quindi aggiungere un paio di righe:
// Check for empty file
char c;
while (fileIn.get(c)) {
// If c is not a whitespace, go back 1 char and quit the loop
if (c != ' ' && c != '\n' && c != '\t') {
fileIn.unget();
break;
}
// If so far there were only whitespaces, and peek() generates eof() == 1, file is empty
fileIn.peek();
if (fileIn.eof()) {
cout << endl << "The file is empty, you monkey!!" << endl;
return 0;
}
}
if (fileIn.eof()) {
cout << endl << "The file is empty, you monkey!!" << endl;
return 0;
}
Un altro modo sarebbe controllare prima del loop:
// Check for empty file
fileIn.peek();
if (fileIn.eof()) {
cout << endl << "The file is empty, you monkey!!" << endl;
return 0;
}
char c;
while (fileIn.get(c)) {
// If c is not a whitespace, go back 1 char and quit the loop
if (c != ' ' && c != '\n' && c != '\t') {
fileIn.unget();
break;
}
// If so far there were only whitespaces, and peek() generates eof() == 1, file is empty
fileIn.peek();
if (fileIn.eof()) {
cout << endl << "The file is empty, you monkey!!" << endl;
return 0;
}
}
Ho sbagliato molto in passato perchè non sapevo che eof() è posto = 1 dalla precedente lettura del file, ma ora che lo so gli faccio precedere un peek() che, seppur non estrae alcun carattere, si accorge se la fine del file è arrivata.
A parte questo, che ne pensate di questo metodo? Ce ne sono di migliori (ed intendo più efficienti)? Questo (3° versione) mi pare carino: se il file è vuoto se ne accorge subito, se ci sono un sacco di spazi vuoti prima della prima lettera di testo li salta (il cursore del file torna indietro con unget() e da lì continua).
Ci sono bug che mi sfuggono?