Thread: what am I doing wrong with this fseek?

  1. #16
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    From MinGW-users - ftello64

    I suggest trying ftello64 and fseeko64; I have never even heard of them before.
    But, it is worth trying.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  2. #17
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    Quote Originally Posted by Salem View Post
    When writing portable code, you should look to create your own wrapper functions around the variable functionality.
    Eg.
    Code:
    #ifdef WIN32
    // Windows
    long long myGetFileSize ( const char *filename ) {
        long long result = -1;
        LARGE_INTEGER answer;
        HANDLE hFile = CreateFile( filename, ... );
        if ( GetFileSizeEx(hFile,&answer) ) {
            result = (long long)answer;
        }
        CloseHandle(hFile);
        return result;
    }
    #else
    // assume POSIX
    long long myGetFileSize ( const char *filename ) {
        long long result = -1;
        int fd = open(filename,O_RDONLY);
        if ( fd >= 0 ) {
            result = lseek64(fd, 0, SEEK_END);
        }
        close(fd);
        return result;
    }
    #endif
    In a larger project, you'll end up with several of these, so it make sense to have
    - porting.h containing all the prototypes
    - porting_win32.c containing all the win32 stuff
    - porting_posix.c containing all the posix stuff

    So you compile all your code, and whichever of the porting.... files makes sense for the platform you're using.
    Your code doesn't compile. I just need a working example so that I can understand what is going on with it, substitute vars and play around with it untill I am sure that I know it.
    POSIX from what I have searched on the web does not appear to have any working examples either just like the Win32.
    Last edited by Once-ler; 03-02-2013 at 03:59 PM.

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Your code doesn't compile. I just need a working example so that I can understand what is going on with it
    You'll learn more by making what I've posted work.

    > substitute vars and play around with it untill I am sure that I know it.
    Yes, the "painting by numbers" approach.
    All this will teach you is how to solve problems which are very similar to problems you've already seen.

    > I am hoping to write code that can be compiled for Windows, Mac OSX, and Linux
    If you seriously want to get to this point in any reasonable amount of time, you need to pick up the pace of learning and trying things for yourself.
    Waiting for other people to give you fully working code just so you can tinker with it a line at a time just won't get you to where you're aspiring to be.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #19
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    I have tried google and dogpile searching for information about how to do this, I have concluded that there is very little information to be had about these processes.
    The best I could come up with was:
    Code:
    #include <stdio.h>
    FILE = *fp;
    main()
    
    
    {
        unsigned long long filesize;
        char[] s= "g:\\Mess.m2ts"
        fp = &s;
        open64("fp, "rb");
        fpos_t file_pos;
        filesize =ftell (fsetpos SEEK_END);
        printf (" the file size is : %ull",filesize);
        return 0;
    }
    but I can't get it to work,not even compile. how do I define the _LARGE_FILE_API macro?
    Salem I just don't understand your example
    Last edited by Once-ler; 03-03-2013 at 11:52 AM.

  5. #20
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    unsigned long long filesize;
    char[] s= "g:\\Mess.m2ts"
    fp = &s;
    open64("fp, "rb");
    fpos_t file_pos;
    filesize =ftell (fsetpos SEEK_END);
    You should really read the man pages of the functions you want to use. Besides all the syntax errors, you still don't understand grumpy's point:
    Quote Originally Posted by grumpy View Post
    And it doesn't change the fact that you are using functions that the standard specifies as working with long types, rather than long long types.
    Regardless of how you declare the variable which should store the return value of ftell(), it will always just return a long int which is too small for your problem. So forget about ftell().

    Quote Originally Posted by Once-ler View Post
    but I can't get it to work,not even compile.
    Salem I just don't understand your example
    You've said you work on Windows. Thus concentrate on the Windows part of Salem's example. If you don't understand how a function should be used read its man page. (I can't help you with Windows-specific functions).

    As said already before, there is no portable way to determine the file size of large files (except this brute force approach which you don't really want to use).

    Bye, Andreas

  6. #21
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    my code now looks like this:
    Code:
        typedef unsigned __int64;
        FILE *fp;
        unsigned __int64 filesize;
        char s[]= "g:\\Mess.m2ts";
        fp = &s;
        fopen64(s, "rb");
        fpos_t file_pos;
        filesize =ftell064 (fsetpos SEEK_END);
        printf (" the file size is : %ud64",filesize);
        return 0;
    but code::blocks stops at the filesize =ftell064 line saying:
    C:\Users\Benjamin\Documents\filesize5.c||In function 'main':|
    C:\Users\Benjamin\Documents\filesize5.c|7|warning: useless type name in empty declaration [enabled by default]|
    C:\Users\Benjamin\Documents\filesize5.c|11|warning : assignment from incompatible pointer type [enabled by default]|
    C:\Users\Benjamin\Documents\filesize5.c|14|error: expected ')' before numeric constant|
    ||=== Build finished: 1 errors, 2 warnings (0 minutes, 0 seconds) ===|
    I feel as if I am close to getting this to work. Could someone please tell me where I am going wrong on this?
    Thanks

  7. #22
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    This was more or less OK (as a valid C program)
    Code:
    FILE *f = fopen("g:\\Mess.m2ts", "rb");
    if (f)
       fseek(f, 0ull, SEEK_END);
       size = ftell(f);
       fclose(f);
       printf ("size is : %ull", size);
    This on the other hand misses the target by a country mile.
    Code:
    FILE = *fp;
    main()
    {
        unsigned long long filesize;
        char[] s= "g:\\Mess.m2ts"
        fp = &s;
        open64("fp, "rb");
        fpos_t file_pos;
        filesize =ftell (fsetpos SEEK_END);
        printf (" the file size is : %ull",filesize);
        return 0;
    }
    This isn't code, this is just random key presses.

    I repeat my previous assertion.
    Learn standard C before venturing out into the world of OS specific API calls.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #23
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    These are NOT the same characters: 0, O, and o !!

    I am giving up on helping you; you seem to not understand simple C syntax.
    You need to walk before you try to run.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #24
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    Salem, your code does not compile, the IDE(Code::Blocks) keeps saying that LARGE_INTEGER is an unknown type and so is HANDLE and that #ifdef is unterminated.
    Is there something that I should #include? Or is there something wrong with my compiler(GCC)?
    I am focusing on the Windows part of your program only.
    Thanks
    Last edited by Once-ler; 03-03-2013 at 04:29 PM.

  10. #25
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    #include <stdio.h>
    
    // Code from http://www.daniweb.com/software-development/c/code/238780/fseeko64-and-ftello64-for-deal-with-large-files-eg.-file-dimension
    // Edited for CB MinGW GCC
    
    int main ()
    {
    
        FILE * pFile;
    
        unsigned long long file_dim = 0;
        char file_name[260] = "";
    
    /////////////////////////////////////////////////////////
    
        printf("ENTER [path] file name.ext:\n");
        gets(file_name);
    
        pFile = fopen (file_name,"rb");
    
        if (pFile==NULL)
            perror ("Error opening file");
        else
        {
            fseeko64 (pFile, 0, SEEK_END);
            file_dim = ftello64 (pFile);
            fclose (pFile);
            printf ("File:\n%s\ncontains %I64d bytes:\n", file_name, file_dim);
        }
        return 0;
    }
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  11. #26
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    Thank you!
    it works!
    and you didn't even have to go into that fread64 or fopen64 stuff
    And thank you for not giving up on me, I do not deliberately try to bother anybody.

  12. #27
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Salem example was likely using MS Visual C or C++; My posted example used MinGW GCC C code or maybe Borland C code.

    FYI: When doing MS Visual C or C++ example code always include "windows.h" unless told to NOT do so.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  13. #28
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    Thanks. I am using GCC and could not make Salem's code work at all.
    I thought I was doing something wrong.
    I would prefer not to use Visual C because it is interpreted code.

  14. #29
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I would prefer not to use Visual C because it is interpreted code.
    Whoever told you this is wrong. Or you just didn't understand them. You are not automatically using the .NET framework, just because you are using Visual C++. Visual C++ out of the box can generate native code too.

  15. #30
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    Thanks for informing me. I thought that visual C after version 6 uses .NET
    I might look into trying it since it does native code, I could use the multi thread options I think but it is not free which will probably mean that I will hold off on trying it for a while
    Last edited by Once-ler; 03-04-2013 at 12:25 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with my fseek() ?
    By barramundi9 in forum C Programming
    Replies: 5
    Last Post: 07-25-2011, 12:40 AM
  2. Reg. fseek
    By pokks in forum C Programming
    Replies: 1
    Last Post: 01-16-2006, 01:28 PM
  3. Help With fseek();
    By Explicit in forum C Programming
    Replies: 3
    Last Post: 05-26-2004, 08:40 PM
  4. fseek
    By Max in forum C Programming
    Replies: 5
    Last Post: 12-15-2003, 03:21 PM
  5. fseek ???
    By davie_scotland in forum C Programming
    Replies: 2
    Last Post: 02-19-2002, 06:13 PM