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

标签 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++;

我想打印出数组xykj并将它们写入一个文本文件。 这样做的目的是确保数据传递正确。

最佳答案

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

#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;
}

更多引用: http://www.cplusplus.com/doc/tutorial/files/

要写入数组数据,请使用它。

for(int count = 0; count < size; count ++){
            out_myfile << x[count] << " " ;
}

关于c++ - 如何将数组的内容写入文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22190048/

相关文章:

javascript:带有对象的公共(public)数组

c# - 双数组的排序列表

java - 在类路径上检测到 log4j-over-slf4j.jar 和 slf4j-log4j12.jar,抢占 StackOverflowError

c++ - HP-UX Itanium 比较和交换

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

c++ - 为什么在 QtCreator 的 qMake 项目需要 CMake?

java - 调试日志记录不适用于特定处理程序

python - django.request 记录器没有传播到根?

c++ - 将函数作为参数传递给 C++ 中的方法

c++ - 具有std::function的std::shared_ptr作为自定义删除器和分配器