Edit : Broken from this thread. http://www.daniweb.com/software-development/cpp/threads/35587/175186#post175186

Hello. I got the same problem. But i need to read the string table from a file. I must use a 3d table? The table in the file must look like this:

A	B	C	D	E
A	QX	FN	LB	YE	HJ
B	AS	EZ	BN	RD	CO
C	PD	RA	MG	LU	OP
…	…	…	…	…	…

I declared the table as: char table[27][27][2]; it's length must be 27 rows and lines. Any ideas how can i read it from a text file? I am a beginner at C++ and use borlandC or visual c++.

Recommended Answers

All 2 Replies

That depends on how you want to write it within the file. If you write it the same way it appears then may be you can use getline() function to get an entire line and then process it by using the space as delimiters to get your values. Or you can add a special delimiter like '|' between terms to keep them separate and then process it the same way as mentioned above.

Edit : Broken from this thread. http://www.daniweb.com/software-development/cpp/threads/35587/175186#post175186

Hello. I got the same problem. But i need to read the string table from a file. I must use a 3d table? The table in the file must look like this:

A	B	C	D	E
A	QX	FN	LB	YE	HJ
B	AS	EZ	BN	RD	CO
C	PD	RA	MG	LU	OP
…	…	…	…	…	…

I declared the table as: char table[27][27][2]; it's length must be 27 rows and lines. Any ideas how can i read it from a text file? I am a beginner at C++ and use borlandC or visual c++.

You have two options... the first one is to find a way to skip all the white spaces and when you reach a non-space, you put that in your array. To do that you would have something like this:

char a = ' ';
for (unsigned int i = 0; i < 27; ++i){
  for (unsigned int j = 0; j < 27; ++j){
    while (a == ' ' || a == '\n') fin.get(a); //skips over new line characters too '\n'
    array[i][j][0] = a;
    fin.get(a)
    array[i][j][1] = a;
  }
}

The other option is to use the extraction operator, which skips over spaces for you. But since you're using a char array instead of a string, the extraction operator will automatically put a null terminating character at the end of each extraction to your array, meaning your array would have to look like this: char array[27][27][3], where the 3rd character would be to hold the null terminating character.

The code would look something like this:

char array[27][27][3];
for (int i = 0; i < 27; ++i){
  for (int j = 0; j < 27; ++j){
    fin.width(3);//note that, when >> is used on a char*, this will cause it to extract only 2 characters and put 1 null character.
    fin >> array[i][j][0];
  }
}
]

Note that I didn't include in either version a check to see if end of file is reached, which you need to put in somewhere. Also, I'm notorious for making a shit ton of errors in my code and not proofreading, so use this as a guideline only. In fact I already see one error hehe
Greywolf

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.