PDA

Visualizza la versione completa : Dimensione file in lettura


GullMc
29-04-2006, 00:37
Salve,
a me servirebbe sapere a priori la grandezza di un file che apro in lettura. Io so che esiste a questo scopo la funzione gcount(), ma questa restituisce la grandezza del file dopo che lo ha scorso tutto. Come faccio a saperlo senza dover fare una prima lettura?

grazie

ibykos
01-05-2006, 16:20
Unix/Linux o windows?

per Unix/linux puoi usare qusta funzione:



int stat (const char *filename, struct stat *buf)

The stat function returns information about the attributes of the file named by filename in the structure pointed to by buf.
If filename is the name of a symbolic link, the attributes you get describe the file that the link points to. If the link points to a nonexistent file name, then stat fails reporting a nonexistent file.

The return value is 0 if the operation is successful, or -1 on failure. In addition to the usual file name errors (see File Name Errors, the following errno error conditions are defined for this function:

ENOENT
The file named by filename doesn't exist.
When the sources are compiled with _FILE_OFFSET_BITS == 64 this function is in fact stat64 since the LFS interface transparently replaces the normal implementation.



la quale salva nella struct stat che gli passi le informazioni relative al file


struct stat
The stat structure type is used to return information about the attributes of a file. It contains at least the following members:
mode_t st_mode
Specifies the mode of the file. This includes file type information (see Testing File Type) and the file permission bits (see Permission Bits).

ino_t st_ino
The file serial number, which distinguishes this file from all other files on the same device.

dev_t st_dev
Identifies the device containing the file. The st_ino and st_dev, taken together, uniquely identify the file. The st_dev value is not necessarily consistent across reboots or system crashes, however.

nlink_t st_nlink
The number of hard links to the file. This count keeps track of how many directories have entries for this file. If the count is ever decremented to zero, then the file itself is discarded as soon as no process still holds it open. Symbolic links are not counted in the total.

uid_t st_uid
The user ID of the file's owner. See File Owner.

gid_t st_gid
The group ID of the file. See File Owner.

off_t st_size
This specifies the size of a regular file in bytes. For files that are really devices this field isn't usually meaningful. For symbolic links this specifies the length of the file name the link refers to.

time_t st_atime
This is the last access time for the file. See File Times.

unsigned long int st_atime_usec
This is the fractional part of the last access time for the file. See File Times.

time_t st_mtime
This is the time of the last modification to the contents of the file. See File Times.

unsigned long int st_mtime_usec
This is the fractional part of the time of the last modification to the contents of the file. See File Times.

time_t st_ctime
This is the time of the last modification to the attributes of the file. See File Times.

unsigned long int st_ctime_usec
This is the fractional part of the time of the last modification to the attributes of the file. See File Times.

blkcnt_t st_blocks
This is the amount of disk space that the file occupies, measured in units of 512-byte blocks.
The number of disk blocks is not strictly proportional to the size of the file, for two reasons: the file system may use some blocks for internal record keeping; and the file may be sparse--it may have "holes" which contain zeros but do not actually take up space on the disk.

You can tell (approximately) whether a file is sparse by comparing this value with st_size, like this:

(st.st_blocks * 512 < st.st_size)

This test is not perfect because a file that is just slightly sparse might not be detected as sparse at all. For practical applications, this is not a problem.


unsigned int st_blksize
The optimal block size for reading of writing this file, in bytes. You might use this size for allocating the buffer space for reading of writing the file. (This is unrelated to st_blocks.)


The extensions for the Large File Support (LFS) require, even on 32-bit machines, types which can handle file sizes up to 2^63. Therefore a new definition of struct stat is necessary.

king64
01-05-2006, 21:33
#include <stdio.h>
#include <io.h>
#include <fcntl.h>

void main()
{
char filename[30];
int handle;
long int size;

printf("Inserisci il nome del file: ");
scanf("%s",filename);
handle=_open(filename,_O_RDONLY);
size=_filelength(handle);
printf("%d\n",size);
}

Loading