You are here:

C++/Read file into 2D array


I do not know what is wrong with the following code. I am trying to read data into a 2D array. The data is as following

Index   Weight   Len
0        2.5      5
1        1.05     6
2         .25     3

All I get is a black screen there is no error message
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <string>

//const int rMAX_SIZE = 9;
//const int cMAX_SIZE = 4;

//Function Declarations
void discard_line(ifstream &in);

int main()
float ASTM_table[9][4];
int Choice_1;
int total_records;

ifstream input;

//Program Introducation

cout<<"This program determines the total weight of rebars used in a given scenario.\n"
   <<"The calculation is based on ASTM (American Scoiety for Testing and Meterials)\n"

 //read ASTM table and store in multidimensional array"E:\\astm.txt",ios::in);


 total_records = 9;


       for(int i = 0;i< total_records;i++)
         input>> ASTM_table[total_records][i];

//Program Menu.....
cout<<"Program Menu:\n"
   <<"(1) Print ASIM table\n"
   <<"(2) Read the lenght of rebars used from a file\n"
   <<"(3) Exit\n"
   <<"Enter the number of your choice: ";


     return 0;

void discard_line(ifstream &in)
   char c;

   while (c!='\n');

ok, let's start from the top in your code:

>   #include <iostream.h>
>   #include <fstream.h>
I suppose you're using a very old (pre-standard) compiler; if you are, you really should get a current one and start using standard C++. Many modern compilers won't even accept your code.

to include the standard headers, use:
  #include <iostream>
  #include <fstream>
and you might want to put in a
  using namespace std ;


>   #include<stdlib.h>
>   #include<string>
You don't use anything from these, so don't include them.
and the standard header is <cstdlib>


>   ifstream input;
You can combine these, as ifstream has a constructor that opens a file for input:
  ifstream input( "E:\\astm.txt" );

and perhaps you should write:
  const char* fname = "E:\\astm.txt" ;
  ifstream input( fname );

also, you should check that you have been able to open the file (the path might have been wrong):
   if( !input )
     cout << "could not open file " << fname << " for input\n" ;
     return 1 ;


>  while(!input.eof());
>  {
>    // read data
>  }

there should not be a ; at the end of the while. the block following is not part of the loop. instead:

   // read data

This is a bugprone usage pattern. The bug is that the eof() member function doesn't return true until after you have tried and failed to read from the file. But because you're already in the body of the loop at that point, you are going to process a failed read.  The fix is very simple, as input operations return the stream and its state can be checked:

float value ;
while( input >> value )
 // add value to ASTM_table


>  total_records = 9;
>  // ...       
>  for(int i = 0;i< total_records;i++)
>  {
>   input>> ASTM_table[total_records][i];
>   total_records++;
>  }

This would result in an infinite loop:
a. at the beginning, i is 0 and total_records is 9.
b. each time through the loop, both are incremented.
c. therefor, i < totalrecords would always be true.

> void discard_line(ifstream &in)
> {
>   char c;
>  do
>         in.get(c);
>   while (c!='\n');
> }

The best method to discard input is to use in.ignore(<some large number>, '\n') to clear the input upto and includeing a newline. You could simply hard code a large number, although it's usually better to use a built-in constant. The standard library provides such a constant, which is
numeric_limits<streamsize>::max() (#include <limits>)

void discard_line(ifstream &in)
  in.ignore(numeric_limits<streamsize>::max(), '\n');

this looks like homework.
with this information, you should be able to complete it now.


All Answers

Answers by Expert:

Ask Experts




my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.


about 15 years or so

post graduate engineer

©2016 All rights reserved.