将文本文件读入二维数组C ++

时间:2017-11-26 21:58:31

标签: arrays file

void intializeBoard(char matrix[][arrSize]) {
    char cc = '0';
    char rc = '1';

    for (int r = 0; r < arrSize; r++) {
        for (int c = 0; c < arrSize; c++) {
            if (r == 0) {
                matrix1[r][c] = cc++;
            }
            else if (c == 0) {
                matrix1[r][c] = rc++;
            }
            else {
                matrix1[r][c] = ' ';
            }
        }
    }
    matrix1[0][0] = ' ';

    char inputChar;
    ifstream input;

    input.open("project3-allmoves.txt");

    while(!input.eof()){
        for(int row = 1; row < arrSize; row++){
            for(int col = 1; col < arrSize; col++){
                inputChar = input.get();
                matrix1[row][col] = inputChar;
            }
        }
        input.close();
    }
}

我正在尝试将字符文件读入2d数组,但是当我调用此函数时它不会输出任何内容。数组大小为10,我在第0列和第0行填充数字从1到9,并将[0] [0]留空。这就是文本文件的样子:

PPCCPCCCP
PPCPCCCPP
PCCPCCCPP
CCPCCPCPC
PCPPPCCCP
PPPPPPCCP
CCCPCPCCP
PCPPCCPPP
PPCCCCPPC

代码:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <iomanip>
#include <fstream>

#include "project.h"

using namespace std;

//Function Prototypes
void intializeBoard(char matrix1[][arrSize]);

int main() {
    char board[arrSize][arrSize];
    bool end = false;
    bool gameFinished = false;
    int playersScore = 0;
    int computersScore = 0;
    string move;
    //    srand(time(0));

    displayMessage(1, move);

    intializeBoard(board);

    drawBoard(board);

This is a picture of what the board looks like  数组大小为10,我在第0列和第0行填充数字从1到9,并将[0] [0]留空。

0 个答案:

没有答案