problem with fstream

Discussion in 'C++' started by Frédéric Manzanares, Nov 28, 2003.

  1. hello,

    my problem: I want to habe one Class with write and read in a file. i have
    overloaded
    the operator >> and <<.

    class c_File
    {
    public :
    fstream fs;
    ......
    public :
    ....
    friend void operator >> (c_File& ,string& );
    friend void operator << (c_File& ,const char * );
    ....
    }
    void operator <<(c_File& myFile, const char* out )
    {
    myFile.fs<<out;
    }
    void operator >>(c_File& myFile, string& out)
    {
    int c;
    string s;
    getline(myFile.fs,s);
    out=s;
    }

    main.C
    {
    c_File oFile;
    string s;
    oFile.OpenFile("file_sample.txt",ios::in|ios::eek:ut|ios::app );
    oFile<<"hey baby!";
    oFile>>s;
    cout<<s<<endl;
    }

    The result is :
    The file was empty!!

    in the file " hey baby"
    but the "cout" do nothing !!

    i have tried with seekp/seekg(0,ios::end), flush and sync ... but it don't
    do better.

    i can't see the "s" variable. There is nothing in it

    i use xlC vers. 6 and aix. 5.2
    have you any idea ??
    thank you a lot for your help

    Frédéric
     
    Frédéric Manzanares, Nov 28, 2003
    #1
    1. Advertising

  2. Frédéric Manzanares

    tom_usenet Guest

    On Fri, 28 Nov 2003 11:07:30 +0100, "Frédéric Manzanares"
    <> wrote:

    >hello,
    >
    >my problem: I want to habe one Class with write and read in a file. i have
    >overloaded
    >the operator >> and <<.
    >
    >class c_File


    What is the purpose of c_File? What does it offer over std::fstream?
    There may be a better solution than defining a new class that is
    unrelated to fstream.

    >{
    >public :
    > fstream fs;
    >.....
    > public :
    >...
    >friend void operator >> (c_File& ,string& );
    >friend void operator << (c_File& ,const char * );
    >...
    >}
    >void operator <<(c_File& myFile, const char* out )
    >{
    > myFile.fs<<out;
    >}
    >void operator >>(c_File& myFile, string& out)
    >{
    >int c;
    >string s;
    > getline(myFile.fs,s);
    > out=s;
    >}
    >
    > main.C
    >{
    > c_File oFile;
    > string s;
    > oFile.OpenFile("file_sample.txt",ios::in|ios::eek:ut|ios::app );
    > oFile<<"hey baby!";
    > oFile>>s;


    The read and write position are shared for fstream. You need to do:

    fstream oFile("file_sample.txt",ios::in|ios::eek:ut|ios::app );
    string s;
    oFile<<"hey baby!";
    oFile.seekg(0, ios::beg); //seek back to start.
    oFile>>s;

    Tom

    C++ FAQ: http://www.parashift.com/c -faq-lite/
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
     
    tom_usenet, Nov 28, 2003
    #2
    1. Advertising

  3. thank you tom.
    The purpose is the operator >> For read a completly line in one string
    variable.
    << isn't need...

    fred

    "tom_usenet" <> schrieb im Newsbeitrag
    news:...
    > On Fri, 28 Nov 2003 11:07:30 +0100, "Frédéric Manzanares"
    > <> wrote:
    >
    > >hello,
    > >
    > >my problem: I want to habe one Class with write and read in a file. i

    have
    > >overloaded
    > >the operator >> and <<.
    > >
    > >class c_File

    >
    > What is the purpose of c_File? What does it offer over std::fstream?
    > There may be a better solution than defining a new class that is
    > unrelated to fstream.
    >
    > >{
    > >public :
    > > fstream fs;
    > >.....
    > > public :
    > >...
    > >friend void operator >> (c_File& ,string& );
    > >friend void operator << (c_File& ,const char * );
    > >...
    > >}
    > >void operator <<(c_File& myFile, const char* out )
    > >{
    > > myFile.fs<<out;
    > >}
    > >void operator >>(c_File& myFile, string& out)
    > >{
    > >int c;
    > >string s;
    > > getline(myFile.fs,s);
    > > out=s;
    > >}
    > >
    > > main.C
    > >{
    > > c_File oFile;
    > > string s;
    > > oFile.OpenFile("file_sample.txt",ios::in|ios::eek:ut|ios::app );
    > > oFile<<"hey baby!";
    > > oFile>>s;

    >
    > The read and write position are shared for fstream. You need to do:
    >
    > fstream oFile("file_sample.txt",ios::in|ios::eek:ut|ios::app );
    > string s;
    > oFile<<"hey baby!";
    > oFile.seekg(0, ios::beg); //seek back to start.
    > oFile>>s;
    >
    > Tom
    >
    > C++ FAQ: http://www.parashift.com/c -faq-lite/
    > C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
     
    Frédéric Manzanares, Nov 28, 2003
    #3
  4. Frédéric Manzanares

    Jeff Guest

    Frédéric,

    To clarify:

    You want the >> operator to read one line at a time.

    To achieve this, you are implementing your own File class, wrapping
    std::eek:fstream.

    Is this correct? If so, I'd like to offer some alternatives.

    1) Use std::getline directly.

    2) If you really want generic syntax for reading arbitrarily
    formatted input, write a new input iterator along the lines of
    the std::istream_iterator template.

    3) Overload the extraction operator (>>) within the scope where you
    want to change its behavior. For example, below is some working
    code.

    Good luck,
    Jeff

    #include <iostream>
    #include <string>

    /* Use this namespace to make the extraction operator (>>)
    * read whole lines from input streams, rather than stopping
    * at whitespace.
    */
    namespace Extract_Whole_Lines
    {
    /* Extract the next line from an input stream, and
    * store the line in a string. Return a reference to
    * the input stream.
    */
    std::istream& operator >> (
    std::istream&,
    std::string& );
    }

    int main( int argc, char** argv )
    {
    using namespace Extract_Whole_Lines;

    std::string s;

    std::cin >> s; // Read one whole line.
    }

    std::istream& Extract_Whole_Lines::eek:perator >> (
    std::istream& in,
    std::string& s )
    {
    std::getline( in, s );

    return in;
    }
     
    Jeff, Nov 29, 2003
    #4
    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. las

    fstream problem

    las, Jul 16, 2003, in forum: C++
    Replies:
    1
    Views:
    437
    Simon Saunders
    Jul 16, 2003
  2. jbruno4000

    Fstream problem

    jbruno4000, Nov 2, 2003, in forum: C++
    Replies:
    5
    Views:
    464
    =?ISO-8859-1?Q?Christian_Brechb=FChler?=
    Nov 3, 2003
  3. Brandon McCombs
    Replies:
    8
    Views:
    484
    Dave O'Hearn
    Nov 24, 2003
  4. Armando
    Replies:
    6
    Views:
    749
    Armando
    Jan 29, 2004
  5. Mike - EMAIL IGNORED

    problem opening fstream

    Mike - EMAIL IGNORED, Feb 6, 2004, in forum: C++
    Replies:
    14
    Views:
    1,620
    Mike - EMAIL IGNORED
    Feb 8, 2004
Loading...

Share This Page