save to file

E

Eomer

How do i save an int value or an array to a .txt file?
which file to include? what is the function? what is the syntax? what
are the common problems?

Thanks in advance,
Eomer
 
T

TB

Eomer sade:
How do i save an int value or an array to a .txt file?
which file to include? what is the function? what is the syntax? what
are the common problems?

Thanks in advance,
Eomer

I recommend buying a book covering basic io/file operations.
Or the internet has many tutorials on C++ IO operations.
<fstream> has everything you need, well almost. Observe:

//---------------------------------------------------------------------------
#include <ostream>
#include <fstream>
#include <iterator>
#include <algorithms>
#include <vector>
//---------------------------------------------------------------------------

int main(int argc, char* argv[])
{
// Save and restore single integer
{
std::eek:fstream out("persistency.txt");
out << 7;
out.close();

int i = 0;
std::ifstream in("persistency.txt");
in >> i;
in.close();
std::cout << i << std::endl;
}

// Save and restore integer vector
{
std::vector<int> v;
std::generate_n(std::back_inserter(v),10,std::rand);

// Open file
std::eek:fstream out("array-persistency.txt");
// Copy v to file
std::copy(v.begin(),v.end(),std::eek:stream_iterator<int>(out," "));
out.close();
// Copy v to std::cout
std::copy(v.begin(),v.end(),
std::eek:stream_iterator<int>(std::cout," "));
v.clear();
std::endl(std::cout);

std::ifstream in("array-persistency.txt");
// Copy file to v
std::copy(std::istream_iterator<int>(in),
std::istream_iterator<int>(), std::back_inserter(v));
// Copy v to std::cout
std::copy(v.begin(),v.end(),
std::eek:stream_iterator<int>(std::cout," "));
in.close();
}
return 0;
}

TB
 
J

JustBoo

How do i save an int value or an array to a .txt file?
which file to include? what is the function? what is the syntax? what
are the common problems?

The munificent if not magnificent "iostreams"

http://www.parashift.com/c++-faq-lite/input-output.html

Reveals all. Well, not all, but it's a good place to start. :)

Also Google with this (including quotes):

"C++" iostreams example code

41,500 hits. Go in 2-4 pages to get to the good stuff.

Good Luck
 
M

Mike Wahler

Eomer said:
How do i save an int value or an array to a .txt file?

Use a type 'std::eek:fstream' object.
which file to include?


what is the function?

Depending upon whether you want to store the binary
image of the integer object or its text form, either
'ofstream::write()' or 'std::eek:perator<<(std::eek:stream&, int&)'
what is the syntax?

Same as for invoking any other function.
what
are the common problems?

Trying to write C++ programs without having studied
textbooks, documentation, etc., or practicing.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top