[C] Controle of binaire file wel inhoud bevat

Pagina: 1
Acties:

  • Neptunus
  • Registratie: Januari 2001
  • Laatst online: 16-05 13:27
Ik heb onderstaande code gemaakt. Werkt helemaal prima. Alleen als het bestand wel bestaat maar de inhoud ik helemaal leeg is gaat het niet goed. Is ook wel te begrijpen omdat met fseek(stream, 0 - sizeof(bak), SEEK_END); ik buiten het geheugen gebied kijk. Dit wil ik natuurlijk niet. Nu is mijn vraag, hoe kan je snel en simpel kijken of het bestand inhoud heeft. Beter gezegt een controle dat je vertelt of er wel of geen inhoud is.

C:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int HoeveelheidBakken(FILE *stream, char bestandNaam[], struct bakstruct bak) {
    int hoeveelheid = 0;
    stream = fopen(bestandNaam, "rb");
    if(stream != NULL){
        fseek(stream, 0 - sizeof(bak), SEEK_END);
        fread(&bak, sizeof(bak), 1, stream);
        hoeveelheid = bak.bakNummer;
        fclose(stream);
        printf("\nhoeveelheid: %d", hoeveelheid);
    } else {
        printf("Bestand bestaat niet!");
    }

    return hoeveelheid;
}

  • SWfreak
  • Registratie: Juni 2001
  • Niet online
Misschien helpt het om eens te kijken of fseek wel succesvol de file-pointer verplaatst heeft?
C:
1
2
if( fseek(...) != 0 )
  //error

Daarnaast kun je ook
C:
1
2
fseek(stream, 0, SEEK_END);
int fileSize = ftell(stream);

doen als ik het me goed herinner...

  • Neptunus
  • Registratie: Januari 2001
  • Laatst online: 16-05 13:27
SWfreak schreef op maandag 15 november 2004 @ 22:18:

C:
1
2
if( fseek(...) != 0 )
  //error


[/code]
Heb het op de volgende manier opgelost.

C:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int HoeveelheidBakken(FILE *stream, char bestandNaam[], struct bakstruct bak) {
    int hoeveelheid = 0, controle;
    stream = fopen(bestandNaam, "rb");
    controle = fseek(stream, 0 - sizeof(bak), SEEK_END);
    if(stream != NULL && controle == 0){
        fseek(stream, 0 - sizeof(bak), SEEK_END);
        fread(&bak, sizeof(bak), 1, stream);
        hoeveelheid = bak.bakNummer;
        fclose(stream);
    } else {
        printf("\nBestand bestaat niet of het bestand heeft geen inhoud!\n");
    }
    return hoeveelheid;
}


Documentatie van fseek zegt het volgende.

fseek
Deze functie verplaatst de wijzer die de positie aangeeft waar eerstvolgend gelezen of geschreven wordt.

Prototype:

int fseek(FILE *stream, long offset, int fromwhere);

offset is de nieuwe positie relatief ten opzichte van de positie gespecifieerd met fromwhere.

De functie geeft 0 bij succes of nonzero bij fout.

De parameter fromwhere kan een van de volgende waarden zijn:

SEEK_SET verplaats vanaf het begin van de file

SEEK_CUR verplaats vanaf de huidige positie

SEEK_END verplaats vanaf het einde van de file

Heb dus gebruikt gemaakt van de int waarde die fseek terug geeft. Ik kijk gewoon of het 0 is. Zoja dan mag het programma verder, zoniet dan stopt het. Is dus eigenlijk wat SWfreak zei.

[ Voor 9% gewijzigd door Neptunus op 15-11-2004 22:30 ]


Verwijderd

kun je de file size (met stat) gewoon niet opvragen ipv dat seek gedoe?

  • 4VAlien
  • Registratie: November 2000
  • Laatst online: 08-04 20:02

4VAlien

Intarweb!

NAME
stat, fstat, lstat - get file status

SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int stat(const char *file_name, struct stat *buf);
int fstat(int filedes, struct stat *buf);
int lstat(const char *file_name, struct stat *buf);

DESCRIPTION
These functions return information about the specified file. You do
not need any access rights to the file to get this information but you
need search rights to all directories named in the path leading to the
file.

stat stats the file pointed to by file_name and fills in buf.

lstat is identical to stat, except in the case of a symbolic link,
where the link itself is stat-ed, not the file that it refers to.

fstat is identical to stat, only the open file pointed to by filedes
(as returned by open(2)) is stat-ed in place of file_name.

They all return a stat structure, which contains the following fields:

struct stat {
dev_t st_dev; /* device */
ino_t st_ino; /* inode */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device type (if inode device) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for filesystem I/O */
blkcnt_t st_blocks; /* number of blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last change */
};

  • MSalters
  • Registratie: Juni 2001
  • Laatst online: 09-04 22:08
stat is geen C, maar POSIX.

Man hopes. Genius creates. Ralph Waldo Emerson
Never worry about theory as long as the machinery does what it's supposed to do. R. A. Heinlein

Pagina: 1