Thread: Reading selective numbers from a file

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    Reading selective numbers from a file

    Hello there all,
    I have a question regarding file-processing. I have this list of numbers and each numbers:
    2 3
    9 7 5
    8 4 10
    1 6
    The first element(2) represents the number of rows this "matrix" has, and the second number,(3) represents the number of columns. Elements {9,7,5,8,4,10} is the matrix and the last two elements(1 and 6) are two special numbers start and goal.
    Basically I know how to read a file from top to bottom I just do not know how to read each element and stop.
    For instance
    Code:
    //first element need to go into variable int numrows.
    // second element(3) needs to go into variable int numcols.
    // every row needs to go into an array of new int[numcols]
    // start variable (1) has to go into variable int start
    // goal variable (6) has to go into variable int goal
    Please help me get started on this little exercise. Thank you.

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    Clearer Description

    Hey all,
    I have a clearer idea of what I need to do. I need to basically read an input file that has matrix elements in it.
    Code:
    Ex:
    2 3
    9 7 5
    8 4 10
    1 6
    
    2 - int numrows;
    3 - int numcols;
    {(9,7,5),(8,4,10)} - matrix elements
    1 - int start;
    6 - int goal;
    I need to read the matrix elements and place them in a 2-D array.
    Code:
    |9 7 5|
    |8 4 10|
    I am guessing we need to use the numrows and numcols but we can ignore the start and goal variables for now. Would someone please help me along with this? Any feedback is appreciated as always. Thank you.

  3. #3
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    just use two for loops, assuming you have all ready declared the appropiate size of the 2d array. So the first for loop would be your row index in this case 2, and the inner loop would be your columns, 3.
    Code:
    for(int i=0; i < Row; i++)
      for(int j=0; j < Col; j++)
        InFile >> Array[i][j];

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    yeah I know... but

    well, of course. Populating the array is not too hard. I just need to get the values out of the file that I am reading from ... the right values that is... any ideas?

  5. #5
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    that code will get the data out of the file, where InFile is the filestream created.
    Be a leader and not a follower.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem reading numbers in a txt file
    By nimamc in forum C Programming
    Replies: 3
    Last Post: 06-03-2009, 02:35 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. Reading a serie of numbers from file
    By kotoko in forum C Programming
    Replies: 5
    Last Post: 09-26-2008, 01:49 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM