Casting a file pointer to char pointer

A

Abhishek

Hello everybody,

I am reading a file into a char array. Because of which we cannot give
array size in GBs of data. Is there a technique through which i can
cast a file pointer to char pointer directly, so that in the "main"
function, when i'm calling the function, i can directly pass converted
file pointer as an argument. Please let me know asap.

Thanks & Regards,
Abhishek PM
 
V

Vladimir S. Oka

Abhishek opined:
Hello everybody,

I am reading a file into a char array. Because of which we cannot
give array size in GBs of data. Is there a technique through which i
can cast a file pointer to char pointer directly, so that in the
"main" function, when i'm calling the function, i can directly pass
converted file pointer as an argument. Please let me know asap.

No. Next...
 
B

Ben Pfaff

Abhishek said:
I am reading a file into a char array. Because of which we cannot give
array size in GBs of data. Is there a technique through which i can
cast a file pointer to char pointer directly, so that in the "main"
function, when i'm calling the function, i can directly pass converted
file pointer as an argument. Please let me know asap.

You might be looking for "memory mapped files", which are an
operating system specific feature not directly supported by
standard C or its library.
 
V

Vladimir S. Oka

Abhishek opined:
Hello everybody,

I am reading a file into a char array. Because of which we cannot
give array size in GBs of data. Is there a technique through which i
can cast a file pointer to char pointer directly, so that in the
"main" function, when i'm calling the function, i can directly pass
converted file pointer as an argument. Please let me know asap.

And why do you need to read the whole file into an array. Can't you
read it in small chunks? You could seek through it as well, if you
need random access.

--
BR, Vladimir

President Thieu says he'll quit if he doesn't get more than 50% of the
vote. In a democracy, that's not called quitting.
-- The Washington Post
 
J

Jack Klein

Hello everybody,

I am reading a file into a char array. Because of which we cannot give
array size in GBs of data. Is there a technique through which i can
cast a file pointer to char pointer directly, so that in the "main"
function, when i'm calling the function, i can directly pass converted
file pointer as an argument. Please let me know asap.

Yes, you can convert a FILE pointer to a pointer to char, with a cast,
but I doubt that is going to do you any good. It will not do what you
apparently think it will do.

If you want to access the data from a file, you must read the file.
One byte at a time, many bytes at a time, one line at a time, or some
other way. If you do cast a FILE * to a char *, you cannot just
dereference the char * to access the data from the file.
 
K

Keith Thompson

Abhishek said:
I am reading a file into a char array. Because of which we cannot give
array size in GBs of data. Is there a technique through which i can
cast a file pointer to char pointer directly, so that in the "main"
function, when i'm calling the function, i can directly pass converted
file pointer as an argument. Please let me know asap.

No, you can't meaningfully convert a file pointer to a char pointer.
You can read the contents of the file into memory, but that means
you'll have to allocate enough space to hold it. If you can't
allocate that much space, you'll have to load the file in smaller
pieces (which is usually adequate).

Some systems may have system-specific ways of treating a file as if it
were an array, but they're inherently non-portable. (One such way, on
some systems, is the mmap() function, but discussion of the details
are off-topic here.)
 
E

Eric Sosman

Abhishek wrote On 03/21/06 17:00,:
Hello everybody,

I am reading a file into a char array. Because of which we cannot give
array size in GBs of data. Is there a technique through which i can
cast a file pointer to char pointer directly, so that in the "main"
function, when i'm calling the function, i can directly pass converted
file pointer as an argument. Please let me know asap.

You can cast a FILE* to a char*, but you won't get
anything useful by doing so. In particular, you won't
get access to the data in the file the FILE* stream is
connected to.

Perhaps you're looking for the mmap() function.
It's not part of C, though, but part of POSIX, so if
you need help you should seek it elsewhere, for instance
on comp.unix.programmer.
 
H

hdante

Like everybody said, mmap is POSIX. However, you may be limited to your
system architecture restrictions (eg: less than 4 GB in 32-bit
systems). It's not really correct to use such a big file like this. You
should organize your data either in a database (see, eg., MySQL and
Postgres), or directly in a record (see, eg., Berkeley DB).
 
C

CBFalconer

hdante said:
Like everybody said, mmap is POSIX. However, you may be limited
to your system architecture restrictions (eg: less than 4 GB in
32-bit systems). It's not really correct to use such a big file
like this. You should organize your data either in a database
(see, eg., MySQL and Postgres), or directly in a record (see,
eg., Berkeley DB).

What possible use is such a silly context free posting? What
everybody, who is you, what big file like what. etc. Google is
NOT usenet, it is only an impossibly poor interface to the usenet
system. Readers do not necessarily have the ability to read
previous articles, and in fact may never receive them. Thus all
articles have to stand by themselves.

See my sig. below for means of achieving this on the horrible
google interface, and READ THE REFERENCED URLs.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
S

santosh

Abhishek said:
Hello everybody,

I am reading a file into a char array. Because of which we cannot give
array size in GBs of data. Is there a technique through which i can
cast a file pointer to char pointer directly, so that in the "main"
function, when i'm calling the function, i can directly pass converted
file pointer as an argument. Please let me know asap.

You can cast a FILE * to a char * but doing so is meaningless. You
won't be able to access the associated stream. Using the standard input
functions to read all or a portion of the file into an explicitly
allocated buffer, after which you can treat it as an array. You might
also avail any system specific memory-mapping functions, (usually
called mmap()), either as a non-standard C library extension or as an
OS API.
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top