Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

get file length

Name: Anonymous 2009-04-17 4:56

Whats the most crossplatform way to get file length in C?

Name: Anonymous 2009-04-17 5:01

fopen,fseek(0,2),ftell,fclose.

All others are os dependent.

Name: Anonymous 2009-04-17 5:02

By crossplatform you mean supporting made-up standards from lunix neckbeards?

Name: Anonymous 2009-04-17 5:23

By C you mean supporting made-up standards from lunix neckbeards?

Name: Anonymous 2009-04-17 5:34

I agree with >>2.

FILE *f;
long len;
f = fopen("file", "r");
fseek(f, 0, SEEK_END);
len = ftell(f);
fclose(f);

fstat() is POSIX and is more or less uniform across GNU/Linux, *BSD and Solaris (on Solaris struct elements are in wrong order but have standard names), however I'm not sure how it will behave on real operating systems like Windows 9x, Plan 9 and I-TRON.

Name: Anonymous 2009-04-17 5:38

>>5
>>2

Exactly. That library calls are standardized by C89.

Name: Anonymous 2009-04-17 5:53

>>6
That library calls
hehe

Name: Anonymous 2009-04-17 6:40

FILE *f;
long len;
f = fopen("file", "rb");
fseek(f, 0, SEEK_END);
len = ftell(f);
fclose(f);

Name: Anonymous 2009-04-17 9:07

>>8
b was deprecated aeons ago.

Name: Anonymous 2009-04-17 9:25

>>9
Just like COBOL?

Name: Anonymous 2009-04-17 9:44

>>5
Not portable. Files opened in text mode need not return byte offsets from ftell() (the values might be "seek descriptors", suitable for passing to fseek() but useless otherwise).

>>8
Not portable. Files opened in binary mode need not support SEEK_END.

size_t n = 0; while (fgetc(f) != EOF) ++n;

Name: Anonymous 2009-04-17 9:51

The most portable program would write a Java program to get the length of the file, compile and execute it, trap the output and then return it.

Name: Anonymous 2009-04-17 10:14

Be aware that stat is more portable than fstat.

Name: Anonymous 2009-04-17 10:26

>>11
Not portable, some C implementations don't define a magic FILE constant "f" that just happens to be the file the programmer wanted the length of.

Name: Anonymous 2009-04-17 10:50

>>11
Not portable.  Files opened in text mode need not read n characters from an n-byte file.

Name: Anonymous 2009-04-17 11:34

>>15
Really? C defines "byte" as the amount of memory needed to store a single character.

Name: Anonymous 2009-04-17 11:51

System.Unsafe.unsafeGetFileLength

Name: Anonymous 2009-04-17 12:04

>>16
Actually, C defines "char" as the amount of memory needed to store a single character.

Name: Anonymous 2009-04-17 12:10

>>16
What about \r\n translation?

Name: Anonymous 2009-04-17 12:27

Prelude> System.Unsafe.unsafeGetFileLength "test.hs"
Error: stack overflow

Name: Anonymous 2009-04-17 12:29

Prelude> :?
Error: stack overflow

Name: Anonymous 2009-04-17 14:53

Prelude>
Error: stack overflow

Name: Anonymous 2009-04-17 14:57

PrErErroeluder: ror: stacstack >overflowk overflow

Name: Anonymous 2009-04-17 15:06

>>21
I lol'd.

Name: Anonymous 2009-04-17 16:12

% ghci
GHCi, version 6.10.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ..
Error: stack overflow
%

Name: Anonymous 2009-04-17 16:31

Prelude> error "stack overflow"
*** Exception: stack overflow

Name: Anonymous 2009-04-17 16:40

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\James>ghci
GHCi, version 6.10.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
Prelude> in c major

<interactive>:1:0: parse error on input `in'

Name: Anonymous 2009-04-17 17:01

    Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.

    C:\Documents and Settings\James>ghci
    GHCi, version 6.10.1: http://www.haskell.org/ghc/  :? for help
    Loading package ghc-prim ... linking ... done.
    Loading package integer ... linking ... done.
    Loading package base ... linking ... done.
    Prelude> in c major
    *** Exception: stack overflow

Name: Anonymous 2009-04-17 17:05

>>16
There is at least one DSP I have worked with where CHAR_BIT is 32. The char types, short, int and long are all 32 bits." - From http://home.att.net/~jackklein/c/inttypes.html

Name: Anonymous 2009-04-17 18:06

Please stop making fun of haskell.

Name: Anonymous 2009-04-17 18:10

[0593][haxus:~] ghci
GHCi, version 6.8.2: http://www.haskell.org/ghc/  :ihbt for help
Loading package base ... linking ... done.
Prelude> :q
*** Exception: stack overflow

Name: Anonymous 2009-04-17 18:12

>>29
Which is perfectly legal. What isn't allowed is for CHAR_BIT to be larger than any other type (or put another way, no type can be smaller than "char"). If you think you need to care about portable fixed-width integers, even though you probably don't, use a C99 compiler.

Name: Anonymous 2009-04-17 18:34

Yet Another Haskell Troll

Name: Anonymous 2009-04-19 23:33

hover your mouse over it in windows explorer. It will show the file length and it works on all supported versions of windows.

What is lunix?

Name: Anonymous 2009-04-20 0:49

>>31
`--> ghci
:qGHCi, version 6.10.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
Prelude> :q
Leaving GHCi.

Name: Anonymous 2009-04-20 5:10


#include <stdio.h>
#include <stdlib.h>

int filesize(char *);

int main(int argc, char *argv[]) {
    int     s;
   
    if(argc < 2) {
        printf("%s <filnamn>\n", argv[0]);
        exit(-1);
    }

    if((s = filesize(argv[1])) != -1) {
        printf("%s: %d bytes\n", argv[1], s);
    } else {
        printf("Kunde inte \xf6ppna %s!\n", argv[1]);
    }

    exit(0);
}

int filesize(char *fil) {
    int         storlek;
    FILE         *fp;
    int             start, slut;
   
    if((fp = fopen(fil, "r")) != NULL) {
        fseek(fp, 0, SEEK_SET);
        start = ftell(fp);
        fseek(fp, 0, SEEK_END);
        slut = ftell(fp);
        rewind(fp);
        storlek = slut-start;
        return(storlek);
    }

    return(-1);
}

Name: Anonymous 2009-04-20 6:39

printf("Kunde inte \xf6ppna %s!\n", argv[1]);

"GRUNNUR"

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List