decalring variables dynamically based on read from file

This is a discussion on decalring variables dynamically based on read from file within the C Programming forums, part of the General Programming Boards category; Hi, Can this be done in C: I Have a file which has certain number of lines as say x1=4 ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    39

    Exclamation decalring variables dynamically based on read from file

    Hi,

    Can this be done in C:

    I Have a file which has certain number of lines as say

    x1=4
    x2=4

    etc etc, the number of lines are dynamic and not known until i read the file in , So I have a declare variables corresponding to the number of lines after the read, is this possible in C,For in C we have to declare all the variable s upfront? please help

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    >> Can this be done in C:

    Open the file, count the lines, allocate accordingly, rewind the filepointer, and read the file. Pretty straighforward.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could also use something like a linked list. Or perhaps a pointer to a pointer to type, and use realloc as you go. There are usually multiple ways to solve the same problem.


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

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    I think you just want arrays if the input is of the form 'Xn = x". Simply store each successive value into the next element. However if you want to retain the arbitrary symbolic variable name, say, the user is expecting an interactive calculator.... then you'd also build an array of those names.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The problem with using an array is that you have to know how big your array is supposed to be beforehand. Even using C99's addition of variable sized arrays, it's a bit of a mess.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  5. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM

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