从文本文件中读取空白

Reading white spaces from a text file

本文关键字:读取 空白 文件 文本      更新时间:2023-10-16

我试图从某个文件中读取空白字符并将它们存储在数组位置,但是当我运行代码时,我一直得到这个线程:线程1 exc_bad_access (code=exc_i386_gpflt)。我不知道我哪里做错了。使用Xcode 6.4 btw.

#include <iostream>
#include <string>
#include<fstream>
#include <ctype.h>
using namespace std;
const int COLS = 8;
const int ROWS = 8;
//Function Prototypes
void displayTop();
void displayNumColumn();
void displayNumRow();
int displayMenu();
void displayBoard();
char displayHelp();
void playGame(char player, int plB, int plW);
void goToMenu(char x);
int saveFile(char x, char board[][COLS], char player, int plB, int plW);
void loadGame();
void clearBoard();
//Global Variables
int num = 1;
char board [COLS][ROWS] = {
    ' ',' ',' ',' ',' ',' ',' ',' ',
    ' ',' ',' ',' ',' ',' ',' ',' ',
    ' ',' ',' ',' ',' ',' ',' ',' ',
    ' ',' ',' ','W','B',' ',' ',' ',
    ' ',' ',' ','B','W',' ',' ',' ',
    ' ',' ',' ',' ',' ',' ',' ',' ',
    ' ',' ',' ',' ',' ',' ',' ',' ',
    ' ',' ',' ',' ',' ',' ',' ',' ',
};
int main() {
    int choice = displayMenu();
    switch (choice) {
        case 1:
            system("clear");
            clearBoard();
            displayBoard();
            break;
        case 2:
            loadGame();
            break;
        case 3:
            displayHelp();
            break;
        case 4:
            return 0;
            break;
        default:
            cout << "Please enter a valid choice." << endl;
            break;
    }
    playGame('B', 2, 2);
}
//Function to draw and display the board
void displayBoard(){
    displayTop();
    num = 1;
    for (int row = 0; row < ROWS; row++){
        displayNumRow();
        cout << "   |";
        for (int column = 0; column < COLS; column++){
            cout << board[row][column] << "   |";
        }
        cout << endl;
        displayTop();
    }
    displayNumColumn();
}
// Function to display a horizontal line
void displayTop(){
    cout << "    ";
    for (int i = 0; i < ROWS; i++){
        cout << "+----";
    }
    cout << endl;
}
//Function to display the alphabets column
void displayNumColumn(){
    cout << "   ";
    for( int i = 1; i <= COLS; i++ ) {
        cout << "    " << i ;
    }
}
//Function to display the numbers row
void displayNumRow(){
    if (num <= ROWS) {
        cout << num;
        num++;
    }
}
// Function to display the menu
int displayMenu(){
    int answer = 0;
    cout << "Othellonn"
    << "1.New Gamen2.Load Gamen3.Helpn4.QuitnYour Choice: ";
    cin >> answer;
    system("clear");
    return answer;
}
// Function to display the help window
char displayHelp(){
    char answer = ' ';
    cout << "How to play OthellonnThe object of the game is to have the majority of your colour discs on the board at the end of the game.nnInput the cell where you want to place your disc in the form of (rowNumber columnNumber) without the bracket and includng the space.nnThe one with the most discs wins!!!!nnSo, are you ready to play? (y or n)nnYour Choice: ";
    cin >> answer;
    if (answer == 'y')
        displayBoard();
    else if (answer == 'n')
        displayHelp();
    else
        cout << "Invalid input" << endl << endl;;
    displayHelp();
    return answer;
}
// Function to act as the game engine
void playGame(char player, int plB, int plW){
    char x = 0;
    int y = 0;
    // Infinite for loop to keep switching between players and asking for moves
    for(;;){
        cout << "nnScore: W = " << plW << " B = " << plB;
        cout << "nPlayer: " << player;
        cout << "nPlease make your move : ";
        cin >> x;
        cin >> y;
        cout << endl;
        goToMenu(x);
        int xi = x -'0';
        // If condition to check for the validity of the input
        if (xi <= ROWS && y <= COLS && board[xi-1][y-1] == ' ' && cin.good()) {
            board[xi-1][y-1] = player;
            displayBoard();
        } else {
            cin.clear();
            cin.ignore(100,'n');
            if (saveFile(x, board, player, plB, plW)) {
                cout << "Game Saved" << endl;
                if (player == 'B') {
                    plB--;
                } else {
                    plW--;
                }
            } else {
                cout << "Invalid Input" << endl;
                if (player == 'B') {
                    plB--;
                } else {
                    plW--;
                }
            }
        }
        // If condition to always change the players and add one to their score
        if (player == 'B') {
            plB++;
            player = 'W';
        } else {
            plW++;
            player = 'B';
        }
    }
}
void goToMenu(char x){
    if (x == 'm') {
        main();
    }
}
int saveFile(char x, char board[][COLS], char player, int plB, int plW){
    ofstream savedGame("savedGame.txt");
    if (x == 's') {
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLS; j++) {
                savedGame << board[i][j];
            }
        }
        savedGame << player;
        savedGame << plB;
        savedGame << plW;
    }else {
        return 0;
    }
    return 1;
}
//Again the same thread: thread 1 exc_bad_access (code=exc_i386_gpflt)
void loadGame(){
    char player;
    char plB;
    char plW;
    int blck;
    int wht;
    ifstream savedGame("savedGame.txt");
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            savedGame >> noskipws;
            savedGame >> board[i][j];
        }
    }
    savedGame >> player;
    savedGame >> plW;
    savedGame >> plB;
    blck = plB - '0';
    wht = plW - '0';
    displayBoard();
    playGame(player, blck, wht);
}
void clearBoard(){
    for (int i = 0; i <= ROWS; i++) {
        for (int j = 0; j <= COLS; j++) {
            board[i][j] = ' ';
        }
    }
    board[3][3] = 'W';
    board[3][4] = 'B';
    board[4][3] = 'B';
    board[4][4] = 'W';
}

你的问题是在for循环上,所有的循环都有"i <= ROWS"所以它会超出数组所以它会抛出异常

for (int i = 0; i < ROWS; i++) 

你完成了