【问题标题】:Read variable length file into 2d char array将可变长度文件读入二维字符数组
【发布时间】:2017-03-26 04:01:31
【问题描述】:

初学者,试图将输入读入二维字符数组。 输入文件格式为:,,,a,a,a,a,,, '\n' ,,,,,,,,,,
需要在 10 x 10 数组中,总是有 9 个逗号并且没有空格,这就是为什么我的 const int 为 20 以防万一有一个“船”占位符占用空间,在字符中读取有问题,当我这样做时阅读它会跳过所有其他字符。 任何帮助,将不胜感激。

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std; 
bool checkship();
string filename; 
ifstream inputfile;
int rowcount= 0, colscount=0;
const int rows=20, cols=20 ; 
char boardarray[rows][cols];

int main()
{
   cout<< "input your battlestation board filename: " << endl;
   cin >> filename;
   inputfile.open(filename.c_str()); 
   while(!inputfile)
   {
      cout << "file did not oipen please retry";
      cin >> filename;
      inputfile.open(filename.c_str());
   }

   while(inputfile)
   {
      for(rowcount=0;rowcount < rows;rowcount++)
      {
         for(colscount=0; colscount < cols; colscount++)
         {
            char ch;
            inputfile >> ch;

            while( ch!= '\n')
            {
               inputfile >>  boardarray[rowcount][colscount];
            }
            if(ch = '\n')
            { rowcount++;
               colscount= 0;
            }
         }                                                                                                                                                                     }  
   }
   inputfile.close();

   for(rowcount=0; rowcount <10 ; rowcount++)
   {
      for(colscount=0; colscount< cols; colscount++)
      {
         cout << boardarray[rowcount][colscount];
         if(colscount == 9)
            cout << endl;
      }
   }

   return 0;
}

【问题讨论】:

标签: c++ arrays


【解决方案1】:

试试这个代码:

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

string filename;
ifstream inputfile;
int rowcount= 0, colscount=0;
const int max_rows=20, max_cols=20 ;
char boardarray[max_rows][max_cols];

int main()
{
   cout<< "input your battlestation board filename: " << endl;
   cin >> filename;
   inputfile.open(filename.c_str(), ios::in | ios::binary);
   while(!inputfile)
   {
      cout << "file did not oipen please retry";
      cin >> filename;
      inputfile.open(filename.c_str(), ios::in | ios::binary);
   }

   while(!inputfile.eof())
   {
        char ch = 0;
        colscount = 0;
        while( ch!= '\n' && !inputfile.eof())
        {
           inputfile.read(&ch, 1);
           if (ch != '\r' && ch!='\n') {
               if (ch != ',') {
                   boardarray[rowcount][colscount] = ch;
               } else {
                   colscount++;
               }
           }
        }
        rowcount++;
        colscount++;
   }
   inputfile.close();

   for(int i=0; i < rowcount; i++)
   {
      for(int j=0; j< colscount; j++)
      {
         cout << boardarray[i][j];
         if(j == (colscount-1))
            cout << endl;
      }
   }
}

【讨论】:

  • 感谢您的帮助,我试一试。卡在这个问题上太久了。 @user3811082
猜你喜欢
  • 1970-01-01
  • 2020-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-17
  • 1970-01-01
  • 1970-01-01
  • 2012-11-19
相关资源
最近更新 更多