Read a line of unknown length in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SoFaraway
    New Member
    • Jun 2007
    • 9

    Read a line of unknown length in C

    Hi all,

    In C, to read a line from a file, we need to allocate some fixed length of memory first. However, if the line is longer than the length of the allocated memory, it can't be read correctly.

    Does anyone have codes that can read in a line of unknown length?

    Thank you very much!
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by SoFaraway
    Hi all,

    In C, to read a line from a file, we need to allocate some fixed length of memory first. However, if the line is longer than the length of the allocated memory, it can't be read correctly.

    Does anyone have codes that can read in a line of unknown length?

    Thank you very much!
    All you need is pointer to a char:

    char *ptr.

    and then you can parse the file using scanf functions.

    See this article and especialy the part which tells about Non-buffered functions.

    Savage

    Comment

    Working...