how to get object size from stream.

N

Noel Mosa

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::eek:ut|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!
 
M

Mike Wahler

Noel Mosa said:
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::eek:ut|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
 
L

Larry I Smith

Noel said:
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::eek:ut|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
 
D

David Harmon

On Tue, 19 Sep 2006 01:42:10 +0200 in comp.lang.c++, Noel Mosa
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<char> incoming(
(istream_iterator<char>(infile)),
(istream_iterator<char>()) );
csub(incoming.size(), &incoming[0]);
}
 
P

Peter Jansson

David said:
On Tue, 19 Sep 2006 01:42:10 +0200 in comp.lang.c++, Noel Mosa
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<char> incoming(
(istream_iterator<char>(infile)),
(istream_iterator<char>()) );
csub(incoming.size(), &incoming[0]);
}

This assumes of course that the 'std::vector<char> incoming' will be
small enough to fit in available memory.


Sincerely, Peter Jansson.
 
D

David Harmon

On Tue, 19 Sep 2006 18:58:11 GMT in comp.lang.c++, Peter Jansson
This assumes of course that the 'std::vector<char> incoming' 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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top