Read in .dat file unknown number of rows

This is a discussion on Read in .dat file unknown number of rows within the C Programming forums, part of the General Programming Boards category; Ok so i have a couple of .dat files i want to read into my program to then go on ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    1

    Read in .dat file unknown number of rows

    Ok so i have a couple of .dat files i want to read into my program to then go on and do multiple calculations too..

    The .dat files have different number of rows each and are just numbers
    They have 2 columns separated by a space.

    What i want to do is to read them into C into dynamic allocated arrays the left column going to an array called 'x' and the right column going to an array called 'y'

    How can i find out the number of rows so i can then malloc the arrays or is there some sort of loop i can create??
    Also i've heard its bad to use EOF??

    any help would be much apprecitated
    Cheers

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Chris. Unfortunately, we don't do what you've asked for.

    The way it works here, is you give it a shot, and then you post up what you've done, and ask away about whatever has you stumped, and we'll try and get you past that problem/s.

    It's VERY rare that we'll start a persons assignment, for them, with no effort being shown on their part.
    Code:
    while(!feof) {
          //other code here
    }
    is a poor construct, because the detection for EOF comes later than you'd expect, resulting in the last value frequently going thru the loop, one time too many.

    But there's nothing wrong with using EOF in a while loop - remembering that EOF is represented by an integer, and not a char (etc.) data type.

    You can also use the return from fscanf() ( return is the number of objects scanned and stored ). If the return is less than two, then you should exit the loop immediately. This return is also an integer.

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    How can i find out the number of rows so i can then malloc the arrays or is there some sort of loop i can create??
    Time for the magic of logic.

    Let's say you had a box of marbles, and you wanted to give one marble to each kid in the neighborhood, which requires a bag for each kid/marble. But you're kind of low on cash (and have no bags) so you only want to buy the number of bags you need. How would you determine how may bags you need to buy to do this?

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    A penny-pinching, philanthropic puzzle - I like it
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read text file with unknown number of rows in C
    By Raymond2010 in forum C Programming
    Replies: 3
    Last Post: 06-25-2011, 11:27 PM
  2. How to read columns and rows
    By university in forum C Programming
    Replies: 1
    Last Post: 12-05-2010, 11:09 AM
  3. read unknown integers from text file
    By underlink in forum C Programming
    Replies: 2
    Last Post: 11-10-2009, 09:23 AM
  4. Read from file of unknown length, then terminate
    By hello234 in forum C Programming
    Replies: 2
    Last Post: 10-12-2008, 04:33 PM
  5. Replies: 1
    Last Post: 09-10-2005, 06:02 AM

Tags for this Thread


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