|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Jan 2012
Messaggi: 1267
|
[C++] Controllare se un file è vuoto
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: Codice:
// 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;
}
}
Codice:
// 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;
}
Codice:
// 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;
}
}
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? |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Oct 2004
Messaggi: 1945
|
Se ne è parlato circa una settimanella fa.... controlli semplicemente la dimensione... 3 comandi, 3 righe di codice
1) fopen 2) fseek 3) ftell |
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Jan 2012
Messaggi: 1267
|
|
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Jan 2012
Messaggi: 1267
|
Uhm uhm, parli di questo esempio:
Codice:
#include <stdio.h>
int main ()
{
FILE * pFile;
long size;
pFile = fopen ("myfile.txt","rb");
if (pFile==NULL) perror ("Error opening file");
else
{
fseek (pFile, 0, SEEK_END);
size=ftell (pFile);
fclose (pFile);
printf ("Size of myfile.txt: %ld bytes.\n",size);
}
return 0;
}
|
|
|
|
|
|
#5 |
|
Senior Member
Iscritto dal: Oct 2004
Messaggi: 1945
|
a si ok adesso ho capito non avevo letto bene la tua richiesta... tu vuoi vedere se il file è vuoto oppure se non lo è se ha solo spazi whitespace, tabs, newline, ecc... allora misà che ti tocca leggere tutto il file
|
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Jan 2012
Messaggi: 1267
|
Eh infatti per quello chiedevo se ci fossero metodi più efficienti...anche se tutto sommato, a meno di file giganti direi che il metodo va già bene.
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 12:54.




















