[英]How to read a matrix from text file and store it into 2D array?
I am a beginner at this. 我是初学者。 I'm trying to read a file and put it into a 2D array.
我正在尝试读取文件并将其放入2D数组。 Here is my code.
这是我的代码。 after it outputs the file it displays the garbage in the memory and the loop never ends unless it hits 50.
输出文件后,它会在内存中显示垃圾,除非循环达到50,否则循环永远不会结束。
include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
void main()
{
char arr[50][50];
ifstream fin;
fin.open("Map.txt");
for (int i = 0; i < 50; i++)
{
for ( j = 0; j < 50; j++)
{
fin.get(arr[i][j]);
}
}
for (int i = 0; arr[i]!=NULL; i++)
{
for (int j = 0; arr[j]!=NULL; j++)
{
cout<< arr[i][j];
}
}
}
The text file looks like this 文本文件如下所示
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@ @@
@@ @@
@@ @@
@@ ^ @@
@@ @@
@@ @@
@@ @@
@@@@@@@@@@@@@@@@ @@
@@ @@
@@@@@@@@@@@@@@@@ @@
@@ @@
@@ x x @@
@@ @@
@@ o @@
@@ @@
@@ o @@
@@ @@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
I think something like this works 我认为像这样的作品
#include <iostream>
#include <fstream>
int main() {
const int nSize = 50;
//-- initialize array with 0 --
char map[nSize][nSize] = { {0} };
std::ifstream in;
in.open("input.txt");
int i = 0, j = 0;
while (!in.eof()) {
char next = in.get();
if (next != '\n')
map[i][j++] = next;
else {
j = 0;
i++;
}
}
int rowsCount = i + 1;
for (i = 0; i < rowsCount; i++) {
j = 0;
while (map[i][j] != 0)
std::cout << map[i][j++];
std::cout << std::endl;
}
return 0;
}
All lines of text ends with "end line symbol" '\\n' or '\\r\\n'. 文本的所有行均以“结束行符号”“ \\ n”或“ \\ r \\ n”结尾。 This can signalize to go to new row of chars in array.
这可以表示要转到数组中的新字符行。 Since array was initialized with 0, we can use it as flag of end of row in output, but better would be calculate size of array while reading it (if all lines have same size).
由于数组已初始化为0,因此我们可以将其用作输出中行尾的标志,但最好是在读取数组时计算数组的大小(如果所有行的大小都相同)。
Try this, but make sure the input matrix in Map.txt is 50*50 characters for sure, otherwise you may receive undetermined results. 尝试执行此操作,但是请确保Map.txt中的输入矩阵确定为50 * 50个字符,否则可能会收到不确定的结果。 (
include "stdafx.h"
is missing, because I use g++ instead of MS Visual Studio, but you may add this include for precompiled headers, if you created your project the way VS needs it) (因为我使用g ++而不是MS Visual Studio,所以缺少了
include "stdafx.h"
,但是如果您以VS需要的方式创建了项目,则可以为预编译头添加此包含)
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const unsigned int HEIGHT = 50;
const unsigned int WIDTH = 50;
int main()
{
char arr[HEIGHT][WIDTH];
ifstream fin;
fin.open("Map.txt");
string line;
//let's assume here the proper size of input Map
for(unsigned int i = 0; i < HEIGHT; i++)
{
getline(fin, line);
for(unsigned int j = 0; j < WIDTH; j++)
{
arr[i][j] = line[j];
}
}
//let's assume here the proper size of input Map
for (int i = 0; i < HEIGHT; i++)
{
for ( int j = 0; j < WIDTH; j++)
{
cout << (char)arr[i][j];
}
cout << endl;
}
}
If you are trying to do what I think you are, try this: 如果您要尝试做我认为的事情,请尝试以下操作:
include "stdafx.h"
include <iostream>
include <fstream>
using namespace std;
void main()
{
char arr[50][50];
ifstream fin;
fin.open("Map.txt");
for (int i = 0; i < 50; i++)
{
for ( int j = 0; j < 50; j++)
{
fin.get(arr[i][j]);
}
}
for (int i = 0; arr[i]!=NULL; i++)
{
for (int j = 0; arr[j]!=NULL; j++)
{
cout<< arr[i][j];
}
}
}
the data inside the text file is : 文本文件中的数据为:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@ @@
@@ @@
@@ @@
@@ ^ @@
@@ @@
@@ @@
@@ @@
@@@@@@@@@@@@@@@@ @@
@@ @@
@@@@@@@@@@@@@@@@ @@
@@ @@
@@ x x @@
@@ @@
@@ o @@
@@ @@
@@ o @@
@@ @@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.