473,511 Members | 16,252 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with fstream

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


Jul 22 '05 #1
3 2259
On Fri, 28 Nov 2003 11:07:30 +0100, "Frédéric Manzanares"
<fm**@gmx.net> 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::out| 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::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
Jul 22 '05 #2
thank you tom.
The purpose is the operator >> For read a completly line in one string
variable.
<< isn't need...

fred

"tom_usenet" <to********@hotmail.com> schrieb im Newsbeitrag
news:39********************************@4ax.com...
On Fri, 28 Nov 2003 11:07:30 +0100, "Frédéric Manzanares"
<fm**@gmx.net> wrote:
hello,

my problem: I want to habe one Class with write and read in a file. i haveoverloaded
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::out| 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::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

Jul 22 '05 #3
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;
}
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3203
by: David Briggs | last post by:
I am using MS VC++ 6.0 with MFC I have a simple class: #include <fstream.h> class Data { public: CString WriteStr(); Data();
3
584
by: David Blasdell | last post by:
This appears very strange to me, I'm having a problem with a fstream object inside a class which is not being called directly from main (another class calls the class that contains the fstream...
2
2465
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { ...
3
3051
by: slarti | last post by:
My VS6 version used #include <fstream.h> which for VS.net I had to change to #include <fstream> using namespace std; Now I get a compiler error :
0
1605
by: JackRazz | last post by:
I'm trying to serialize a collection to a file stream by serializing each object individually. The code below works fine with the BinaryFormatter, but the SoapFormatter reads the first object and...
5
1372
by: Simon Verona | last post by:
I have a pair of functions that I'm calling using remoting - called readfile and writefile. The readfile works fine when called from the client PC. The writefile method returns an error: ...
5
2028
by: znubbe | last post by:
Hi, I hope anyone can help me with this problem. I have a field of image type in a SQL 2000 database. I'm using this code to insert a document: Dim conn Dim rs Dim oStream
5
3934
by: ehui928 | last post by:
The following program is used to open a file in mode both read and append, but it has a problem that the file can't be opened. #include <iostream> #include <fstream> #include <cstdlib> ...
2
1722
by: Rene | last post by:
Hello to all! I am a newbee to C++, I did a beginners' course, and now I am stuck with a strange problem. I have written the function that is pasted downwards. The peculiar thing is that when...
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7353
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7418
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7508
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3212
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.