10 Replies - 4996 Views - Last Post: 28 March 2011 - 09:55 PM Rate Topic: -----

#1 aiislinn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-August 08

Loading a text file into a C++ multidimensional array

Posted 07 March 2011 - 05:16 PM

Hi. I am trying to load a text file with 149 columns and 500+ rows into a multidimensional array.

The data in the text file is delimtered with semicolon eg:

1;12;00:00;30;03/03/2006;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;........... and so on.

My code so far is (I have created a snippet of my text file that is 19 rows and 149 columns to test with):

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;

int main () 
{
    
    string str[18][148];
    ifstream myFile("MZS_UMS_test.txt");
    int a = 0;
    int b = 0;
    if(!myFile)
    //Test file open
    {
                cout<<"Error opening output file"<<endl;
                system("pause");
                return -1;
    }
    while(!myFile.eof())
    {
      getline(myFile,str[a][b],';');
      if(a ==18)
      {
           a=0;
           b++;
           getline(myFile,str[a][b],';');
      }
      a++;
    }
    
    system("pause");
    return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Loading a text file into a C++ multidimensional array

#2 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Loading a text file into a C++ multidimensional array

Posted 07 March 2011 - 05:27 PM

Does it work, what's the problem?
Was This Post Helpful? 0
  • +
  • -

#3 aiislinn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-August 08

Re: Loading a text file into a C++ multidimensional array

Posted 07 March 2011 - 06:14 PM

The data does not seem to be reading in to the correct positions in the array when I check individual positions in the array.

When I try to run the following to print out the contents of the array my program hangs and crashes:

	for(int i = 0; i < 18; ++i)
	{	
            for(int j = 0; j < 148; ++j)
	        cout << str[i][j];
	cout << endl;
        }



I'm not sure where I'm going wrong.
Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Loading a text file into a C++ multidimensional array

Posted 07 March 2011 - 06:44 PM

A couple of things

  • use <cstdlib> rather than <stdlib.h>
  • You are reading the file twice without checking status. Do this instead:


...
while (getline(myFile,str[a][b],';'))
{
 	if(a ==18)
 	{
 		a=0;
 		b++;
 	}
 	a++;
}
...


Was This Post Helpful? 0
  • +
  • -

#5 aiislinn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-August 08

Re: Loading a text file into a C++ multidimensional array

Posted 07 March 2011 - 08:44 PM

It now crashes on compilation. I've tried it outside of my end of file while loop and it also crashes. It will compile when I change the selection if (a == 18) on the while loop to a <= 18 etc, but that defeats the purpose of dropping the array to the next line, (unless I am doing that incorrectly) and some of the info reads into the array but a lot of it is skipped.

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

int main () 
{
    
    string str[18][148];
    ifstream myFile("MZS_UMS_test.txt");
    int a = 0;
    int b = 0;
    if(!myFile)
    {
                cout<<"Error opening input file"<<endl;
                system("pause");
                return -1;
    }
    
    while(!myFile.eof())
    {
      while (getline(myFile,str[a][b],';'))
      {
          if(a == 18)
          {
              a=0; 
              ++b;
          }
          a++;
      }
    }


Was This Post Helpful? 0
  • +
  • -

#6 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Loading a text file into a C++ multidimensional array

Posted 07 March 2011 - 09:12 PM

Quote

It now crashes on compilation.


Please post the exact error(s).

Please post your data file.
Was This Post Helpful? 0
  • +
  • -

#7 aiislinn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-August 08

Re: Loading a text file into a C++ multidimensional array

Posted 07 March 2011 - 09:49 PM

The exact error is "footfall.exe has stopped working. Windows can check online for a solution to the problem" it basically crashes on compilation.

Attached is my data file I am trying to load. This is an example with 19 lines, the real ones will have over 500 lines.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Loading a text file into a C++ multidimensional array

Posted 07 March 2011 - 10:08 PM

In the following snippet the while(getline()) will retrieve one item from the line. It looks like you should be getting about 147 more of these entries. So you probably have your a and b mixed up. The variable a is for the first index and the b is for the second index.

Also in C/C++ arrays start at 0 and go to size - 1, so a should not ever go above 17 in this case.

  while(!myFile.eof())
    {
      while (getline(myFile,str[a][b],';'))
      {
          if(a == 18)
          {
              a=0; 
              ++b;
          }
          a++;
      }



I would get the entire line into a std::string and then parse that line to get the individual items. Is there any reason that you are trying to retrieve all of the numbers into a std::string instead of a numeric type?

You should also consider using a std::vector instead of your array.


Jim
Was This Post Helpful? 1
  • +
  • -

#9 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1431
  • View blog
  • Posts: 4,967
  • Joined: 19-February 09

Re: Loading a text file into a C++ multidimensional array

Posted 07 March 2011 - 10:14 PM

View Postaiislinn, on 08 March 2011 - 05:44 AM, said:

It now crashes on compilation. I've tried it outside of my end of file while loop and it also crashes.


If the program does not compile it should give an error.


Here a is the row/line and b is the column. So each loop you increment the row! Useful to use proper names for variables. Then if the row is 18, increment the column.

     while (getline(myFile,str[a][b],';'))
      {
          if(a == 18)
          {
              a=0; 
              ++b;
          }
          a++;
      }



Was This Post Helpful? 0
  • +
  • -

#10 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Loading a text file into a C++ multidimensional array

Posted 07 March 2011 - 10:42 PM

I agree with jimblumberg's advice. You should be using a vector of vectors since you don't know how many lines the data file might contain. Likewise with the number of tokens in each line. The vectors will give you a size for each. If you need string arrays you'll have the sizes to create them.
Was This Post Helpful? 0
  • +
  • -

#11 aiislinn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-August 08

Re: Loading a text file into a C++ multidimensional array

Posted 28 March 2011 - 09:55 PM

Thanks, I'm now reading in each line into a string array and then reading in line of the array as a vector.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1