473,396 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to get object size from stream.

Hi,
i have an incoming object (file) on a istream.

i need to pass it to another (C programmed!)module in a char* array and
providing its size(also needing the size to mallocate the char* array).

i used to get it using the tellg() function. but on some files this does
not seem to work right.
kinda like this:

istream input;
input.seekg(0, ios::end);
int size=input.tellg();

but on some files the module i get the istream from seem to deliver a -1
when calling seekg. which is the error state.
strangely though i can read the whole input to a file like in:

ofstream out("file.bla", ios::out|ios::binary);
out << input.rdbuf();

so my idea and question is:
can i somehow copy/read the whole content into some kind of automated
buffer and get the size this way?(how much was copied).
the one thing i cant do is copy to a local file. so it should be some
kind of temp buffer.
I know this approach is somewhat slow and also wasteful but thats ok.
i need the size of that file!!!! :D
thanks for any ideas!
Sep 18 '06 #1
5 10754

"Noel Mosa" <no****@NOZPEMjustmail.dewrote in message
news:ee**********@online.de...
Hi,
i have an incoming object (file) on a istream.

i need to pass it to another (C programmed!)module in a char* array and
providing its size(also needing the size to mallocate the char* array).

i used to get it using the tellg() function. but on some files this does
not seem to work right.
kinda like this:

istream input;
input.seekg(0, ios::end);
int size=input.tellg();

but on some files the module i get the istream from seem to deliver a -1
when calling seekg. which is the error state.
strangely though i can read the whole input to a file like in:

ofstream out("file.bla", ios::out|ios::binary);
out << input.rdbuf();

so my idea and question is:
can i somehow copy/read the whole content into some kind of automated
buffer and get the size this way?(how much was copied).
the one thing i cant do is copy to a local file. so it should be some
kind of temp buffer.
I know this approach is somewhat slow and also wasteful but thats ok.
i need the size of that file!!!! :D
AFAIK the best you can do is determine how many characters can
be read from the file, which may or may not correspond to the
host OS' notion of 'file size'. Also note that the size of a
character (a.k.a. byte) can and does vary among platforms.

#include <cstdlib>
#include <fstream>
#include <ios>
#include <iostream>

int main()
{
std::ifstream ifs("filename", std::ios::binary);
if(ifs)
{
std::streamsize count(0);

while(ifs.get() != std::ios::traits_type::eof())
++count;

if(!ifs.eof())
std::cout << "Error reading input.\n";
else
ifs.clear();

std::cout << count << " characters read from file.\n";
}
else
std::cout << "Cannot open input.\n";

return ifs ? EXIT_SUCCESS : EXIT_FAILURE;
}
-Mike
Sep 19 '06 #2
Noel Mosa wrote:
Hi,
i have an incoming object (file) on a istream.

i need to pass it to another (C programmed!)module in a char* array and
providing its size(also needing the size to mallocate the char* array).

i used to get it using the tellg() function. but on some files this does
not seem to work right.
kinda like this:

istream input;
input.seekg(0, ios::end);
int size=input.tellg();

but on some files the module i get the istream from seem to deliver a -1
when calling seekg. which is the error state.
strangely though i can read the whole input to a file like in:

ofstream out("file.bla", ios::out|ios::binary);
out << input.rdbuf();

so my idea and question is:
can i somehow copy/read the whole content into some kind of automated
buffer and get the size this way?(how much was copied).
the one thing i cant do is copy to a local file. so it should be some
kind of temp buffer.
I know this approach is somewhat slow and also wasteful but thats ok.
i need the size of that file!!!! :D
thanks for any ideas!
A C++ std::string can hold binary data.

Create a char[] buffer to hold blocks read from the file.

Create a std::string

Open the file in binary mode

Use ifstream readsome() to read a block
into the char[] buffer.

Append the bytes just read to the std::string.

Repeat until all bytes are read and appended to the std::string.

Use std::string length() to determine the final size of the
std::string.

Use calloc() to allocate the buffer for the C code - based
on the value returned by length().

Pass std::string c_str() to memcpy() to copy the content
of the std::string to the C buffer created by calloc().

