problem with fstream

  • Thread starter Frédéric Manzanares
  • Start date
F

Frédéric Manzanares

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
 
T

tom_usenet

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
 
F

Frédéric Manzanares

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

fred
 
J

Jeff

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;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top