C++ 从文本文件创建2d int表格。输出是(可能)内存块
我一直在尝试从文本文件中输入的内容创建一个2d表格。文本文件本身是20x20,仅由“0”和“1”组成,没有任何空格,行之间仅使用enter。这是我的密码:C++ 从文本文件创建2d int表格。输出是(可能)内存块,c++,c,C++,C,我一直在尝试从文本文件中输入的内容创建一个2d表格。文本文件本身是20x20,仅由“0”和“1”组成,没有任何空格,行之间仅使用enter。这是我的密码: #include <iostream> #include <string> #include <fstream> #include <cstdio> #include <cstdlib> using namespace std; int main() { int z;
#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;
}
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
intz;
国际表格[20][20];
//绘制默认地图
文件*pLab;
pLab=fopen(“labirynt.txt”,“r”);
而((z=fgetc(pLab))!=EOF){
如果(z='0'){
cout让我们尝试一个简单得多的例子。(你应该总是从简单开始,逐步发展到复杂。)
labirynt.txt:
1011
在守则中:
int n;
plLab.open("labirynt.txt");
plLab>>n;
cout << n << endl;
很明显,代码不知道您打算只读取文件的一个字符。因此,当您尝试以这种方式读取整个文件时,您读取的内容超过了结尾,并且无法设置表中大多数项的值,因此它们包含随机垃圾
有几种方法可以解决此问题,这是最简单的方法之一:
int n;
char c;
plLab.open("labirynt.txt");
plLab >> c;
n=0;
if(c=='1')
n=1;
cout << n << endl;
intn;
字符c;
plLab.open(“labirynt.txt”);
plLab>>c;
n=0;
如果(c=='1')
n=1;
cout如果您在文本文件中写入不带空格的数字,如下所示:
fout<<1<<0<<1<<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;
}
#包括
#包括
#包括
#定义第20行
#定义COLS 20
int main(){
int tabLab[ROWS][COLS];//您正在使用int,但到目前为止,您只需要存储0或1。。。
//------------读取输入文件,填充2d数组--------------
char-inFileName[]=“迷宫.txt”;
字符c;
标准::ifstream infielab;
infielab.open(inFileName,std::ios_base::in);
如果(!infielab.good()){
STR::CURR,为什么你在几个地方迭代,直到代码21 > /代码>,当你有20x20数组?为什么你要混合C和C++文件库?这个C代码是什么方式?
//inside the read loop
char temp;
plLab>>temp;
tabLab[j][i]= (temp == '0') ? 0 : 1;
#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;
}