By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,587 Members | 1,079 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,587 IT Pros & Developers. It's quick & easy.

how to get object size from stream.

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.