Thread: formating data from a file

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    18

    formating data from a file

    Hi I want to know what the easiest way of reading information from a file is, all the files are formated as

    line of text
    [# lines of ints] [# of ints per line]
    int int int int
    int int int int
    etc.

    Here is an example file:

    HS_problem_1 problem_10_10
    10 10
    0 0 0 0 1 0 0 1 0 0
    0 0 1 0 0 1 0 0 0 1
    1 0 0 1 0 0 0 0 0 1
    1 0 1 0 1 0 1 1 0 0
    0 0 0 0 0 0 0 1 1 0
    0 0 0 0 1 0 1 0 1 0
    0 0 0 0 0 0 1 0 0 0
    0 1 0 0 0 0 0 1 0 0
    0 0 0 1 0 0 1 0 0 0
    0 1 0 0 0 0 0 0 1 0

    How would i go about it?
    thanks

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >How would i go about it?

    Something along this line.

    line of text <read whole line using fgets>
    [# lines of ints] [# of ints per line] <read whole line using fgets, parse rows and cols with sscanf>
    <malloc rows in 2D array>
    <for loop through rows>
    <nested for loop through cols>
    <malloc cols in 2D array>
    int int int int<read data using fscanf>
    int int int int...>
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Just in case you are a newbie, and assuming you know the maximum row/column size in any of the files you're bound to read, create a 2D array that will hold the largest data and forget the malloc() part of Dave's suggestion. The rest is fine.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    18
    thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  5. Writing and modifying data in a file
    By Micko in forum C Programming
    Replies: 2
    Last Post: 02-17-2005, 03:42 AM