Tabulation in a .txt !!

Guys, I hope you can help me. I need to do a program, in which I create the sin function, but that's not the point... How can I tabulate the results in a .txt file? I mean, how do I create a table that contains my results?
Well, first you'd have to create a .txt file with the following statement:
ofstream myFile("OutputData.txt");

From there, you can use myFile as you would std::cout, including all of the stream manipulators, such as \t and \n for tab and newline, respectively.

So you could do something like

myFile << dataX << '\t' << dataY << '\n';

Which would write two piece of data, separated by a tab into your output file and start a new line.

Is that what you were looking for?

EDIT: Source: http://www.cplusplus.com/doc/tutorial/files/
Last edited on
Thanks Aaron for help! I haven't tested the code, but yes, I'm familiar with how we can write inside a .txt file. But, could you write a code that builds a table, in which column 1 is for one type of data, the column 2 for a different type and the 3rd for another type?

I mean, how can we tabulate data? o.O

** What I'm asked to do is a program that gets the results of sin(x) and cos(x) if x is between -2*PI and 2*PI with a step of 0,001. That means:

X[0] = -2*PI
X[1] = -2*PI + 0,001
X[2] = X[1] + 0,001
.
.
.
(and so on)

I'm already having problems generating the numbers...

int definicionElementosX (int j, double PI) {
double elementos[1000];
if (yaHecho == "si") {
return elementos[j];
}
else {
elementos[1] = -2*PI;
i = 2;
do {
elementos[i] = elementos[1] + i * 0,001;
i ++;
} while (elementos[i] <= 2*PI);
yaHecho = "si";
return elementos[1];
}
}

That's the function I created and when I use it:

int j = 1; double X[1000];
do {
X[j] = definicionElementosX (j, PI);
cout << "X["<<j<<"] = " << setprecision(4) << X[j] << endl;
j ++;
} while (X[j] <= 2*PI);

I'm not getting the results I want! :( The results are 0! And some are in scientific terms...
Last edited on
What Aaron Vienneau said should work, unless you are wanting to draw the contents of an array to a txt in a tabular form?
Yes, I want that. But now that I just tested the code Aaron Vienneau provided, I realize that it's easier if we don't include some kind of bars that separate the results. But is it possible to separate the results in a tabular form and can you check the code I left in my previous answer?
I think I know my mistake: I defined the function as int, instead of double! :D
If you're Looking to draw the results to a table in a .txt file, you'd have to use ASCII art.

Something like,

.#######################.
|___Cell 1__|___Cell 2__|___Cell 3__|
########################
|___Cell 4__|___Cell 5__|___Cell 6__|
########################
|___Cell 7__|___Cell 8__|___Cell 9__|
########################

As plain text files cannot contain graphics of any kind. But this would be terribly time-consuming for very little benefit. Not to mention, it would make reading the file back into a program, an absolute nightmare. Alternatively, you could look into LibXL which apperantly is a C++ library that allows you to read and write .xlsx files; which are, by nature, tabulated.
Thanks for your help Aaron Vienneau! But I figured a code that can tabulate some results a little bit easier, using .fill() stringstream ss .str() and .precision

Topic archived. No new replies allowed.