Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write the content of an array into a text file?

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.

like image 712
Marcus Avatar asked Mar 05 '14 06:03

Marcus


People also ask

Can I write an array to a text file?

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. Show activity on this post. Yes, you can write into a text file.

How to create a text file from an array in Python?

Creating a text file using the in-built open () function and then converting the array into string and writing it into the text file using the write () function. Finally closing the file using close () function. Below are some programs of the this approach: function can be used to save the array into a text file.

How to write a list to a text file?

You can convert your Listto an array then write the array to a textfile double[] myArray = scoreArray.ToArray(); File.WriteAllLines("scores.txt", Array.ConvertAll(myArray, x => x.ToString()));

How to write a multi-dimensional array to a text file using NumPy?

Similarly, as shown below, we can also write multi-dimensional arrays to a text file using the context manager. NumPy is a scientific library that offers a range of functions for working with arrays. We can save an array to a text file using the numpy.savetxt () function.


2 Answers

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] << " " ;
}
like image 69
i'm PosSible Avatar answered Oct 08 '22 21:10

i'm PosSible


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/.

like image 38
Ben Key Avatar answered Oct 08 '22 22:10

Ben Key