allocating memory

This is a discussion on allocating memory within the C Programming forums, part of the General Programming Boards category; i want to write a program that reads a text file containing an unspecified number of lines of unspecified length. ...

  1. #1
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246

    allocating memory

    i want to write a program that reads a text file containing an unspecified number of lines of unspecified length. the hardest part is to allocate memory. should i just allocate memory with malloc to char array[][] that i think should be enough or what? what's the best way to do it...

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read a line into a buffer, then malloc some space and copy it into it. Lather, rinse, repeat.

    Something like a linked list would work well for holding the lines for you.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Use the system file functions of your compiler to get the file size, allocate a buffer for the entire file, and read it all in.

    Otherwise, you'll have to know at least how big a buffer you can use that is larger than any line in the file.

    Third way is to read the file thru once finding out how long the longest line is, allocate that buffer, and start reading again.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 10:48 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  4. allocating memory in constructor
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-25-2004, 07:45 AM
  5. Replies: 5
    Last Post: 11-24-2002, 10:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21