_Leo Site Admin

Registrado: 11 Ago 2002Mensajes: 1875Ubicación: Lanzarote - Islas Canarias (España)
|
Publicado: Dom 08 Dic, 2002 10:26 pm Asunto: |
|
|
Varias maneras de hacerlo:
| Código: | ///////////////////////////////////////////////////////////
// Estilo C++ - Añadimos: #include<fstream>
int TamanyoArchivo(const String& path)
{
int tamanyo = -1;
std::ifstream fichero(path.c_str());
if (fichero)
{
fichero.seekg(0, std::ios::end);
tamanyo = fichero.tellg();
fichero.close();
}
return tamanyo;
}
///////////////////////////////////////////////////////////
// Estilo C - Añadimos: #include<stdio.h>
// (tambien io.h en algunas versiones)
int TamanyoArchivo(const String& path)
{
int tamanyo = -1;
FILE *fp = fopen(path.c_str(), "rb");
if (fp)
{
fseek(fp, 0, SEEK_END);
tamanyo = ftell(fp);
fclose(fp);
}
return tamanyo;
}
///////////////////////////////////////////////////////////
// Estilo VCL
int TamanyoArchivo(const String& path)
{
int tamanyo;
TFileStream *fs = NULL;
try {
fs = new TFileStream(path, fmOpenRead);
tamanyo = fs->Size;
delete fs;
}
catch(EFOpenError& e) {
tamanyo = -1;
}
return tamanyo;
}
///////////////////////////////////////////////////////////
// Estilo API
int TamanyoArchivo(const String& path)
{
int tamanyo = -1;
HANDLE hFile = CreateFile(path.c_str(),
GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
tamanyo = GetFileSize(hFile, NULL);
CloseHandle(hFile);
}
return tamanyo;
} |
Y para probarlo. Por ejemplo:
Caption = TamanyoArchivo("c:\\autoexec.bat");
Devuelve -1 si el archivo no existe. |
|