save to file

Discussion in 'C++' started by Eomer, Jan 14, 2006.

  1. Eomer

    Eomer Guest

    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
    Eomer, Jan 14, 2006
    #1
    1. Advertising

  2. Eomer

    TB Guest

    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
    TB, Jan 14, 2006
    #2
    1. Advertising

  3. Eomer

    JustBoo Guest

    On 14 Jan 2006 09:45:14 -0800, "Eomer" <> wrote:
    >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
    JustBoo, Jan 14, 2006
    #3
  4. Eomer

    Eomer Guest

    thanks both of you :D
    Eomer, Jan 14, 2006
    #4
  5. Eomer

    Mike Wahler Guest

    "Eomer" <> wrote in message
    news:...
    > 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?



    <fstream>

    >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
    Mike Wahler, Jan 14, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. user
    Replies:
    1
    Views:
    744
    Bruce Barker
    Apr 4, 2005
  2. teo
    Replies:
    1
    Views:
    447
  3. BusyBoy
    Replies:
    2
    Views:
    578
    =?Utf-8?B?TGVvbiBNYXluZQ==?=
    Oct 31, 2006
  4. subhadip
    Replies:
    0
    Views:
    613
    subhadip
    Mar 28, 2007
  5. Shahar Golan
    Replies:
    5
    Views:
    275
    kaeli
    Oct 16, 2003
Loading...

Share This Page