我一直在尝试根据文本文件中的内容创建一个2d表。文本文件本身为20x20,仅包含' 0' 0和' 1'没有任何空格,只在行之间使用enter。这是我的代码:
#include <iostream>
#include <string>
#include <fstream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main() {
int z;
int tabLab[20][20];
//Drawing default map
FILE * pLab;
pLab = fopen ("labirynt.txt", "r");
while((z = fgetc(pLab)) != EOF) {
if(z == '0') {
cout << (char)219 << (char)219;
} else if (z == '1') {
cout << " " ;
} else if (z=='\n') {
cout << endl;
}
//Saving default map to 2d table
}
ifstream plLab;
plLab.open("labirynt.txt");
if(plLab.good()==true)
{
for(int j = 0; j < 21; j++)
{
for(int i = 0; i < 20; i++)
{
plLab>>tabLab[j][i];
}
}
}
cout << endl;
//Checking if table has been created correctly
for (int k = 0; k < 21; k++) {
for(int l = 0; l < 21; l++) {
cout << tabLab[k][l];
}
cout << endl;
}
}
我一直在努力解决输出问题,这个数字随机数字很大,但到目前为止我还没有设法修复它。
答案 0 :(得分:1)
让我们尝试一个更简单的案例。 (你应该始终从简单开始,然后建立复杂。)
labirynt.txt:
1011
在代码中:
int n;
plLab.open("labirynt.txt");
plLab>>n;
cout << n << endl;
这会产生:
1011
显然,代码不知道您打算只读取文件的一个字符。因此,当您尝试以这种方式读取整个文件时,您会读取结束并且无法设置表中大多数条目的值,因此它们包含随机垃圾。
有几种方法可以解决这个问题,这是最简单的方法之一:
int n;
char c;
plLab.open("labirynt.txt");
plLab >> c;
n=0;
if(c=='1')
n=1;
cout << n << endl;
从此可以构建表填充。
答案 1 :(得分:0)
如果您将没有空格的数字写入文本文件,如下所示:
fout<<1<<0<<1<<0
当你把它读到int时,你将获得整个1010,因为字符串流读取直到第一个空格并且它将是行尾。
您可以通过多种方式解决它:例如,通过char:之后的读取char:
//inside the read loop
char temp;
plLab>>temp;
tabLab[j][i]= (temp == '0') ? 0 : 1;
答案 2 :(得分:0)
您的代码存在一些问题,但最重要的是它缺少输入数据的错误检查,并且您在循环中使用了错误的边界。 看看这个:
#include <iostream>
#include <fstream>
#include <string>
#define ROWS 20
#define COLS 20
int main() {
int tabLab[ROWS][COLS]; // You are using int, but till now you have to store only 0 or 1...
// ---------- Read the input file, populating the 2d array --------------
char inFileName[] = "labyrinth.txt";
char c;
std::ifstream inFileLab;
inFileLab.open(inFileName, std::ios_base::in);
if ( !inFileLab.good() ) {
std::cerr << "Error: I can't open the file " << inFileName << std::endl;
exit(1);
}
for ( int i = 0; i < ROWS; i++) {
for ( int j = 0; j < COLS; j++ ) {
if ( !inFileLab.get(c) ) {
std::cerr << "Error: I can't read element (" << i + 1 << ", " << j + 1 << ") in file " << inFileName << std::endl;
exit(2);
}
if ( c == '0' )
tabLab[i][j] = 0;
else if ( c == '1' )
tabLab[i][j] = 1;
else {
std::cerr << "Error: Wrong value read for element (" << i + 1 << ", " << j + 1 << ") in file " << inFileName << std::endl;
exit(3); // You can choose to set table[i][j] = 0 or 1 and continue;
}
}
if ( i < ROWS - 1) { // if the last row doesn't end with a newline it's not a big deal.
if ( !inFileLab.get(c) ) {
std::cerr << "Error: Abnormal line termination at row " << i + 1 << " in file " << inFileName << std::endl;
exit(4);
}
if ( c != '\n') {
std::cerr << "Error: Line " << i + 1 << " is too long in file " << inFileName << std::endl;
exit(5);
}
}
} // The file can be bigger, I just ignore the extra data
inFileLab.close(); // and close the file (that could be tested too)
// --------------------- Show the 2d array -------------------------------
const char block = 219;
for ( int i = 0; i < ROWS; i++) {
for ( int j = 0; j < COLS; j++ ) { // Like in your program, if t[i][j] == 1 you print space, blocks otherwise
if ( tabLab[i][j] ) std::cout << " "; // Imho, the opposite would be better...
else std::cout << block << block;
}
std::cout << std::endl;
}
// -------------------- Save the 2d array in another file -------------------
char outFileName[] = "labyrinth_copy.txt";
std::ofstream outFileLab;
outFileLab.open(outFileName, std::ios_base::out);
if ( !outFileLab.good() ) {
std::cerr << "Error: I can't open the output file " << outFileName << std::endl;
exit(5);
}
for ( int i = 0; i < ROWS ; i++) {
for ( int j = 0; j < COLS; j++ ) {
outFileLab << tabLab[i][j];
if ( !outFileLab.good() ) {
std::cerr << "Error: I can't save element (" << i + 1 << ", " << j + 1 << ") in file " << outFileName << std::endl;
exit(6);
}
}
outFileLab << std::endl;
if ( !outFileLab.good() ) {
std::cerr << "Error: I could save only " << i << "rows out of " << ROWS << " in file " << outFileName << std::endl;
exit(7);
}
} // The file is closed automatically when execution goes out of scope
// --------------------------------- Everything went fine ---------------------------
return 0;
}
您可以进行更多改进。