C++ 将网格写入文件

C++ 将网格写入文件,c++,C++,我正在编写代码,读取一组数字,然后在屏幕上以4x4网格显示它们,然后程序将确定它是否是幻方。我的问题是如何让用户将数字输入到readData方法中显示的文件中,以便在调用该方法时显示用户网格 此外,data.txt文件存储在哪里 谢谢 #include<iostream> #include<fstream> using namespace std; const int dimension = 4; // dimension for the array typedef

我正在编写代码,读取一组数字,然后在屏幕上以4x4网格显示它们,然后程序将确定它是否是幻方。我的问题是如何让用户将数字输入到readData方法中显示的文件中,以便在调用该方法时显示用户网格

此外,data.txt文件存储在哪里

谢谢

#include<iostream>
#include<fstream>
using namespace std;
const int dimension = 4;   // dimension for the array
typedef int Sq[dimension] [dimension];  // declare vector for type names

void ReadData(Sq square ) // read data from file
{  ifstream inFile;
  char fileName[13] = "data.txt";
  inFile.open (fileName);  // open file for reading                
  for (int r = 0; r < dimension; r++)   // row loop
      for ( int c = 0; c < dimension; c++)  // column loop
            inFile >> square[r][c]; // read data into matrix
      inFile.close( ); // close the file                
}

void Display ( const Sq square ) // display matrix
{ cout << " Magic Square Program " << endl << endl;
 for (int r = 0; r < dimension; r++)
   { for ( int c = 0; c < dimension; c++)
        { cout.width(6);  //set output width to 6 characters
           cout << square[r][c] << "\t ";  // display numbers
            }
       cout << endl;
    }    
 }
bool magicSquare( Sq square)  // add rows, columns, and diagonals
{ int firstSum = 0, sum;
  bool magic = true;
  for (int r = 0; r < dimension; r++) // add 1st column for a comparison
      firstSum += square[r][1]; 
   for (int r = 1; r < dimension; r++) // row loop first when adding rows
      { sum = 0;
         for ( int c = 0; c < dimension; c++)
             sum += square[r][c];  // add row
         if ( sum != firstSum)  // check for magic failure
              return (false);  // not magic
       }

     for ( int c = 0; c < dimension; c++)   // column loop first when adding columns
       { sum = 0;
          for (int r = 0; r < dimension; r++)
              sum += square[r][c];   // add columns
              if ( sum != firstSum)  // check for magic failure
               return (false);  // not magic
        }
     sum = 0;
     for (int r = 0; r < dimension; r++)  
          sum += square[r][r];   // add front diagonal
        if ( sum != firstSum)  // check for magic failure
           return (false);  // not magic
      sum = 0;
      for (int r = 0; r < dimension; r++)  
          sum += square[r][dimension - r - 1];   // add back diagonal
         if ( sum != firstSum)  // check for magic failure
          return (false);  // not magic
      else
          return (true);
      }  // end magicSquare function

int main( )
{
  Sq square;
 ReadData( square);  // read data from file
 Display ( square);  // display matrix
 if ( magicSquare(square) )   // check for magic property
    cout << "\n This Square is Magic \n " << endl;
 else
    cout << "\n This Square is Not Magic \n " << endl;
 system("Pause");
 return(0);
 }
#包括
#包括
使用名称空间std;
常量int维度=4;//数组的维度
typedef int Sq[维度][维度];//为类型名声明向量
void ReadData(Sq square)//从文件中读取数据
{ife;
字符文件名[13]=“data.txt”;
infle.open(fileName);//打开文件进行读取
for(int r=0;r>方形[r][c];//将数据读入矩阵
infle.close();//关闭文件
}
无效显示(常数平方)//显示矩阵
{cout
我的问题是如何让用户将数字输入到readData方法中显示的文件中

礼貌地问?因为它是磁盘上的一个文件,用户必须在程序启动之前填充它。如果文件不存在,您可以使用适当的
ifstream
标志not创建文件,并显示错误消息,然后终止程序

此外,data.txt文件存储在哪里

在二进制文件的当前执行路径中。这可能是也可能不是二进制文件所在的文件夹


作为一个旁注,使用C++,<>代码> STD::数组可能比C样式数组更适合。

最简单的方法是让程序把文件名作为命令行参数。main应该是这样的,在这里,<代码> ARC> <代码>是参数的数目,而<>代码> ARGV[]/COD>是数组和指向它们的字符指针。(

argv[0]
始终是可执行文件的名称)。请参阅

那么,你知道了吗

if (argc == 2)
{
   ReadData(square, argv[1]);
   ...
}
else
  ...
ReadData将如下所示:-

void ReadData(Sq &square, const std::string &filename) // read data from file
{
  ifstream inFile;
  inFile.open (filename);  // open file for reading   

注意!您需要将square作为参考参数(
&square
),否则您的输入数据就会被忽略。

代码太多了,我的眼睛受伤了。您能把它缩小一点吗?
void ReadData(Sq &square, const std::string &filename) // read data from file
{
  ifstream inFile;
  inFile.open (filename);  // open file for reading