Casting a file pointer to char pointer

Discussion in 'C Programming' started by Abhishek, Mar 21, 2006.

  1. Abhishek

    Abhishek Guest

    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
     
    Abhishek, Mar 21, 2006
    #1
    1. Advertising

  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.


    No. Next...

    --
    BR, Vladimir

    Barker's Proof:
    Proofreading is more effective after publication.
     
    Vladimir S. Oka, Mar 21, 2006
    #2
    1. Advertising

  3. Abhishek

    Ben Pfaff Guest

    "Abhishek" <> 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[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \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\
    );}return 0;}
     
    Ben Pfaff, Mar 21, 2006
    #3
  4. 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
     
    Vladimir S. Oka, Mar 21, 2006
    #4
  5. Abhishek

    Jack Klein Guest

    On 21 Mar 2006 14:00:49 -0800, "Abhishek" <> 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/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Mar 21, 2006
    #5
  6. "Abhishek" <> 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) <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.
     
    Keith Thompson, Mar 21, 2006
    #6
  7. Abhishek

    Eric Sosman Guest

    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, Mar 21, 2006
    #7
  8. Abhishek

    hdante Guest

    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).
     
    hdante, Mar 22, 2006
    #8
  9. Abhishek

    CBFalconer Guest

    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/>
     
    CBFalconer, Mar 22, 2006
    #9
  10. Abhishek

    santosh Guest

    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.
     
    santosh, Mar 22, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lovecreatesbeauty
    Replies:
    1
    Views:
    1,099
    Ian Collins
    May 9, 2006
  2. Xavier Roche

    Aliasing bug when casting char[] into char* ?

    Xavier Roche, Feb 6, 2007, in forum: C Programming
    Replies:
    4
    Views:
    423
    Peter Nilsson
    Feb 7, 2007
  3. Xavier Roche

    Aliasing bug when casting char[] into char* ?

    Xavier Roche, Feb 6, 2007, in forum: C Programming
    Replies:
    1
    Views:
    345
    Xavier Roche
    Feb 7, 2007
  4. Michael R. Copeland

    Casting char to char*

    Michael R. Copeland, Sep 19, 2005, in forum: C++
    Replies:
    9
    Views:
    395
    Robbie Hatley
    Sep 21, 2005
  5. Michael R. Copeland

    Casting char to char*

    Michael R. Copeland, Sep 19, 2005, in forum: C++
    Replies:
    10
    Views:
    464
    Default User
    Sep 22, 2005
Loading...

Share This Page