saving txt

Discussion in 'C++' started by MC felon, Dec 17, 2006.

  1. MC felon

    MC felon Guest

    hi
    how do i save strings to txts and open them back?
    MC felon, Dec 17, 2006
    #1
    1. Advertising

  2. MC felon

    Ivan Novick Guest

    MC felon wrote:
    > hi
    > how do i save strings to txts and open them back?

    // write to file
    #include <iostream>
    #include <fstream>
    int main(int argc, char** argv)
    {
    std::string str1("Hello");
    std::string str2("World");
    std::eek:fstream f("test.txt");
    if (!f)
    {
    std::cerr << "can't open file\n";
    exit(1);
    }
    f << str1 << std::endl;
    f << str2 << std::endl;
    return 0;
    }
    ===============================
    // read from file
    #include <iostream>
    #include <fstream>
    int main(int argc, char** argv)
    {
    std::ifstream f("test.txt");
    if (!f)
    {
    std::cerr << "can't open file\n";
    exit(1);
    }
    std::string str;
    while(f >> str)
    {
    std::cout << str << std::endl;
    }
    return 0;
    }
    ===============================
    You may want to get a C++ reference book.
    ---
    Ivan
    http://www.0x4849.net
    Ivan Novick, Dec 17, 2006
    #2
    1. Advertising

  3. Ivan Novick wrote:
    > MC felon wrote:
    >> hi
    >> how do i save strings to txts and open them back?

    > // write to file
    > #include <iostream>
    > #include <fstream>
    > int main(int argc, char** argv)
    > {
    > std::string str1("Hello");
    > std::string str2("World");
    > std::eek:fstream f("test.txt");
    > if (!f)
    > {
    > std::cerr << "can't open file\n";
    > exit(1);
    > }
    > f << str1 << std::endl;
    > f << str2 << std::endl;
    > return 0;
    > }
    > ===============================
    > // read from file
    > #include <iostream>
    > #include <fstream>
    > int main(int argc, char** argv)
    > {
    > std::ifstream f("test.txt");
    > if (!f)
    > {
    > std::cerr << "can't open file\n";
    > exit(1);
    > }
    > std::string str;
    > while(f >> str)
    > {
    > std::cout << str << std::endl;
    > }
    > return 0;
    > }
    > ===============================
    > You may want to get a C++ reference book.
    > ---
    > Ivan
    > http://www.0x4849.net


    I'd also advise the OP to read the strings back using
    std::getline(), because the >> operator splits the input
    on whitespace.

    HTH,
    - J.
    Jacek Dziedzic, Dec 17, 2006
    #3
    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. Paul Evans

    Saving textbox data as .txt file?

    Paul Evans, Dec 13, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    5,076
    David Wier
    Dec 13, 2004
  2. scrimp
    Replies:
    6
    Views:
    445
    scrimp
    Jun 3, 2005
  3. Sameen
    Replies:
    2
    Views:
    423
    Victor Bazarov
    Aug 29, 2005
  4. Jochen Brenzlinger
    Replies:
    7
    Views:
    5,471
    Roedy Green
    Sep 15, 2011
  5. Clinton Goff

    Opening 2nd browser window and saving as .txt

    Clinton Goff, Aug 27, 2003, in forum: Javascript
    Replies:
    3
    Views:
    109
    Grant Wagner
    Aug 27, 2003
Loading...

Share This Page