Larry
Sep 19 '06 #3
On Tue, 19 Sep 2006 01:42:10 +0200 in comp.lang.c++, Noel Mosa
<no****@NOZPEMjustmail.dewrote,
>so my idea and question is:
can i somehow copy/read the whole content into some kind of automated
buffer and get the size this way?(how much was copied).
Sure, that's just what you want to do, and then you also have the
data ready to pass off to the subroutine.

One way, if your compiler is up-to-date

#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <iterator>
using namespace std;

int main()
{
ifstream infile("readme.txt", ios::in|ios::binary);
infile >noskipws;
std::vector<charincoming(
(istream_iterator<char>(infile)),
(istream_iterator<char>()) );
csub(incoming.size(), &incoming[0]);
}
Sep 19 '06 #4
David Harmon wrote:
On Tue, 19 Sep 2006 01:42:10 +0200 in comp.lang.c++, Noel Mosa
<no****@NOZPEMjustmail.dewrote,
>>so my idea and question is:
can i somehow copy/read the whole content into some kind of automated
buffer and get the size this way?(how much was copied).


Sure, that's just what you want to do, and then you also have the
data ready to pass off to the subroutine.

One way, if your compiler is up-to-date

#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <iterator>
using namespace std;

int main()
{
ifstream infile("readme.txt", ios::in|ios::binary);
infile >noskipws;
std::vector<charincoming(
(istream_iterator<char>(infile)),
(istream_iterator<char>()) );
csub(incoming.size(), &incoming[0]);
}
This assumes of course that the 'std::vector<charincoming' will be
small enough to fit in available memory.
Sincerely, Peter Jansson.
Sep 19 '06 #5
On Tue, 19 Sep 2006 18:58:11 GMT in comp.lang.c++, Peter Jansson
<we*******@jansson.netwrote,
>This assumes of course that the 'std::vector<charincoming' will be
small enough to fit in available memory.
Or virtual memory, as the case may be. The original point of the
exercise was to read the data all into memory. It also assumes that
Noel will add a check for the file opening successfully and anything
else he cares about.

Sep 19 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Juan Carlos CORUÑA | last post by:
Hello all, I have developed an automation server using Mark's win32 extensions and I must return a IStream COM object from one method of the automation server. Here is an extract of my code:...
6
by: csvka | last post by:
Hello, I wonder if I could pick your brains. I'm beginning to learn about C++. I have opened a file in my program and I want to read lines from it. I would like this to be done in a separate...
5
by: nmtoan | last post by:
Hi, I could not find any answer to this simple question of mine. Suppose I have to write a program, the main parts of it are as follows: #include <blahblah.h> struct {
6
by: cameron | last post by:
I need to get the size of an objet in memory. I have tried: System.IO.MemoryStream m = new System.IO.MemoryStream(); System.Runtime.Serialization.Formatters.Binary.BinaryFormatter b = new...
8
by: TJ | last post by:
I need to be able to pass a pointer to a (managed code) Stream object to a COM method so it can serialize its data into the stream. I have a C# application that makes uses of Randolf Duke's...
6
by: Scirious | last post by:
People, how can I know how big an object is? I mean, I have an object the collects data from a stream and when it grows to an especific size I need to create a new object to continue collecting the...
3
by: boeledi | last post by:
Dear All, (First of all this is not a c# piece of code but it does not really matter). I would really appreciate if someone could help me. I am developing an ASP.NET web site and I have to...
2
by: =?Utf-8?B?UGF1bA==?= | last post by:
I am using ASP.Net 2.0 and VB.Net (although C#is ok also). I want to create an object/method/function that will take a URL as an input parameter and then return all of the HTML in that page. I...
158
by: jty0734 | last post by:
i don't know what input size of string is. so i can't gets inputsize before malloc function. i want determine the size of malloc without get inputsize in advance. how to deal with it?
1
by: quddusaliquddus | last post by:
Hi :D, I am sending data to server via TCP IP Connection. I am using a continuous loop at the server end - that accepts new clients and while streams can be read, it reads data stream. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.