C ++:2D char数组文件无法正确读取

时间:2013-10-30 05:24:50

标签: c++ arrays xcode char

当程序首次创建.dat文件时,它会创建#的图表就好了。但是在创建.dat之后从文件中读取时,会将#打印为颠倒的问号(在Xcode中)。

我认为我的“else if”语句不是将文件读入数组,但我不确定如何修改它。

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

using namespace std;

void loadSeats(char[][30]);
void displaySeatingChart(char [][30], float []);


const int columns = 30;
const int rows = 15;
char map[rows][columns];

int main()
{
    float sales[15];

    loadSeats(map);
    displaySeatingChart(map, sales);

    return 0;
}

void loadSeats(char map[][30])
{
    ifstream inFile;
    inFile.open("seatingChart.dat");

    if(!inFile)
    {
       ofstream outFile;
       outFile.open("seatingChart.dat");
       for (int i=0; i<15; i++)
       {
            for (int j=0; j<30; j++)
            {
                map[i][j]= '#';
                outFile << map[i][j];
            }
        }
        outFile.close();
    }
    else if(inFile)
    {
        inFile >> map[rows][columns];
        cout << "File Found . . ." << endl;
        cout << "Loading Seating Chart . . ." << endl;
    }
}

void displaySeatingChart(char [][30], float [])
{
    cout << "\t\t\tSeats" << endl;
    cout << "         123456789012345678901234567890" << right << setw(10) << "Price\n";

    for (int count = 0; count < 15; count++)
    {
        cout << "Row " << left << setw(2) << (count + 1) << "   ";

        for (int count2 = 0; count2 < 30; count2++)
        {
            cout << "" <<  map [count] [count2];
        }
       cout << right << setw(9) << "15.00" << endl;
    }
    cout << endl;
}

0 个答案:

没有答案