writing binary data whose size exceeds unsinged it

M

mohammad.nabil.h

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 ?
 
K

Keith Thompson

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>
 
M

mohammad.nabil.h

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
 
M

Mike Wahler

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
 
K

Keith Thompson

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.
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.
 
M

mohammad.nabil.h

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

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.
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.
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 ].
 
I

Ian Collins

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.
 
M

mohammad.nabil.h

Ian said:
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

Ian Collins

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.
 
C

CBFalconer

Mark said:
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
 
C

CBFalconer

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
 
K

Keith Thompson

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.)
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.
 
K

Keith Thompson

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top