Thread: reading input files with different types of data

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    8

    reading input files with different types of data

    I have files with data that I want to use as input. Now heres the problem. The input files are built like this:

    Code:
    1
    2
    
    1.1
    2.2
    Now the thing I want to do is put the first two values in one array and the second two values in another array. BUT... the size of the arrays depend on how many values there are in the the first cluster. The input file could just as well look like:
    Code:
    1
    2
    3
    
    1.1
    2.2
    3.3
    To summarize: I want to get the length of the first cluster before I put it into an array. Perhaps dynamic arrays will do the trick?

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    If you want the length of the first cluster you must read the file twice (first for calculating the length and second for reading the values). It's better to use dynamic arrays. Here's an example:
    Code:
    #include <stdlib.h>
    #include <malloc.h>
    
    int main(void)
    {
      int i;
      int *intArray = NULL;
    
      for(i = 1; i < 11; i++)
      {
        intArray = (int *)realloc(intArray, i * sizeof(*intArray) );
        intArray[i-1] = i;
      }
    
      free(intArray);
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    You can store each line of data into a vector. When you encount an empty line, then you can dynamically allocate an array of doubles based on the size of the vector. Copy the data in the vector to your array of doubles. Afterward, empty the vector and continue reading in new data.

    Kuphryn

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    8
    which function returns the existence of the empty line?

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    You determine the status of a line via the size of the string you read in. For example, if the string you read in for line #15 equals zero, then line #15 is empty.

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. reading data fom files
    By shuo in forum C++ Programming
    Replies: 11
    Last Post: 10-22-2007, 12:24 AM
  3. Problem with reading data files
    By CConfusion in forum C Programming
    Replies: 3
    Last Post: 04-02-2006, 02:55 PM
  4. reading data from the users input
    By SpEkTrE in forum C++ Programming
    Replies: 1
    Last Post: 04-10-2005, 06:15 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM