Reading some lines with diferent variables

Hi!!

I'm new here and new programming in C++.

I'm doing a program that I need t read from a file some information.
That information consist in a level number and a board information like this:
level-1
000000
000000
001100
001100
000000
000000
level-2
000000
000000
011110
011110
000000
000000


My question is: How can I read a "level" independent of the other information on the file?

I already try with some code on th forum, but didn't solve the problem.

Regards,
Anyone can help on this?
closed account (jyU4izwU)
is This On Note Pad Or In Your Code
If you've alread tried, why don't you post the code, so we can help you fix it?

(As you're new... post your code using code tags, of course)

Andy

How to use code tags
http://www.cplusplus.com/articles/jEywvCM9/
about code tags

How to use tags
http://www.cplusplus.com/articles/z13hAqkS/
about code and other sorts of tag
assuming you want to parse some sort of ini-like file with :

<header>
data data data
...

1) skip lines until you find <header> (or level-n)
2) skip one more line
3) read data
Hi...

Sorry, my mistake... :(

Here you have the code already done...

This was my first:

1
2
3
4
5
6
7
8
9
10
11
12
13
void CFrogsJumpBoard::Read_Level()
{
	int x, y;
    ifstream fich ("Level.txt");
    for(int i=0;i<6;i++){
    	for(int j=0;j<6;j++) {
            //fich >> board[i][j];
            if (board[i][j]==0) {board[i][j]=0;}
            if (board[i][j]==1) {board[i][j]=1;}
            if (board[i][j]==2) {board[i][j]=178;}
    	}
	}
}


Then I try with this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void CFrogsJumpBoard::Read_Level()
 {
	ifstream load ("Level.txt");
	if(!load){cout << "Nao foi possivel carregar o jogo guardado." << endl;}
	load >> Level;
	for(int i=0;i<6;i++){
		for(int j=0;j<6;j++){
			load >> board[i][j];
			cout << board[i][j];
			system ("pause");
		}}
		
	load.close();

}


But in first place, can you tell me if in the file txt i need to give a space under the numbers?

Regards.
Yep, your code will deal with "0 0 1 1 0 0" but not "001100" (i.e. space between the numbers?)

Edit:

If you mean space between the numbers? i.e. "0 0 1 1 0 0" versus "001100", then...

If you're using an int array for your board, and reading into it directly, then yes; you'll need spaces.

If it uses chars, no.

But if you do want to use an int array for some reason, then you could read into a temporary char variable and then use that to set the int array element. This will also handle the no-space case.

(Or you could even read e.g. "001100" into a string and then extract the chars one by one...)

Andy

PS You can do with out the close() at the end; ifstream's destructor will call it for you.
Last edited on
Hi...

It's running now with this code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool FrogJumpTable::load_level(const char *filename)
 {
	string file="Level.txt";
	ifstream load (filename);
	char ch;
    if (load.is_open())
  	{
		load >> level;
		for(int i=0; i<6; i++) 			
                  {for(int j=0; j<6; j++)
	            load >> table [i][j];}	
    load.close();
  }
  else cout << "Unable to open file" << endl;
  return 0;
}


But now I need to increase the file name... (Level1.txt;Level2.txt;Level3.txt;...)

do you know how to do it?
do you mean that you want to use the function with multiple files?
Probably not the most efficient way but maybe try something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <fstream>

int** read_file( const std::string &filename )
{
    const auto size( 6 );
    auto **array = new int*[ size ];

    for( auto i = 0; i < size; ++i )
    {
        array[ i ] = new int[ size ];
    }

    std::ifstream in( filename );
    for( auto i = 0; i < size; ++i )
    {
        for( auto j = 0; j < size; ++j )
        {
            array[ i ][ j ] = in.get() - '0';
        }
    }

    return( array );
}

int main()
{
    auto array = read_file( "Giblit.txt" );
    for( auto i = 0; i < 6; ++i )
    {
        for( auto j = 0; j < 6; ++j )
        {
            std::cout << array[ i ][ j ] << std::endl;
        }
    }

    return( 0 );
}


*slight fix
Last edited on
This may help:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <fstream>


using namespace std;

int main()
{
	ofstream iambored("hello.txt");
	iambored<<"Matthew 14 4";
	iambored.close();
	ifstream ihatemathhomework("hello.txt");
	string dontwannadoit;
	getline(ihatemathhomework,dontwannadoit , '\0');
	string thisismatthew=dontwannadoit.substr(dontwannadoit.find("Matthew"),7);
	cout<<thisismatthew;
        ihatemathhomework.close();


	return 0;
}


I just did that for a guy in another thread
Last edited on
I suggest storing all the information in a string using getline. If this file is huge then have a special delineating character. Use '\0' if you want the whole file. Then use string::find to find where "level-1" is. Use string::find to find where "level-2" is. Use string::substr to save everything inbetween to another string. After that you would go one at a time and search every element and save it to a vector.
Hi... Sorry for the late reply. Thanks all for your help.

I already solve the problem with this code. Could help if someone have a problem like thiss one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
	int nr1, nr2;
	string file ("Level\\Level_");
 	string type (".txt");
 	if (Level<10) nr1=char(48);
 		else nr1=char ((Level/10)+48);
	nr2 = char((Level%10)+48);
	file += nr1;
	file += nr2;
	file += type;
	ifstream in;
	in.open(file.c_str());
	if (in.is_open())
	{
	for(int i=0; i<dim; i++) 			
            {for(int j=0; j<dim; j++)
	            in >> board[j][i];}
	in.close();
	}
	else {
		gotoxy (0,40);
		cout << "Unable to open file" << endl;
		return false;
	}
}


Thanks again for all your help.
Regards
Topic archived. No new replies allowed.