473,378 Members | 1,391 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,378 software developers and data experts.

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

Mar 21 '06 #1
9 9214
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.

Mar 21 '06 #2
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

Mar 21 '06 #3
"Abhishek" <ap****@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;}
Mar 21 '06 #4
On 21 Mar 2006 14:00:49 -0800, "Abhishek" <ap****@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
Mar 21 '06 #5
"Abhishek" <ap****@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) ks***@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.
Mar 21 '06 #6


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.

--
Er*********@sun.com

Mar 21 '06 #7
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).

Mar 22 '06 #8
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/>
Mar 22 '06 #9
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.

Mar 22 '06 #10

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

Similar topics

3
by: cylin | last post by:
Dear all, I met a char pointer problem. please help,thanks. How to change the the value which is pointed by a char pointer by a function? for example,...
1
by: venkatesh | last post by:
Hi Members, I need to convert Char Pointer to a Structure.How Can i typecast this. thanks in advance venkat
4
by: Joe H | last post by:
Hi i converted a program to a lib. The parameters of main are maincommand(int argc, char **argv) I want to use main a s function that takes a string how can i convert a char * to a char ** or even...
14
by: mr_semantics | last post by:
I have been reading about the practise of casting values to unsigned char while using the <ctype.h> functions. For example, c = toupper ((unsigned char) c); Now I understand that the standard...
13
by: chellappa | last post by:
hi , please explain me , char pointer , char Array, char ,string... i have some many confussion about this... please clarify to me.. i need some example about this program by chellappa.ns
33
by: Mark P | last post by:
A colleague asked me something along the lines of the following today. For some type X he has: X* px = new X; Then he wants to convert px to a char* (I'm guessing for the purpose of...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
13
by: arnuld | last post by:
i see the use of pointers, from K&R2 but what is the use of: 1. "pointer to pointer": char c; char** ppc; 2. pointer to function:
17
by: Andrea Taverna (Tavs) | last post by:
Subject: Initialization of a const matrix implemented as pointer-to-pointer Hello everyone. I've got the following matrix definition in a source file static const char **a; I need it to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.