How do you read in characters from a string opened file and store them in 2D array? In particular, it should read ROWS lines, | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do you read in characters from a string opened file and store them in 2D array? In particular, it should read ROWS lines,

#include <iostream> #include <fstream> #include <iomanip> #include <string> #include <cstdlib> using namespace std; // define variables const int ROWS = 20; const int COLUMNS = 60; const unsigned int EMPTY = 0, ROCKY = 1, ROUGH = 2, WALL = 3, PLAYER_START = 4, PLAYER_GOAL = 5, MONSTER_GOAL = 6, ATTACKER_START = 7, DRONE_START = 8; typedef unsigned int Level[ROWS][COLUMNS];// creating a new type void loadlevel(Level level[][COLUMNS], string filename);// function prototype int main() { Level nums1[ROWS][COLUMNS];// declaring array string filename1;// declaring string cout << "Please input a filename: "; cin >> filename1;// input filename loadlevel(nums1, filename1);// recall return 0; } void loadlevel(Level level[][COLUMNS], string filename) { ifstream inputdata; inputdata.open(filename.c_str()); for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLUMNS; j++) { // read the file opened and store the characters in the 2D array } } }

18th Jan 2019, 1:50 AM
Eviolet Rider
Eviolet Rider - avatar
3 Answers
+ 1
If the file contains data as a matrix with the elements in each row separated by spaces and rows separated by newlines, then you can simply read then using the >> operator like cin. for(int i=0;i<ROWS;i++) for(int j=0;j<COLUMNS;j++) inputdata>>level[i][j]; In case the data is stored in a different way, then you will have to read every row as a string and then retrieve data from it. You can use stringstream for this. Usage will depend on the storage format of the data in the file. Secondly, there is an error in your code. The typedef statement assigned Level as a 2D-array of unsigned ints (int[ROWS][COLUMNS]). Thus you do not have to use [] after the variable name, as Level l1 is a 2D array in itself, but Level l1[][COLUMNS] is a 4D array, which is probably not what you intended it to be.
18th Jan 2019, 2:46 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 1
Thank you for answering😊😊😊...I m really new to programming but could u help out a little bit more of possible? when you said i create a 4D array how do i create a a 2D array with a new type Level corresponding to unsigned int with rows and coloumns as dimensions. do i just write typedef unsigned int Level (int [ROWS] [COLUMNS]) Also the file is in a matrix so cin>>inputdata can be used to read the file right?
18th Jan 2019, 3:44 AM
Eviolet Rider
Eviolet Rider - avatar
0
Eviolet Rider When you use the typedef statement: typedef int Level[ROWS][COLUMNS]; The compiler stores the name Level as an alias for the type int[ROWS][COLUMNS]. Thus to declare a level you just need to do: Level L1; // Now L1 is a 2D-array/ matrix. There is no need to specify the dimensions again. If you do, like this: Level L1[ROWS][COLUMNS]; This translates to: unsigned int L1[ROWS][COLUMNS][ROWS][COLUMNS]; Which is a matrix of Levels. So use the former for your case. As for this statement: cin>>inputdata This is incorrect as inputdata is a stream just like cin. You can do either: cin>>L1[i][j]; Or: inputdata>>L1[i][j]; Note that the former in this case reads from the console, and not from the file you mentioned. So what you need for your code is the latter.
18th Jan 2019, 8:40 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar