Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Casting a file pointer to char pointer (http://www.velocityreviews.com/forums/t441910-casting-a-file-pointer-to-char-pointer.html)

Abhishek 03-21-2006 10:00 PM

Casting a file pointer to char pointer
 
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


Vladimir S. Oka 03-21-2006 10:31 PM

Re: Casting a file pointer to char pointer
 
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...

--
BR, Vladimir

Barker's Proof:
Proofreading is more effective after publication.


Ben Pfaff 03-21-2006 10:38 PM

Re: Casting a file pointer to char pointer
 
"Abhishek" <apmsai@gmail.com> writes:

> 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.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}

Vladimir S. Oka 03-21-2006 10:40 PM

Re: Casting a file pointer to char pointer
 
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


Jack Klein 03-21-2006 10:42 PM

Re: Casting a file pointer to char pointer
 
On 21 Mar 2006 14:00:49 -0800, "Abhishek" <apmsai@gmail.com> wrote in
comp.lang.c:

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

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

Keith Thompson 03-21-2006 10:53 PM

Re: Casting a file pointer to char pointer
 
"Abhishek" <apmsai@gmail.com> writes:
> 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.)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Eric Sosman 03-21-2006 10:58 PM

Re: Casting a file pointer to char pointer
 


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.

--
Eric.Sosman@sun.com


hdante 03-22-2006 02:08 AM

Re: Casting a file pointer to char pointer
 
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).


CBFalconer 03-22-2006 04:21 AM

Re: Casting a file pointer to char pointer
 
hdante wrote:
>
> 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/>



santosh 03-22-2006 07:48 AM

Re: Casting a file pointer to char pointer
 
Abhishek wrote:
> 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.



All times are GMT. The time now is 05:14 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.