File handling in C

Discussion in 'C Programming' started by PJ, Sep 29, 2005.

  1. PJ

    PJ Guest

    Hi,

    I have a file that contains some data. I want to write a function that
    reads the file and return a string(char *) which contains the content
    of file.(File can be of any size).

    PJ
    PJ, Sep 29, 2005
    #1
    1. Advertising

  2. PJ

    Mike Wahler Guest

    "PJ" <> wrote in message
    news:...
    > Hi,
    >
    > I have a file that contains some data. I want to write a function that
    > reads the file and return a string(char *) which contains the content
    > of file.(File can be of any size).


    So write it. If you get stuck, post your code and
    ask specific questions. That's how to get help here.

    If you're looking for someone to write the code for
    you, try a 'help wanted' group.

    -Mike
    Mike Wahler, Sep 29, 2005
    #2
    1. Advertising

  3. PJ

    Eric Sosman Guest

    PJ wrote On 09/29/05 10:28,:
    > Hi,
    >
    > I have a file that contains some data. I want to write a function that
    > reads the file and return a string(char *) which contains the content
    > of file.(File can be of any size).


    Use fopen() to "connect" an input stream to your file.

    Use malloc() to obtain a memory area to hold the
    content.

    Use getc() to read characters from the stream, one at
    a time. As you read each character, store it in the next
    byte of your memory area. If you receive more characters
    than will fit, use realloc() to enlarge the area.

    When getc() says there are no more characters, store
    a '\0' to mark the end of the string. Remember that this
    terminator takes space just like "real" characters; if
    all the "real" characters exactly fill your memory area
    you need one more byte for the '\0'.

    Use fclose() to "disconnect" the stream from the file.

    --
    Eric Sosman, Sep 29, 2005
    #3
  4. PJ wrote:
    > Hi,
    >
    > I have a file that contains some data. I want to write a function that
    > reads the file and return a string(char *) which contains the content
    > of file.(File can be of any size).
    >
    > PJ


    This seems like another question with the same answer. Go get FreeDOS
    Edlin (available at either ibiblio or alt.sources) and see how I did
    it.

    Gregory Pietsch
    Gregory Pietsch, Sep 29, 2005
    #4
    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. Thomas Due

    Handling large file upload

    Thomas Due, Aug 11, 2003, in forum: ASP .Net
    Replies:
    6
    Views:
    3,942
    Jos Vernon
    Aug 20, 2003
  2. uwb
    Replies:
    4
    Views:
    359
  3. Mark Tarver
    Replies:
    22
    Views:
    1,298
    J Kenneth King
    Apr 26, 2009
  4. Peter
    Replies:
    34
    Views:
    1,932
    James Kanze
    Oct 17, 2009
  5. Iñaki Baz Castillo
    Replies:
    1
    Views:
    181
    Iñaki Baz Castillo
    Apr 15, 2008
Loading...

Share This Page