This question already has answers here:
How to output array of doubles to hard drive?
(6 answers)
Closed 6 years ago.
Here is the snippet of code that prints the output
void PrintFunc(double Y[])
{
for(int i=0 ; i<=N+1 ; i++)
{
cout << x_min+i*r << "\t" << Y[i] << endl;
}
}
I want to write it to a text file, how do I do it? i ranges from 1 to 2000.
#include <fstream>
std::ofstream ofs("filename.txt")
// then, in the loop:
ofs << ... << std::endl;
Related
This question already has answers here:
C++ alignment when printing cout <<
(7 answers)
How can I easily format my data table in C++?
(8 answers)
Align columns output c++
(2 answers)
Closed 5 months ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I am looking for a simple CPP solution to align a string to the right size.
What do I mean by this?
here is an example:
the normal output would be
hello [message]
foo [message]
I would like it to output like this:
hello [message]
foo [message]
note: this is not for aligning a table but rather to know how to align a message by adding certain spaces to the left (or smth).
Like this ?
#include <iostream>
#include <iomanip>
int main()
{
std::cout << std::setw(10) << "Hello" << " [message]" << std::endl ;
std::cout << std::setw(10) << "foo" << " [message]" << std::endl ;
}
In the library, you can find std:right and std:setw which can help to justify O/P to the right.
If 'i' is your string, then below is a snippet that you can use :-
#include <iomanip>
cout << right
<< setw(20)
<< fixed
<< i << endl;
This question already has answers here:
std::vector::resize() vs. std::vector::reserve()
(5 answers)
Why doesn't this vector assignment work?
(1 answer)
Why aren't these vectors equal?
(1 answer)
Closed 4 years ago.
Snippet1: The following snippet prints out 0 1 but returns an empty vector.
vector<int> trial() {
vector<int> ret;
ret.reserve(2);
ret[0] = 0;
ret[1] = 1;
cout << ret[0] << " " << ret[1] << "\n";
return ret;
}
Snippet 2: The following snippet prints out 0 1 and returns a vector contains {0,1}.
vector<int> trial() {
vector<int> ret;
ret.push_back(0);
ret.push_back(1);
cout << ret[0] << " " << ret[1] << "\n";
return ret;
}
Why doesn't snippet 1 work like snippet 2. If I am reserving memory and adding values to the vector.
reserve() does not change the vector size, what you want is resize() it.
This question already has answers here:
No console output on cout
(7 answers)
Closed 5 years ago.
Is this possible to print Point value?
For the example, I have a Point like this:
Point coordinate = Point(150,300);
And I want to show its value. I tried several ways, like:
first:
cout << coordinate << "\n";
second:
cout << coordinate.x << coordinate.y << "\n";
I also try the suggestion to flush it, become:
std::cout << coordinate << std::endl;
But none of those are work in my case. Is there any suggestion? Thanks for your help.
Ps. I work with opencv 3 and c++
std::cout << coordinate.x << "," << coordinate.y << std::endl;
This question already has answers here:
std::vector::resize() vs. std::vector::reserve()
(5 answers)
Closed 6 years ago.
I was unable to make std::copy works. I tried several codes in internet, but I was unable to make it work. I need to use copy, first, to understand why is not working, second, to use it in a constructor of a class(I can't declare a new variable in the constructor).
I have this code:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> myVector;
vector<int> vectorCopy;
for (int i = 0; i < 10; i++) {
myVector.push_back(i);
}
cout << vectorCopy.size() << endl; //0
vectorCopy.reserve(myVector.size());
cout << vectorCopy.size() << endl; //PROBLEM: again 0
copy(myVector.begin(), myVector.end(), vectorCopy.begin());
cout << vectorCopy.size() << endl; //PROBLEM 2: again 0
for (int j = 0; j < vectorCopy.size(); j++) {
cout << vectorCopy[j] << endl;//never execute;
}
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
Any ideas? I'm using CLION on MacOS Sierra and C++11.
Thanks!!
You need to pass an output iterator as the third parameter to std::copy(). Usually you'll use something like a std::back_inserter.
This question already has answers here:
Print leading zeros with C++ output operator?
(6 answers)
Closed 8 years ago.
I need to use some integer values as a part of some file names I create as outputs in a c++ program. For this I use the code above. The thing is my int values ( named n_irrad in the code) goes from 1 to 20000 for example, so I need the file names to be MC_Interface00001.txt, MC_Interface00002.txt, ..., MC_Interface20000.txt. So, ¿how can I set the number of digits in the file name? With the code I'm using I obviously get MC_Interface1.txt, MC_Interface2.txt, ..., MC_Interface20000.txt.
Thanks in advance.
ofstream MC_Interface;
string Irrad = static_cast<ostringstream*>( &(ostringstream() << n_irrad) )->str();
string MC_Interface_FileName = "MC_Interface" + Irrad + ".txt";
MC_Interface.open(MC_Interface_FileName.c_str());
Try the following
#include <iostream>
#include <iomanip>
#include <sstream>
int main()
{
std::ostringstream os;
os << std::setw( 5 ) << std::setfill( '0' ) << 10;
std::string s = "MC_Interface" + os.str();
std::cout << s << std::endl;
return 0;
}
The output is
MC_Interface00010
ofstream MC_Interface;
std::stringstream ss("MC_Interface");
ss << std::setw(5) << std::setfill('0') << n_irrad;
ss << ".txt";
MC_Interface.open(ss.str());