writing binary data whose size exceeds unsinged it

Discussion in 'C Programming' started by mohammad.nabil.h@gmail.com, Jan 27, 2006.

  1. Guest

    hello, it's me again.
    i was implementing a DBMS, as usual, where i needed to call fwrite
    using 8-byte integer as the length of the data to be written ( just in
    case there is data bigger than 2 GB, which may happen ).
    The problem is that fwrite takes a size_t ( an unsiged 4-byte integer
    ), not 8-byte integer. i also can't play the trick of dividing the
    length between the _Size and _Count parameters because their
    multiplication is also unsigned int ( in the fwrite implementation ).
    My question is, does there exist any IO function that writes data whose
    length requires 8-byte integer ?
     
    , Jan 27, 2006
    #1
    1. Advertising

  2. writes:
    > hello, it's me again.
    > i was implementing a DBMS, as usual, where i needed to call fwrite
    > using 8-byte integer as the length of the data to be written ( just in
    > case there is data bigger than 2 GB, which may happen ).
    > The problem is that fwrite takes a size_t ( an unsiged 4-byte integer
    > ), not 8-byte integer. i also can't play the trick of dividing the
    > length between the _Size and _Count parameters because their
    > multiplication is also unsigned int ( in the fwrite implementation ).
    > My question is, does there exist any IO function that writes data whose
    > length requires 8-byte integer ?


    Sure, on systems where size_t is 8 bytes.

    If you need to write more data than a single fwrite() call can handle
    in your implementation, you should be able to use multiple fwrite()
    calls. Assuming you have a 64-bit integer type (e.g., unsigned long
    long), you can write a simple wrapper function that invokes fwrite()
    as many times as necessary.

    <OT>
    You might also look for "large file support" in your operating system's
    documentation.
    </OT>

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jan 27, 2006
    #2
    1. Advertising

  3. Guest


    > Sure, on systems where size_t is 8 bytes.
    >
    > If you need to write more data than a single fwrite() call can handle
    > in your implementation, you should be able to use multiple fwrite()
    > calls. Assuming you have a 64-bit integer type (e.g., unsigned long
    > long), you can write a simple wrapper function that invokes fwrite()
    > as many times as necessary.


    Thanks so much, i haven't got the idea until u told me, so i did it :
    ulonglong _lfwrite(const void* _Str,size_t _Size,ulonglong _Count,FILE*
    _File)
    {
    union { unsigned int ints[2]; ulonglong count; };

    unsigned int loops = ints[1]; // high-order int
    unsigned int count_each_loop = ints[0];

    ulonglong items_written = 0;

    while(loops)
    {
    loops--;
    items_written += fwrite(_Str,_Size,count_each_loop,_File);
    }

    return items_written;
    }

    >
    > <OT>
    > You might also look for "large file support" in your operating system's
    > documentation.
    > </OT>


    My operating system is windows XP, i think the large file support is
    about the file system. FAT32 can't support files larger than 4 GB. but
    NTFS can support up to 256 TB in a clustered system, and up to 2 TB
    otherwise. source
    http://www.windowsitpro.com/Articles/Index.cfm?ArticleID=27253
     
    , Jan 27, 2006
    #3
  4. Mike Wahler Guest

    <> wrote in message
    news:...
    > hello, it's me again.
    > i was implementing a DBMS, as usual, where i needed to call fwrite
    > using 8-byte integer as the length of the data to be written ( just in
    > case there is data bigger than 2 GB, which may happen ).
    > The problem is that fwrite takes a size_t ( an unsiged 4-byte integer
    > ), not 8-byte integer.


    The range of 'size_t' is implementation-dependent (subject
    to minimum requirements)

    > i also can't play the trick of dividing the
    > length between the _Size and _Count parameters because their
    > multiplication is also unsigned int ( in the fwrite implementation ).


    Yes, but note that you can call 'fwrite()' as many times
    as you like. :)

    > My question is, does there exist any IO function that writes data whose
    > length requires 8-byte integer ?


    There might. But the language doesn't require 'size_t'
    to have size of eight bytes.


    for(i = 0; i < chunks; ++i)
    fwrite( /* etc */ );

    -Mike
     
    Mike Wahler, Jan 27, 2006
    #4
  5. writes:
    >> Sure, on systems where size_t is 8 bytes.
    >>
    >> If you need to write more data than a single fwrite() call can handle
    >> in your implementation, you should be able to use multiple fwrite()
    >> calls. Assuming you have a 64-bit integer type (e.g., unsigned long
    >> long), you can write a simple wrapper function that invokes fwrite()
    >> as many times as necessary.


    Please don't snip attribution lines (the line that says
    "So-and-so writes:").

    > Thanks so much, i haven't got the idea until u told me, so i did it :


    Please don't use silly abbreviations like "u" for "you". If you want
    us to read what you write, take the time to spell things out.

    > ulonglong _lfwrite(const void* _Str,size_t _Size,ulonglong _Count,FILE*
    > _File)
    > {
    > union { unsigned int ints[2]; ulonglong count; };
    >
    > unsigned int loops = ints[1]; // high-order int
    > unsigned int count_each_loop = ints[0];
    >
    > ulonglong items_written = 0;
    >
    > while(loops)
    > {
    > loops--;
    > items_written += fwrite(_Str,_Size,count_each_loop,_File);
    > }
    >
    > return items_written;
    > }


    Don't use identifiers starting with underscores. Many of them are
    reserved for use by the implementation.

    What is "ulonglong"? It looks like it should be an alias for
    "unsigned long long", but why use an alias? It can only cause
    confusion.

    >> <OT>
    >> You might also look for "large file support" in your operating system's
    >> documentation.
    >> </OT>

    >
    > My operating system is windows XP, i think the large file support is
    > about the file system. FAT32 can't support files larger than 4 GB. but
    > NTFS can support up to 256 TB in a clustered system, and up to 2 TB
    > otherwise. source
    > http://www.windowsitpro.com/Articles/Index.cfm?ArticleID=27253


    The "OT" tags are short for off-topic, i.e., the issue isn't relevant
    to this newsgroup. (Yeah, I know, it's another silly abbreviation.)
    Details of Windows XP are definitely off-topic here; there are plenty
    of Windows newsgroups.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jan 28, 2006
    #5
  6. On Sat, 28 Jan 2006 00:13:13 GMT, in comp.lang.c , Keith Thompson
    <> wrote:

    >
    >What is "ulonglong"?


    its a river in east africa

    >It can only cause confusion.


    only if you paddle backwards.

    gd&r
    Mark McIntyre
    --
    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Mark McIntyre, Jan 28, 2006
    #6
  7. Guest

    Keith Thompson wrote:

    > Please don't snip attribution lines (the line that says
    > "So-and-so writes:").


    > >> <OT>
    > >> You might also look for "large file support" in your operating system's
    > >> documentation.
    > >> </OT>

    > >
    > > My operating system is windows XP, i think the large file support is
    > > about the file system. FAT32 can't support files larger than 4 GB. but
    > > NTFS can support up to 256 TB in a clustered system, and up to 2 TB
    > > otherwise. source
    > > http://www.windowsitpro.com/Articles/Index.cfm?ArticleID=27253

    >
    > The "OT" tags are short for off-topic, i.e., the issue isn't relevant
    > to this newsgroup. (Yeah, I know, it's another silly abbreviation.)
    > Details of Windows XP are definitely off-topic here; there are plenty
    > of Windows newsgroups.


    Sorry for that. I am totally new to posting to news groups; this is my
    second post so I don't really know the system around here.

    >
    > > ulonglong _lfwrite(const void* _Str,size_t _Size,ulonglong _Count,FILE*
    > > _File)

    >
    > Don't use identifiers starting with underscores. Many of them are
    > reserved for use by the implementation.


    I'll follow the advice, thank you.

    >
    > What is "ulonglong"? It looks like it should be an alias for
    > "unsigned long long", but why use an alias? It can only cause
    > confusion.
    >


    I used the alias because I use this type a lot in my code and I surely
    don't want to forget the "unsigned" before it undeliberately. So I made
    the alias for this reason. Also the alias gives an easy way to change
    the type - for example: to signed - in all the code, by just one
    change.

    >
    > > > Thanks so much, i haven't got the idea until u told me, so i did it :

    >
    > Please don't use silly abbreviations like "u" for "you". If you want
    > us to read what you write, take the time to spell things out.


    Sorry for that too .. It was unintentional. It just came from being
    used to use it in instant messaging. Even when I write big essays, and
    intend to write "you" correctly; I sometimes miss and type it "u"
    subconciously a few times. (Note: my first language is not english).
    Thanks for bearing my 'mistakes' so far, and hope you will forgive me
    :). [ I hope smilies are allowed too ].
     
    , Jan 28, 2006
    #7
  8. Ian Collins Guest

    wrote:
    >
    >>What is "ulonglong"? It looks like it should be an alias for
    >>"unsigned long long", but why use an alias? It can only cause
    >>confusion.
    >>

    >
    >
    > I used the alias because I use this type a lot in my code and I surely
    > don't want to forget the "unsigned" before it undeliberately. So I made
    > the alias for this reason. Also the alias gives an easy way to change
    > the type - for example: to signed - in all the code, by just one
    > change.
    >

    Does your compiler/platform have the C99 size types? If so, you can use
    uint64_t and friends.

    --
    Ian Collins.
     
    Ian Collins, Jan 28, 2006
    #8
  9. Guest

    Ian Collins wrote:
    > wrote:
    > >
    > >>What is "ulonglong"? It looks like it should be an alias for
    > >>"unsigned long long", but why use an alias? It can only cause
    > >>confusion.
    > >>

    > >
    > >
    > > I used the alias because I use this type a lot in my code and I surely
    > > don't want to forget the "unsigned" before it undeliberately. So I made
    > > the alias for this reason. Also the alias gives an easy way to change
    > > the type - for example: to signed - in all the code, by just one
    > > change.
    > >

    > Does your compiler/platform have the C99 size types? If so, you can use
    > uint64_t and friends.
    >

    I don't think so. My compiler is Visual Studio 2005 in Windows XP. Does
    anybody have an idea where I can find the list of compilers supporting
    C99 standard ? I've been searching for a while and i didn't find any.
     
    , Jan 28, 2006
    #9
  10. Ian Collins Guest

    wrote:
    > Ian Collins wrote:
    >
    >> wrote:
    >>
    >>>>What is "ulonglong"? It looks like it should be an alias for
    >>>>"unsigned long long", but why use an alias? It can only cause
    >>>>confusion.
    >>>>
    >>>
    >>>
    >>>I used the alias because I use this type a lot in my code and I surely
    >>>don't want to forget the "unsigned" before it undeliberately. So I made
    >>>the alias for this reason. Also the alias gives an easy way to change
    >>>the type - for example: to signed - in all the code, by just one
    >>>change.
    >>>

    >>
    >>Does your compiler/platform have the C99 size types? If so, you can use
    >>uint64_t and friends.
    >>

    >
    > I don't think so. My compiler is Visual Studio 2005 in Windows XP. Does
    > anybody have an idea where I can find the list of compilers supporting
    > C99 standard ? I've been searching for a while and i didn't find any.
    >

    Just see if you have inttypes.h, they are declared in there. Nothing
    special, just typedefs.

    --
    Ian Collins.
     
    Ian Collins, Jan 28, 2006
    #10
  11. CBFalconer Guest

    Mark McIntyre wrote:
    > Keith Thompson <> wrote:
    >
    >> What is "ulonglong"?

    >
    > its a river in east africa


    Isn't that the one that Katherine Hepburn descended with Humphrey
    Bogart, circa 1914?

    --
    "The power of the Executive to cast a man into prison without
    formulating any charge known to the law, and particularly to
    deny him the judgement of his peers, is in the highest degree
    odious and is the foundation of all totalitarian government
    whether Nazi or Communist." -- W. Churchill, Nov 21, 1943
     
    CBFalconer, Jan 28, 2006
    #11
  12. CBFalconer Guest

    wrote:
    > Keith Thompson wrote:
    >

    .... snip ...
    >>
    >> Please don't use silly abbreviations like "u" for "you". If you
    >> want us to read what you write, take the time to spell things out.

    >
    > Sorry for that too .. It was unintentional. It just came from being
    > used to use it in instant messaging. Even when I write big essays,
    > and intend to write "you" correctly; I sometimes miss and type it
    > "u" subconciously a few times. (Note: my first language is not
    > english). Thanks for bearing my 'mistakes' so far, and hope you
    > will forgive me :). [ I hope smilies are allowed too ].


    They are. You are doing fine, and learning at a great rate. It's
    the ones who refuse to listen and generally clutter the newsgroups
    that annoy. The following references may be useful.

    http://www.caliburn.nl/topposting.html
    http://www.netmeister.org/news/learn2quote.html
    http://www.catb.org/~esr/faqs/smart-questions.html
    http://www.greenend.org.uk/rjk/2000/06/14/quoting.html
    http://www.i-hate-computers.demon.co.uk/
    http://web.ukonline.co.uk/g.mccaughan/g/remarks/uquote.html

    --
    "The power of the Executive to cast a man into prison without
    formulating any charge known to the law, and particularly to
    deny him the judgement of his peers, is in the highest degree
    odious and is the foundation of all totalitarian government
    whether Nazi or Communist." -- W. Churchill, Nov 21, 1943
     
    CBFalconer, Jan 28, 2006
    #12
  13. writes:
    > Keith Thompson wrote:

    [snip]
    >> What is "ulonglong"? It looks like it should be an alias for
    >> "unsigned long long", but why use an alias? It can only cause
    >> confusion.

    >
    > I used the alias because I use this type a lot in my code and I surely
    > don't want to forget the "unsigned" before it undeliberately. So I made
    > the alias for this reason. Also the alias gives an easy way to change
    > the type - for example: to signed - in all the code, by just one
    > change.


    A better solution is just to remember to type "unsigned long long"
    each time. Or decide exactly why you're using that particular type,
    and create an alias based on that reason. If you specifically want to
    use unsigned long long, just use unsigned long long. If you want a
    large index type that might be either unsigned long long or unsigned
    long, declare a typedef such as "large_index".

    The name "ulonglong" clearly implies "unsigned long long". Imagine
    the confusion if you change it to

    typedef long long ulonglong;

    Creating typedefs for predefined types is ok if the typedef specified
    the purpose for which you're using the type. Typedefs that just save
    a few keystrokes are a waste of time. You're likely to read your code
    many more times than you write it; a little extra effort while you're
    writing it really pays off. (This applies both to C and to English.)

    >> > > Thanks so much, i haven't got the idea until u told me, so i did it :

    >>
    >> Please don't use silly abbreviations like "u" for "you". If you want
    >> us to read what you write, take the time to spell things out.

    >
    > Sorry for that too .. It was unintentional. It just came from being
    > used to use it in instant messaging. Even when I write big essays, and
    > intend to write "you" correctly; I sometimes miss and type it "u"
    > subconciously a few times. (Note: my first language is not english).
    > Thanks for bearing my 'mistakes' so far, and hope you will forgive me
    > :). [ I hope smilies are allowed too ].


    Yes, smilies are allowed. :cool:} And your efforts are greatly
    appreciated.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jan 28, 2006
    #13
  14. writes:
    > Ian Collins wrote:
    >> wrote:
    >> >
    >> >>What is "ulonglong"? It looks like it should be an alias for
    >> >>"unsigned long long", but why use an alias? It can only cause
    >> >>confusion.
    >> >>
    >> >
    >> >
    >> > I used the alias because I use this type a lot in my code and I surely
    >> > don't want to forget the "unsigned" before it undeliberately. So I made
    >> > the alias for this reason. Also the alias gives an easy way to change
    >> > the type - for example: to signed - in all the code, by just one
    >> > change.
    >> >

    >> Does your compiler/platform have the C99 size types? If so, you can use
    >> uint64_t and friends.
    >>

    > I don't think so. My compiler is Visual Studio 2005 in Windows XP. Does
    > anybody have an idea where I can find the list of compilers supporting
    > C99 standard ? I've been searching for a while and i didn't find any.


    I don't know about Visual Studio, but a lot of compilers support
    <inttypes.h> and/or type long long (both new features in C99) that
    don't necessarily have full C99 support.

    There's also a version of <inttypes.h> that can be used with C90
    compilers at <http://www.lysator.liu.se/c/q8/index.html>.

    Note that any code that depends on 64-bit types integer types, or on
    long long, isn't going to be entirely portable until C99 actually
    catches on -- but it's not necessarily going to be limited to full C99
    implementations.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jan 28, 2006
    #14
  15. On Fri, 27 Jan 2006 21:38:00 -0500, in comp.lang.c , CBFalconer
    <> wrote:

    >Mark McIntyre wrote:
    >> Keith Thompson <> wrote:
    >>
    >>> What is "ulonglong"?

    >>
    >> its a river in east africa

    >
    >Isn't that the one that Katherine Hepburn descended with Humphrey
    >Bogart, circa 1914?


    we /both/ need to get out more.
    Mark McIntyre
    --
    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Mark McIntyre, Jan 28, 2006
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Z2xlbm4=?=
    Replies:
    0
    Views:
    348
    =?Utf-8?B?Z2xlbm4=?=
    Jun 22, 2006
  2. Joseph Suprenant

    Unsinged char to int

    Joseph Suprenant, Aug 18, 2003, in forum: C Programming
    Replies:
    2
    Views:
    433
    Jan Engelhardt
    Aug 18, 2003
  3. Replies:
    5
    Views:
    3,214
  4. Ben
    Replies:
    7
    Views:
    449
    Dave Anderson
    May 17, 2006
  5. Replies:
    2
    Views:
    403
    Eric Sosman
    Dec 17, 2013
Loading...

Share This Page