Taking a file's content (each character) and putting them into a 2D array.

Basically, I want to be able to take the content of a file and put them into a 2D array. Each line would correspond with each row. Problem is, I have no idea how to do this. I don't even know if this is possible.

Right now, all I know is that I have to use get.line

After passing it into an array, I want to be able to check the value of all the arrays, and if they equal to a value, replace them. (Like take all the W from the array and change them to S)

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
 #include <iostream>
#include <fstream>

using namespace std;

void main (void)

//char map_three (char map[20][20])
{

	ifstream inData;
	inData.open ("file1.txt");  //open file

        while (!inData.eof()) { //read from input files
             getline( inData, data);
             //not sure what to do here.




}

	int rows = 20; //create array using pointers and double pointer
	int ** maze = new int * [rows];

	int cols =60;
	for (int r = 0; r < rows; r ++)
	{
		maze[r] = new int [cols];
	}

}
It would help if you show sample file contents, the file format is not obvious from the description.
For example, if you're reading every character from the line, your data structure could be a simple vector of strings:

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

int main()
{
    std::vector<std::string> maze;
    std::ifstream inData("file1.txt");  //open file

    std::string line;
    while ( getline(inData, line) ) //read until eof
        maze.push_back(line);

    // replace every 'W' with 'S'
    for(auto& line: maze)
        std::replace(line.begin(), line.end(), 'W', 'S');

    // output
    for(auto& line: maze)
        std::cout << line << '\n';
}

but if you have, for example, space-separacted characters (e.g ". . . S ." rather than "....S..", then conversion of line into the string to be pushed back would be a little different.
Can't the arrays take in space-bars? I just need each character to go in their seperate array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
W__W____W_____________W____W______W___W________W____________
W__W__WWW_____________W____W__________W________W_______WWWWW
W__W__W_______________W____W______WWW_W____W___________W___W
W__W__W_____WWWWW_____W____W______W_W_WWWWWWWW___W___W_W_W_W
W__WWWW_____W___W_____W____WWW_W_WW_W_____W______W___W_W_W_W
W____________W__W______W___W___W_WWWW_WWW_W______W___W_W_W_W
W____________W__W_______W__W___W______W_W_WWWW_WWWWWWW_W_W_W
W__WWWW_______W_W________W_W_WWW_WWWWWWWW____W_W_______W_W_W
W__W__W_______W_W_________WW_W___W_W_________W_WWWWWWWWW_W_W
___W__W________WW__________W_W__W__W__WWWWWWWW_W_______W_W_W
W__WWWW______________________W__WWWW________W__WWWWWWW_W_W_W
W_____________W____________WWW___W________W_W________W_W_W_W
W_____________WW__________WW___WWW_WWWWWWWW_W_WWWW_WWW_W_W_W
W___WWWWWWWWW_WW_________W_W_W_W_W_W________W____W_W___W_W_W
W___W_______W_W_W_______W__W_W_WWW_WW__WWWW_WWW__W_W___W_W_W
W___W_______W_W_W______W___W________WWWW______W__W_W_____W_W
W___W_WWWW__W_W__W____W____WWW______W____WW__WW__W_WWWWWWW_W
W___W_W__W__W_W__W___W_____W________W____WW______W_________W
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW


This is what I want to pass in, with all the underlines to be changed to spacebars and the Ws changed to another ASCII character. This is a map for a simple game.

And in your code,
for(auto& line: maze)
I don't understand this. I thought for is a loop? And what is auto and &?
Last edited on
That sample I posted works on that input file.
for(auto& line: maze)
I don't understand this. I thought for is a loop?

It is a loop, yes. if your compiler is too old to handle this syntax, the equivalent 1998 loop would be
1
2
for(std::vector<std::string>::iterator i = maze.begin; i != maze.end(); ++i)
        std::replace(i->begin(), i->end(), 'W', 'S');
Topic archived. No new replies allowed.