• OW 2.0 C: Substitute for fopen()?

    From Harry Potter@21:1/5 to All on Sat Nov 14 23:29:13 2020
    I found the problem: I was opening binary files in ASCII mode. In the second argument of fread(), I'm supposed to use "rb-".

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Charlie Gibbs@21:1/5 to Harry Potter on Sun Nov 15 18:03:55 2020
    On 2020-11-15, Harry Potter <rose.joseph12@yahoo.com> wrote:

    I found the problem: I was opening binary files in ASCII mode.
    In the second argument of fread(), I'm supposed to use "rb-".

    Ah yes, I forgot to warn you about that. (Too much Linux
    programming, I guess.) My rule of thumb is that fseek() and
    ftell() are unusable unless a file is opened in binary mode.
    (One exception: fseek(fp,0L,0) will get you back to the start
    of a file regardless of which mode you're in.)

    Now that you've cleaned up your fread(), how's it working?

    BTW if you don't want to allocate 16MB of memory if you
    might only use a fraction of it, use fseek(fp,0L,2) followed
    by ftell(fp); this will give you the size of the file in
    bytes (provided you're open in binary mode :-), and you
    can pass this to malloc() to dynamically allocate a buffer
    which, as Goldilocks says, is not too large, not too small,
    but just right.

    --
    /~\ Charlie Gibbs | "Some of you may die,
    \ / <cgibbs@kltpzyxm.invalid> | but it's sacrifice
    X I'm really at ac.dekanfrus | I'm willing to make."
    / \ if you read it the right way. | -- Lord Farquaad (Shrek)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Harry Potter@21:1/5 to All on Sun Nov 15 18:28:32 2020
    The following is the current code, and it _still_ gives me 0 bytes: -------------------------
    fIn=fopen (argv[1], "rb");
    fread(InBuffer, 1, 16*1024*1024, fIn);
    vz.InEnd=ftell (fIn);
    fclose (fIn); fIn=0;
    printf ("Size: %d\n", vz.InEnd);
    -------------------------

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Charlie Gibbs@21:1/5 to Harry Potter on Mon Nov 16 04:55:39 2020
    On 2020-11-16, Harry Potter <rose.joseph12@yahoo.com> wrote:

    The following is the current code, and it _still_ gives me 0 bytes: -------------------------
    fIn=fopen (argv[1], "rb");
    fread(InBuffer, 1, 16*1024*1024, fIn);
    vz.InEnd=ftell (fIn);
    fclose (fIn); fIn=0;
    printf ("Size: %d\n", vz.InEnd);
    -------------------------

    I'm not sure what to say. I wrote a test program that I compiled
    both under gcc on Linux and Borland C++ Builder 5.5 on Windows XP.
    When run with the command

    foo foo.c

    it displays the following lines:

    fread() returned 857
    ftell() returned 857

    857 is the size in bytes of the source module, foo.c.
    Under Linux it displays 816 rather than 857 because
    Linux newlines are LF rather than CRLF. None of the
    error checks in the code were triggered.

    Here's the complete program:

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

    int main(int argc, char **argv)
    {
    FILE *fp;
    char *buf;
    long templong;
    int i;

    if(argc != 2) {
    printf("Usage: %s <filespec>\n", argv[0]);
    exit(1);
    }
    fp = fopen(argv[1], "rb");
    if(fp == NULL) {
    printf("Can't open %s - errno = %d\n", argv[1], errno);
    exit(1);
    }
    buf = malloc(16777216L);
    if(buf == NULL) {
    printf("malloc() failed - errno = %d\n", errno);
    fclose(fp);
    exit(1);
    }
    i = fread(buf, 1, 16777216L, fp);
    if(i <= 0) {
    printf("fread() returned %d - errno = %d\n", i, errno);
    free(buf);
    fclose(fp);
    exit(1);
    }
    printf("fread() returned %d\n", i);
    templong = ftell(fp);
    printf("ftell() returned %ld\n", templong);
    fclose(fp);
    free(buf);
    exit(0);
    return(0);
    }

    --
    /~\ Charlie Gibbs | "Some of you may die,
    \ / <cgibbs@kltpzyxm.invalid> | but it's sacrifice
    X I'm really at ac.dekanfrus | I'm willing to make."
    / \ if you read it the right way. | -- Lord Farquaad (Shrek)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Harry Potter@21:1/5 to All on Mon Nov 30 04:57:10 2020
    The following is the current code. It still has the same problem: ------------------------------------
    fIn=fopen (argv[1], "rb");
    fread(InBuffer, 1, 16*1024*1024, fIn);
    vz.InEnd=ftell (fIn);
    fclose (fIn); fIn=0;
    printf ("Size: %d\n", vz.InEnd); ------------------------------------

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Charlie Gibbs@21:1/5 to Harry Potter on Mon Nov 30 17:28:22 2020
    On 2020-11-30, Harry Potter <rose.joseph12@yahoo.com> wrote:

    The following is the current code. It still has the same problem: ------------------------------------
    fIn=fopen (argv[1], "rb");
    fread(InBuffer, 1, 16*1024*1024, fIn);
    vz.InEnd=ftell (fIn);
    fclose (fIn); fIn=0;
    printf ("Size: %d\n", vz.InEnd); ------------------------------------

    You could try determining the file size before actually reading
    it. Plus, if you already know the file size, you can ask fread()
    for exactly that number of bytes.

    fIn=fopen (argv[1], "rb");
    fseek(fIn, 0L, 2); /* Jump to end of file. */
    vz.InEnd=ftell (fIn);
    fseek(fIn, 0L, 0); /* Go back to start of file. */
    if(fread(InBuffer, vz.InEnd, 1, fIn) <= 0)
    printf("Read error %d\n", errno);
    fclose (fIn); fIn=0;
    printf ("Size: %d\n", vz.InEnd);

    Note that I swapped the fread() arguments again. Now that you
    know exactly how many bytes to read, you can ask fread() to
    get them in a single chunk, which might be more efficient.

    --
    /~\ Charlie Gibbs | "Some of you may die,
    \ / <cgibbs@kltpzyxm.invalid> | but it's a sacrifice
    X I'm really at ac.dekanfrus | I'm willing to make."
    / \ if you read it the right way. | -- Lord Farquaad (Shrek)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Harry Potter@21:1/5 to All on Mon Nov 30 15:43:29 2020
    Your code worked! Thank you! :D Converting to OW didn't help in speed, though. :(

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)