9

How to write the content of an array to a text file? Is it possible?

Below is my code:

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

I would like to print out arrays x, y, k, and j and write them into a text file. The purpose of doing this is to ensure that the data passing is correct.

Melebius
  • 6,183
  • 4
  • 39
  • 52
Marcus
  • 99
  • 1
  • 1
  • 3
  • Well for writing to a text file, [here](http://www.cplusplus.com/doc/tutorial/files/) is a good tutorial/synopsis in regards to C++ Use for formatted output to make it look neat, see [this](http://stackoverflow.com/questions/11226143/formatting-output-in-c) answer for an example. – James H Mar 05 '14 at 06:24

3 Answers3

12

Yes, you can write into a text file.

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

More reference: http://www.cplusplus.com/doc/tutorial/files/

To write array data use this.

for(int count = 0; count < size; count ++){
            out_myfile << x[count] << " " ;
}
Melebius
  • 6,183
  • 4
  • 39
  • 52
i'm PosSible
  • 1,373
  • 2
  • 11
  • 30
2

My suggestion is that you use the JSON (JavaScript Object Notation) format if you want the resulting file to be human readable. JSON is documented at http://json.org/. The ThorsSerializer found at https://github.com/Loki-Astari/ThorsSerializer is a C++ Serialization library for JSON. Serialization, as defined at http://en.wikipedia.org/wiki/Serialization, "is the process of translating data structures or object state into a format that can be stored (for example, in a file or memory buffer, or transmitted across a network connection link) and reconstructed later in the same or another computer environment." If ThorsSerializer does not work for you, I suggest that you do a Google search for "C++ JSON Serialization library."

Another option I found when doing this Google search is "cereal - A C++11 library for serialization" found at http://uscilab.github.io/cereal/.

Benilda Key
  • 2,836
  • 1
  • 22
  • 34
  • 2
    Using JSON should be considered when saving and loading arrays in an application but it is IMHO an overkill for dumping arrays for debugging purposes. – Melebius Jan 23 '18 at 07:40
1

Use fstream class for operations like file write, read, append, seek, etc..

Praful Surve
  • 788
  • 1
  • 10
  • 22