Visualizzazione dei risultati da 1 a 6 su 6
  1. #1

    [C++] Dimensione di un file binario

    Ciao a tutti,
    sto facendo un esercizio per scuola in cui mi si chiede di aprire un file binario contenente dei dati di temperatura. Il primo esercizio mi chiede di trovare quante coppie ci sono (data e valore della temperatura) calcolando il valore in base alle dimensioni del file e alla dimensione del campo data(un int) e temperatura (un float).

    Il mio problema è il seguente: come cavolo faccio a trovare la dimensione del file? Grazie
    http://www.newsol.ch
    "I'm so sorry"
    Mario Corti, CEO Swissair, 2 Ottobre 2001
    La crisi della società Swissair è culminata con il "grounding" dell’intera flotta aerea nei primi giorni del mese di ottobre 2001.

  2. #2
    Utente di HTML.it
    Registrato dal
    Feb 2003
    Messaggi
    698
    uhm

    in c mi pare che usavo un archibugio tipo questo

    codice:
    FILE *f = fopen("unfile.ext","rb");
    
    long sz;
    fseek(f,0,SEEK_END);
    sz = ftell(f);
    
    printf("dimensione = %ld \n",sz);
    
    fclose(f);
    
    return 0;
    in pratica ti posizioni all'ultimo byte e poi leggi a quale posizione è il puntatore di lettura. ovviamente l'unità di misura in questo caso è il byte

  3. #3
    Moderatore di Sicurezza informatica e virus L'avatar di Habanero
    Registrato dal
    Jun 2001
    Messaggi
    9,782
    Usa la funzione lseek

    dimensione=lseek(fd,0,SEEK_END);

    ricordati che se poi vuoi cominciare a leggere il file dall'inizio devi riportare l'I/O pointer all'inizio del file con

    lseek(fd,0,SEEK_SET);


    Edit:
    preceduto...

    ovvio che la lseek rispetto a fseek usa i file descritor e quindi nel mio esempio devi aprire il file con Open e non con Fopen...
    Probabilmente per il tuo scopo è meglio la soluzione di Gil Mour
    Leggi il REGOLAMENTO!

    E' molto complicato, un mucchio di input e output, una quantità di informazioni, un mucchio di elementi da considerare, ho una quantità di elementi da tener presente...
    Drugo

  4. #4
    Grazie mille a entrambi. Ma quindi con fopen nn uso fstream? O si?
    Perché da quanto ho letto fstream ha i suoi attributi open e close leegati a un oggetto.
    http://www.newsol.ch
    "I'm so sorry"
    Mario Corti, CEO Swissair, 2 Ottobre 2001
    La crisi della società Swissair è culminata con il "grounding" dell’intera flotta aerea nei primi giorni del mese di ottobre 2001.

  5. #5
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,462
    Quelli che hai visto sono metodi del C.

    In C++ puoi fare

    #include <fstream>

    using namespace std;

    void main()
    {
    ifstream file ("file.dat", ios::in | ios::binary | ios::ate);

    ifstream:os_type size = file.tellg();

    cout << size << endl;
    }

  6. #6
    Utente di HTML.it L'avatar di ibykos
    Registrato dal
    Feb 2005
    Messaggi
    201
    in usix esiste anche la funzione fstat

    codice:
    #include <sys/stat.h>
    
    int fstat(int fildes, struct stat *buf);
    Documentazione:

    The fstat() function shall obtain information about an open file associated with the file descriptor fildes, and shall write it to the area pointed to by buf.

    [SHM] If fildes references a shared memory object, the implementation shall update in the stat structure pointed to by the buf argument only the st_uid, st_gid, st_size, and st_mode fields, and only the S_IRUSR, S_IWUSR, S_IRGRP, S_IWGRP, S_IROTH, and S_IWOTH file permission bits need be valid. The implementation may update other fields and flags.

    [TYM] If fildes references a typed memory object, the implementation shall update in the stat structure pointed to by the buf argument only the st_uid, st_gid, st_size, and st_mode fields, and only the S_IRUSR, S_IWUSR, S_IRGRP, S_IWGRP, S_IROTH, and S_IWOTH file permission bits need be valid. The implementation may update other fields and flags.

    The buf argument is a pointer to a stat structure, as defined in <sys/stat.h>, into which information is placed concerning the file.

    The structure members st_mode, st_ino, st_dev, st_uid, st_gid, st_atime, st_ctime, and st_mtime shall have meaningful values for all other file types defined in this volume of IEEE Std 1003.1-2001. The value of the member st_nlink shall be set to the number of links to the file.

    An implementation that provides additional or alternative file access control mechanisms may, under implementation-defined conditions, cause fstat() to fail.

    The fstat() function shall update any time-related fields as described in the Base Definitions volume of IEEE Std 1003.1-2001, Section 4.7, File Times Update, before writing into the stat structure.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.