将 txt 文件值存储到二维数组中
Store txt file values into 2D array
首先,这是我的代码http://pastebin.com/BxpE7aFA。
现在。我想读取一个如下所示的文本文件 http://pastebin.com/d3PWqSTV
并将所有这些整数放入一个名为 level 的数组中。 level 的大小为 100x25,在顶部声明。
我现在唯一的问题是您在哪里看到 ???。如何从文件中获取字符并将其放入 level[i][j]?
检查矩阵的初始化代码,应该是int level[HEIGHT][WIDTH];
而不是int level[WIDTH][HEIGHT];
还有,你的数据行比WIDTH
短。代码以下一种方式工作:我们遍历水平矩阵的所有行,通过 (file >> row)
指令从文件中读取一行,如果读取成功,则我们将行填充到水平矩阵中,否则我们读取 EOF 所以打破循环。
#include<iostream>
#include<fstream>
#include<string>
#include <limits>
static const int WIDTH = 100;
static const int HEIGHT = 25;
int main()
{
int level[HEIGHT][WIDTH];
for(int i = 0; i < HEIGHT; i++)
{
for(int j = 0; j < WIDTH; j++)
{
level[i][j] = 0;
}
}
std::ifstream file("Load/Level.txt");
for(int i = 0; i < HEIGHT; i++)
{
std::string row;
if (file >> row) {
for (int j = 0; j != std::min<int>(WIDTH, row.length()) ; ++j)
{
level[i][j] = row[j]-0x30;
}
std::cout << row << std::endl;
} else break;
}
return 0;
}
您可以使用 file >> level[i][j];
用 level.txt
的内容填充您的二维字符数组 level[ ][ ]
。
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
static const int WIDTH = 100;
static const int HEIGHT = 25;
char level[HEIGHT][WIDTH]={0};
int main()
{
std::ifstream file;
file.open("level.txt");
if(file.is_open())
{
std::cout << "File Opened successfully!!!. Reading data from file into array" << std::endl;
while(!file.eof())
{
for(int i = 0; i < HEIGHT; i++)
{
for(int j = 0; j < WIDTH; j++)
{
//level[i][j] = ???
file >> level[i][j];
std::cout << level[i][j];
}
std::cout << std::endl;
}
}
}
file.close();
return 0;
}
首先,这是我的代码http://pastebin.com/BxpE7aFA。
现在。我想读取一个如下所示的文本文件 http://pastebin.com/d3PWqSTV 并将所有这些整数放入一个名为 level 的数组中。 level 的大小为 100x25,在顶部声明。
我现在唯一的问题是您在哪里看到 ???。如何从文件中获取字符并将其放入 level[i][j]?
检查矩阵的初始化代码,应该是int level[HEIGHT][WIDTH];
而不是int level[WIDTH][HEIGHT];
还有,你的数据行比WIDTH
短。代码以下一种方式工作:我们遍历水平矩阵的所有行,通过 (file >> row)
指令从文件中读取一行,如果读取成功,则我们将行填充到水平矩阵中,否则我们读取 EOF 所以打破循环。
#include<iostream>
#include<fstream>
#include<string>
#include <limits>
static const int WIDTH = 100;
static const int HEIGHT = 25;
int main()
{
int level[HEIGHT][WIDTH];
for(int i = 0; i < HEIGHT; i++)
{
for(int j = 0; j < WIDTH; j++)
{
level[i][j] = 0;
}
}
std::ifstream file("Load/Level.txt");
for(int i = 0; i < HEIGHT; i++)
{
std::string row;
if (file >> row) {
for (int j = 0; j != std::min<int>(WIDTH, row.length()) ; ++j)
{
level[i][j] = row[j]-0x30;
}
std::cout << row << std::endl;
} else break;
}
return 0;
}
您可以使用 file >> level[i][j];
用 level.txt
的内容填充您的二维字符数组 level[ ][ ]
。
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
static const int WIDTH = 100;
static const int HEIGHT = 25;
char level[HEIGHT][WIDTH]={0};
int main()
{
std::ifstream file;
file.open("level.txt");
if(file.is_open())
{
std::cout << "File Opened successfully!!!. Reading data from file into array" << std::endl;
while(!file.eof())
{
for(int i = 0; i < HEIGHT; i++)
{
for(int j = 0; j < WIDTH; j++)
{
//level[i][j] = ???
file >> level[i][j];
std::cout << level[i][j];
}
std::cout << std::endl;
}
}
}
file.close();
return 0;
}