# Output the data into a file and view the data

+1 vote

Hello Everyone,
I tried to output the data into a file and learned the writeToFile,
but I do not how to show them.
For example

## include "itensor/all.h"

using namespace itensor;

int
main()
{
int N = 100; //number of sites
SpinHalf sites(N); //SpinHalf is a subclass of SiteSet
MPS psi(sites);
writeToFile("sitesfile",sites);
writeToFile("psi
file",psi);
}

When vi psi_file in the folder, I get :

^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@ø^?^A^@^@^@^@^@^@^@^B^@^@^@^@^@^@^@^@^@^@^@Site^@^@^@H4<9c>ó^B^@^@^@S=1/2

I would like to list my own data in three columns : i , j , result
Thanks for any help
Wz

Hi, so the writeToFile function is only for the purpose of writing ITensor objects as binary data. So the odd output you're seeing is raw binary reinterpreted by your text editor as ascii data (even though it's not ascii data).

If you want to write data in ascii to a file, I'd recommend writing a loop over the data you want to save and inside the loop, use the printfln function. Here is an example:

auto i = Index("i",3);
auto j = Index("j",3);
auto k = Index("k",3);

auto T = ITensor(i,j,k);
randomize(T);

PrintData(T);

std::ofstream of("of.dat");
for(auto ni : range1(i.m()))
for(auto nj : range1(j.m()))
for(auto nk : range1(k.m()))
{
printfln(of,"%d %d %d %.12f",ni,nj,nk,T.real(i(ni),j(nj),k(nk)));
}
of.close();

commented by (130 points)
Hello Miles
Thank you very much for your nice example of showing how to output the data.
I ran the code and it worked very well.
Thank you again for solving the problem.
Wz
commented by (20.8k points)
Great. If it's too slow (such as for a very large tensor) then let me know and I can suggest some code that would be faster.

Another simpler way to see the contents of a tensor if you don't mind some additional output showing the tensor indices etc is to do this:
std::ofstream of("of.dat");
printfln(of,"T = %f",T);
of.close();

The %f formatting option prints both the tensor information and all of the non-zero entries. It uses a faster and more generic algorithm that works for a tensor of any rank.