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.
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.
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.
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()));
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.
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] << " " ;
}
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/.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With