C ++从txt文件二维数组 [英] C++ 2d array from .txt file

查看:138
本文介绍了C ++从txt文件二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code打印出显示的字符和空格的20×20桌的.txt文件的wacked出的版本。我怎样才能数组正确显示,因为它在.TXT一样。我不能使用矢量或全局变量。它可以在不那些来进行。前两行中的文本是20和20,以获得该阵列的尺寸。

  ifstream的INPUTFILE;
INT boardSizeRow;
INT boardSizeCol;
inputFile.open(fileboard1.txt);
INPUTFILE>> boardSizeRow;
INPUTFILE>> boardSizeCol;
inputFile.get();炭游戏键盘[20] [20];
对于(INT行= 0;&行LT; boardSizeRow;排++)
{
    对于(INT COL = 0;&山坳下,boardSizeCol;西++)
    {
        游戏键盘[行] [山口] = inputFile.get();
    }
}对于(INT行= 0;&行LT; boardSizeRow;排++)////////////// TO测试打印
{
    对于(INT COL = 0;&山坳下,boardSizeCol;西++)
    {
        COUT<<游戏键盘[行] [COL];
    }
    inputFile.get();
    COUT<< ENDL;
}
返回0;
2020WWWWWWWWWWWWWWWWWWWW
  W¯¯GO W W
W¯¯WWW¯¯小号W¯¯
W W GW W W
预激WW摹W¯¯
 WK W W
W W W W W WW¯¯
W¯¯WK W W
W SWüW WW¯¯
                   W¯¯
    W¯¯摹W¯¯
  摹W WW¯¯
ðWWWWWW¯¯
             宽深W¯¯
W W W W W W
    WW W W W W
  摹W W
    WWW¯¯小号W W
   WWW摹W¯¯
WWWWWWWWWWWWWWWWWWWW


解决方案

这就是答案:)
好几个编辑:
输入:


   3
   3
   2 2 3
   2 2 3
   2 2 3

在code:

 的#include<&的fstream GT;
#包括LT&;&iostream的GT;诠释主(){
使用命名空间std;
ifstream的INPUTFILE;
INT boardSizeRow;
INT boardSizeCol;
inputFile.open(fileboard1.txt);
INPUTFILE>> boardSizeRow;
INPUTFILE>> boardSizeCol;字符*游戏键盘=新的char [boardSizeRow * boardSizeCol]。
对于(INT行= 0;&行LT; boardSizeRow;排++)
{
    对于(INT COL = 0;&山坳下,boardSizeCol;西++)
    {
        INPUTFILE>> *(游戏键盘+ boardSizeCol *行+列);
    }
}对于(INT行= 0;&行LT; boardSizeRow;排++)////////////// TO测试打印
{
    对于(INT COL = 0;&山坳下,boardSizeCol;西++)
    {
        COUT<< *(游戏键盘+ boardSizeCol *行+ COL)<< ;
    }
    COUT<< ENDL;
}
删除[]游戏键盘
返回0;

}

My code prints out a wacked out version of a .txt file that displays a 20x20 table of characters and white spaces. How can i get the array to display properly as it does in the .txt. I can not use vectors or global variables. It can be done without those. The first two lines in the text are 20 and 20 to get the dimensions for the array.

ifstream inputFile;
int boardSizeRow;
int boardSizeCol;
inputFile.open("fileboard1.txt");
inputFile >> boardSizeRow;
inputFile >> boardSizeCol;
inputFile.get();

char gameBoard[20][20];
for (int row = 0; row < boardSizeRow; row++)
{
    for (int col = 0; col < boardSizeCol; col++)
    {
        gameBoard[row][col] = inputFile.get();
    }
}

for (int row = 0; row < boardSizeRow; row++) //////////////TO TEST PRINT
{
    for (int col = 0; col < boardSizeCol; col++)
    {
        cout << gameBoard[row][col];
    }
    inputFile.get();
    cout << endl;
}
return 0;


20

20

WWWWWWWWWWWWWWWWWWWW
  W GO  W          W
W WW      w    S   W      
W   W   GW  w      W  
WPW  WW          G W    
 WK       W        W     
W W W  W    w   w  W  
W WK W             W    
W   SW  U    w  w  W
                   W
    w          G   W
  G         w    w W 
D   wwwww          W
             w  D  W
w w   W w   w      W
    ww  w     w w  W
  G        w       W
    ww  w S    w   W
   WWW      G      W
WWWWWWWWWWWWWWWWWWWW

解决方案

This is the answer :) Ok several edits: The input:

   3
   3
   2 2 3 
   2 2 3 
   2 2 3 

The code:

#include <fstream>
#include <iostream>

int main(){
using namespace std;
ifstream inputFile;
int boardSizeRow;
int boardSizeCol;
inputFile.open("fileboard1.txt");
inputFile >> boardSizeRow;
inputFile >> boardSizeCol;

char *gameBoard= new char[boardSizeRow*boardSizeCol];
for (int row = 0; row < boardSizeRow; row++)
{
    for (int col = 0; col < boardSizeCol; col++)
    {   
        inputFile >> *(gameBoard + boardSizeCol * row + col);
    }   
}

for (int row = 0; row < boardSizeRow; row++) //////////////TO TEST PRINT
{
    for (int col = 0; col < boardSizeCol; col++)
    {   
        cout << *(gameBoard + boardSizeCol * row + col) << " ";
    }   
    cout << endl;
}
delete []gameBoard
return 0;

}

这篇关于C ++从txt文件二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