如何将数组的内容写入文本文件?

Mar*_*cus 5 c++ arrays logging

如何将数组的内容写入文本文件?可能吗?

以下是我的代码:

x=0;
y=0;
//copy to real array
if(nRow == 0){
    for(i=nTCol; i>=0 ; i--){
        nPanelMap[nRow][x] = nTempMap[i];
        x++;
    }

}
if(nRow == 1){
    for (i=nTCol; i>=0 ; i--){
        nPanelMap[nRow][y] = nTempMap[i];
        y++;
    }
}
k=0;

for (i=nTCol; i>=0 ; i--){
    array [k] = nPanelMap[nRow][x];
    k++;
    array [k] = nPanelMap[nRow][y];
    k++;

}
j=0;
for (i=nTCol; i>=0 ; i--){

    nPanelMap[nRow][j] = array [k];
    j++;
}
nRow++;
Run Code Online (Sandbox Code Playgroud)

我想打印出数组x,y,k,和j,并写入到一个文本文件中.这样做的目的是确保数据传递正确.

i'm*_*ble 7

是的,您可以写入文本文件.

#include <iostream>
#include <fstream>
using namespace std;

int main () {
  const int size = 5;
  double x[] = {1,2,3,4,5}; 

  ofstream myfile ("example.txt");
  if (myfile.is_open())
  {
    myfile << "This is a line.\n";
    myfile << "This is another line.\n";
    for(int count = 0; count < size; count ++){
        myfile << x[count] << " " ;
    }
    myfile.close();
  }
  else cout << "Unable to open file";
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

更多参考: http ://www.cplusplus.com/doc/tutorial/files/

要编写数组数据,请使用此方法

for(int count = 0; count < size; count ++){
            out_myfile << x[count] << " " ;
}
Run Code Online (Sandbox Code Playgroud)