Connecting Tech Pros Worldwide Forums | Help | Site Map

problem with fstream

Frédéric Manzanares
Guest
 
Posts: n/a
#1: Jul 22 '05
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::out| 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







tom_usenet
Guest
 
Posts: n/a
#2: Jul 22 '05

re: problem with fstream


On Fri, 28 Nov 2003 11:07:30 +0100, "Frédéric Manzanares"
<fm29@gmx.net> wrote:
[color=blue]
>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[/color]

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.
[color=blue]
>{
>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::out| ios::app );
> oFile<<"hey baby!";
> oFile>>s;[/color]

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

fstream oFile("file_sample.txt",ios::in|ios::out|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
Guest
 
Posts: n/a
#3: Jul 22 '05

re: problem with fstream


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

fred

"tom_usenet" <tom_usenet@hotmail.com> schrieb im Newsbeitrag
news:39oesv8tpcuiim1c2bqh21e2v9m0gqai6c@4ax.com...[color=blue]
> On Fri, 28 Nov 2003 11:07:30 +0100, "Frédéric Manzanares"
> <fm29@gmx.net> wrote:
>[color=green]
> >hello,
> >
> >my problem: I want to habe one Class with write and read in a file. i[/color][/color]
have[color=blue][color=green]
> >overloaded
> >the operator >> and <<.
> >
> >class c_File[/color]
>
> 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.
>[color=green]
> >{
> >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::out| ios::app );
> > oFile<<"hey baby!";
> > oFile>>s;[/color]
>
> The read and write position are shared for fstream. You need to do:
>
> fstream oFile("file_sample.txt",ios::in|ios::out|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[/color]


Jeff
Guest
 
Posts: n/a
#4: Jul 22 '05

re: problem with fstream


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::ofstream.

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::operator >> (
std::istream& in,
std::string& s )
{
std::getline( in, s );

return in;
}
Closed Thread