[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 455
  • Last Modified:

Help required with reading text file into 3d array

Hi,

I'm trying to read text file into 3d array shown in the code. However, nothing displays on the screen.

My text file cabinconfig.txt contains the following:

BIIB
BIIB
BIIB
BIIB
BIIB
WIIW
WIIW
WIIW
WIIW
WIIW
WIIW
WIIW
WIIW

Please help me out figure out what's wrong with my code, why i'm unable to read and display the information from my text file.

Thankyou.

#include <iostream>
#include <fstream>

using namespace std;
int main()
{
char Cabins[3][13][4];
ifstream indata;
     indata.open("cabinconfig.txt");
      if(!indata)
      {
                 cout << "File could not be opened" << endl;
                 system("pause");
                 exit(1);
      }
      else
      {
       for(int l=0; l<3; l++)//Levels
      {
       for(int r=0; r<13; r++)//Rows
       {
        for(int c=0; c<4; c++)//Colunms
        {
            	indata >> Cabins[l][r][c];
        }
       }
      }
      
      }
      indata.close();
     system("pause");
}

Open in new window

0
FallenAngel1
Asked:
FallenAngel1
  • 4
  • 3
3 Solutions
 
mr_stevieCommented:
Hello,

From the code you provided, you are not outputting to the screen.

You could add the below code to see all the elements in the array.

I'm not sure if this is what you mean.
		for(int l=0; l<3; l++)//Levels
		{
			for(int r=0; r<13; r++)//Rows
			{
				for(int c=0; c<4; c++)//Colunms
				{
					cout << Cabins[l][r][c] << endl;
				}
			}
		}

Open in new window

0
 
FallenAngel1Author Commented:
now i'm getting some different characters along with characters from my text file...and not in the format the following format

BIIB
BIIB
BIIB
etc

my output is shown in the image
output.png
0
 
mr_stevieCommented:
The different characters you are getting is most likely garage output which represents an empty array element.

As for everything else, you should store the information in a std::string as chars only store one character element unless you're storing them in a char array.

Instead of using the ">>" operator, you should use getline() which will take each line in the .txt file

Once you use the getline() to store it in a temporary string variable, pass it into the array.

You should get the expected result.

Hope this helps.



#include <iostream>
#include <fstream>
#include <string> // STD::String library

using namespace std;

int main()
{
	// NOTE: Using an array of strings
	string Cabins[3][13][4];
	ifstream indata;
	indata.open("cabinconfig.txt");
	
	// Temporary String Variable to store the line from the file.
	string line;

	if(!indata)
	{
			 cout << "File could not be opened" << endl;
			 system("pause");
			 exit(1);
	}
	else
	{
		for(int l=0; l<3; l++)//Levels
			for(int r=0; r<13; r++)//Rows
				for(int c=0; c<4; c++)//Colunms
				{
					// Get the string from indata and store it into line
					getline( indata, line );

					// Pass the line into the array
					Cabins[l][r][c] = line;
				}
	}

	indata.close();
	system("pause");
}

Open in new window

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
FallenAngel1Author Commented:
Hi, again nothing is being displayed from the .txt file. screen is blank.
0
 
mr_stevieCommented:
Are you sure you're outputting to the screen?
If not, refer to my code above.

If you're using a good I.DE., you can use the Debugging option to watch variables and such to make sure the variables are what they are suppose to be...
0
 
FallenAngel1Author Commented:
I added the code you referred to and that's when i got the garbage output. I also tried out the other code and the blank screen was displayed.. i'm using Dev C++.
0
 
mr_stevieCommented:
The first piece of code I provided was to display all the elements in an array...

The second piece of code provided was using the getline() method but it is not putting anything onto the console. You'll need to add the first piece of code I provided into the second piece so that it will output onto the screen...
0
 
phoffricCommented:
I may be missing something, but could you explain how you wanted to store the text file in your 3D array of chars?

Looking at the text file, I see 13 lines, where each line has 4 viewable chars plus a newline for a total of 5 chars. If you throw away the newline (which may be reasonable depending upon your application), then the text file structure suggests a 2D array of chars, [13 lines] by [4 chars]; i.e., [13][4].

Putting it another way, if you ignore newlines, then there are 13*4 = 52 viewable letters in your text file.
If I defined an array_Of_Chars as:
     char array_Of_Chars[13][4];
then I have defined 52 chars. So, I would have an intuitive idea of where these text file chars should go. Namely,
array_Of_Chars[0][0] <- 'B'
array_Of_Chars[0][1] <- 'I'
array_Of_Chars[0][2] <- 'I'
array_Of_Chars[0][3] <- 'B'
array_Of_Chars[1][0] <- 'B'
array_Of_Chars[1][1] <- 'I'

So, that is why I do not understand how you plan on putting just those 52 chars into an array which has 52*3 chars. What is the first [3] representing?

Or, does your file just represent the first of three levels?

In you input loop, from what I've seen, it appears that you hit the EOF but continue reading the file and still trying to put something in the next cell in your array. In general, when reading from a file (or console), you want to check for EOF.

By the way, is this question for a homework assignment? If so, please let us know, since we are not permitted to provide homework solutions, just some guidance. Thanks.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now