• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 269
  • Last Modified:

I have an array m_plambda, how do I write it to a .txt or .csv file?

I provided a sample code
post.txt
0
barca_ing
Asked:
barca_ing
  • 6
  • 4
1 Solution
 
maartennieberCommented:
Hi,

you can use std::ofstream for that (include fstream).

http://msdn.microsoft.com/en-us/library/7z434859.aspx

std::ofstream ofs("numbers.txt");
ofs << 12.34; // writes 12.34 to the stream
ofs << ","; // writes a comma, etc (just loop over the numbers to write them all)
ofs << std::endl; // write end-of-line character(s)

Note that you must use ofs.good() to check that the stream was opened properly, and call ofs.close() when you are done.
Also note that std::ofstream has a function 'precision' for setting the precision (number of decimals to write), For a double, the maximum precision is 16 decimals (for some reason, you should set the precision to 17 to actually get the maximum). The precision function returns the previous precision, and you can use this function to restore the precision when you have written the numbers to the file.

By the way, I would use std::vector<double> to store the numbers instead of an array (it takes care of the memory management for you and offers high-level functions).
To write the numbers to the file, I would create a small helper function.

0
 
evilrixSenior Software Engineer (Avast)Commented:
^^^Just a small note, the rules of CVS are more complex that just putting a comma between each value. If you really do want CSV format then I suggest you look at this for the full rules.

http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm

Below is an example of dumping the array as a simple text file with each value of a separate line. Note that this would work even if you replaced the fixed size array with a vector (rather than passing the start and end address of the array to the 1st 2 params in std::copy you's just pass the iterators returned by begin() and end()).
#include <cassert>
#include <algorithm>
#include <fstream>
 
int main(int argc, char * argv[])
{
	char const *example[] = {
		"hello world",
		"hello mum",
		"hello dad"
	};
 
	size_t example_len = sizeof(example)/sizeof(example[0]);
 
	std::ofstream ofs("c:\\temp\\example.txt");
 
	if(ofs)
	{
		std::copy(example, example+example_len, std::ostream_iterator<char const *>(ofs, "\n"));
	}
	else
	{
		assert("Oops" && false);
	}
}

Open in new window

0
 
evilrixSenior Software Engineer (Avast)Commented:
Just spotted your post.txt file hidden in your question :)

My example, modified to suit...
#include <cassert>
#include <algorithm>
#include <fstream>
 
int main(int argc, char * argv[])
{
	double example[] = {
		1.1,
		2.1,
		3.1
	};
 
	size_t example_len = sizeof(example)/sizeof(example[0]);
 
	std::ofstream ofs("c:\\temp\\example.txt");
 
	if(ofs)
	{
		char delim[] = "\n"; // This can be whatever you like
		std::copy(example, example+example_len, std::ostream_iterator<double>(ofs, delim));
	}
	else
	{
		assert("Oops" && false);
	}
}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
barca_ingAuthor Commented:
hey evilrix,

Thank you for the code you have save me a lot of time.  Just one thing in your array "example" you are defining the array.  What if the array "example" has already been filled up with data, how do you copy the data in that array into a .txt file?

Thanks!
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> What if the array "example" has already been filled up with data
The process would be identical... regardless of how the array is created or populated there is no difference when it comes to copying it to a file.
0
 
barca_ingAuthor Commented:
how could I implement that code you provided in a .h file that gets called by a cpp file?

That code snippet that I provided is a portion of the .h file.  
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> how could I implement that code you provided in a .h file that gets called by a cpp file?
Exactly the same as if it was in the .cpp file. I'm not sure I fully understand your question ot qhy you think the fact it's in a header makes any difference.

BTW: Why is the code going into a header?
0
 
barca_ingAuthor Commented:
The .cpp file just launches a form.  The header file contains all the code that gets initialized if a start, stop, etc button are clicked on within the form.  The variable m_pLambda gets filled up after start button is clicked.  I just need to write it to a file now.  
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> The .cpp file just launches a form.
Oh, ok so I take it this is managed C++? I hate how Microsoft decided to put all code in headers... it is such bad practice.

>>   I just need to write it to a file now.  
Well, I take it you're comfortable with how to do this in a .cpp file? Just because it's in a header makes it no different. if you are still unsure please post the full function code here with a comment as to where you want to persist the data and I'll see if I can add it in for you... but please do try yourself because you won't learn if you don't try. I am happy to guide you if you want to give it a go.
0
 
barca_ingAuthor Commented:
Thank you, I figured it out!
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> Thank you, I figured it out!
No problem... well done you :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now