ifstream / ofstream question (using libstdc++ v3)

Discussion in 'C++' started by phoenix, Jan 13, 2005.

  1. phoenix

    phoenix Guest

    How can i read a numbers from file.. I created this file like this:

    int dane, i;
    std::eek:fstream plik_z;
    plik_z.open(nazwa, std::ios::eek:ut | std::ios::trunc | std::ios::binary);
    for (i = 0; i < 15; i++) plik_z << dane;
    plik_z.close();


    Is it any way to read from this file all numbers ?.. If so - how can i do
    it.
    If it's impossible - how should i create this file to be able to read a
    numbers from this file?
    I couldn't find any formatted input / output functions in ifstream /
    ofstream.

    phoenix.
    phoenix, Jan 13, 2005
    #1
    1. Advertising

  2. phoenix wrote:
    > How can i read a numbers from file.. I created this file like this:
    >
    > int dane, i;
    > std::eek:fstream plik_z;
    > plik_z.open(nazwa, std::ios::eek:ut | std::ios::trunc | std::ios::binary);
    > for (i = 0; i < 15; i++) plik_z << dane;


    You probably need to put some kind of separator between the numbers
    you output. Use a space, for example:

    ... plik_z << dane << ' ';

    > plik_z.close();
    >
    >
    > Is it any way to read from this file all numbers ?..


    Yes. Open another stream of type 'std::ifstream'. Then use operator >>
    to read the numbers. It will automatically skip the spaces.

    > If so - how can i do
    > it.
    > If it's impossible - how should i create this file to be able to read a
    > numbers from this file?


    See above.

    > I couldn't find any formatted input / output functions in ifstream /
    > ofstream.


    Functions called 'operator<<' and 'operator>>' are for output and input.

    What C++ book are you reading that doesn't explain that?

    Victor
    Victor Bazarov, Jan 13, 2005
    #2
    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. Armando
    Replies:
    2
    Views:
    7,035
    Martijn Lievaart
    Jan 23, 2004
  2. Replies:
    4
    Views:
    6,650
    Victor Bazarov
    Jul 16, 2004
  3. Squid Seven

    ofstream * vs. ofstream

    Squid Seven, Jul 13, 2005, in forum: C++
    Replies:
    5
    Views:
    589
    Ivan Johansen
    Jul 14, 2005
  4. Alexander Dong Back Kim

    how to use libstdc++.so.5 instead of libstdc++.so.6

    Alexander Dong Back Kim, Sep 30, 2008, in forum: C++
    Replies:
    1
    Views:
    767
  5. KevinSimonson
    Replies:
    4
    Views:
    360
    James Kanze
    Nov 4, 2010
Loading...

Share This Page