Hello,
I need to a simple example for writing class' objects to a text file using fstream , I tried to search the forums and I found binary examples but not for to a text file. Could you help me please ?
Regards
Kevin S.
8 1497
You should be able to use the insertion operator<<. fstream knows whether the file is text or binary. All you have to do is get your data into the stream.
Here is the my code, is it possible to correct the code ? I recieve related class showit has no member for related letter.
Regards
Kevin S. - #include<iostream>
-
#include<fstream>
-
#include <cstdlib>
-
#include <math.h>
-
#include <conio.h>
-
using namespace std;
-
-
class showit {
-
int numberx;
-
public:
-
void assignx(int a){
-
numberx=a;
-
}
-
int xprint(){
-
return numberx;
-
}
-
}k,e,v,i,n;
-
int main(){
-
k.assignx(1);
-
e.assignx(2);
-
v.assignx(3);
-
i.assignx(4);
-
n.assignx(5);
-
cout << k.xprint() << endl;
-
cout << e.xprint() << endl;
-
cout << v.xprint() << endl;
-
cout << i.xprint() << endl;
-
cout << n.xprint() << endl;
-
system("pause");
-
}
-
class filex
-
{
-
private :
-
showit infox;
-
void writeToFile()
-
{
-
ofstream yprint("newFile.txt", ios::app);
-
yprint << infox.k << endl;
-
yprint << infox.e << endl;
-
yprint << infox.v << endl;
-
yprint << infox.i << endl;
-
yprint << infox.n << endl;
-
yprint.close();
-
}
-
}
OK. I fixed your code.
Let's take this in pieces. Here's the new main(): - int main(){
-
showit k,e,v,i,n;
-
k.assignx(1);
-
e.assignx(2);
-
v.assignx(3);
-
i.assignx(4);
-
n.assignx(5);
-
-
filex obj;
-
obj.writeToFile(k);
-
obj.writeToFile(e);
-
obj.writeToFile(v);
-
obj.writeToFile(i);
-
obj.writeToFile(n);
-
-
cout << k.xprint() << endl;
-
cout << e.xprint() << endl;
-
cout << v.xprint() << endl;
-
cout << i.xprint() << endl;
-
cout << n.xprint() << endl;
-
system("pause");
-
}
The first thing is some objects of type showit are created. These objects are k,e,v,i,n.
Next, the objects are assigned a value.
Next a filex object s created.
Next, each showit object is written to file.
And you are done.
To make this work, the filex class now looks like: - class filex
-
{
-
private :
-
showit infox;
-
ofstream theFile;
-
-
public:
-
filex();
-
~filex();
-
void writeToFile(showit& arg);
-
-
};
-
-
-
filex::filex()
-
{
-
this->theFile.open ("newFile.txt", ios::app);
-
}
-
filex::~filex()
-
{
-
this->theFile.close();
-
}
-
void filex::writeToFile(showit& arg)
-
{
-
theFile << arg.xprint() << endl;
-
-
}
Here you see the class has a constructor to open the file and a destructor to close the file. Then there is the writeToFile method which writes the showit object passed in as an argument to the file.
So this code:
has created the filex object and has called the filex constructor which as opened the file.
The filex destructor will be called at the end of the program by the compiler without you needing to code anything.
So, what dot you think?
Thank you for your interest but it is not working. When I try to run, I receive following error message.
"filex" undeclared (first use this function)
used code is below as you suggested - #include<iostream>
-
#include<fstream>
-
#include <cstdlib>
-
#include <math.h>
-
#include <conio.h>
-
using namespace std;
-
-
class showit {
-
int numberx;
-
public:
-
void assignx(int a){
-
numberx=a;
-
}
-
int xprint(){
-
return numberx;
-
}
-
}k,e,v,i,n;
-
int main(){
-
showit k,e,v,i,n;
-
k.assignx(10);
-
e.assignx(20);
-
v.assignx(30);
-
i.assignx(40);
-
n.assignx(50);
-
-
filex obj;
-
obj.writeToFile(k);
-
obj.writeToFile(e);
-
obj.writeToFile(v);
-
obj.writeToFile(i);
-
obj.writeToFile(n);
-
-
cout << k.xprint() << endl;
-
cout << e.xprint() << endl;
-
cout << v.xprint() << endl;
-
cout << i.xprint() << endl;
-
cout << n.xprint() << endl;
-
system("pause");
-
}
-
-
class filex
-
{
-
private :
-
showit infox;
-
ofstream theFile;
-
-
public:
-
filex();
-
~filex();
-
void writeToFile(showit& arg);
-
};
-
filex::filex()
-
{
-
this->theFile.open ("newFile.txt", ios::app);
-
}
-
filex::~filex()
-
{
-
this->theFile.close();
-
}
-
void filex::writeToFile(showit& arg)
-
{
-
theFile << arg.xprint() << endl;
-
-
}
-
Regards
Demir
if I disable line 26 to 31 the code working but off douse it is not writing any thing.
Regards
Demir
I mean that of course:) sorry
The compiler needs to know about filex before you use it. Therefore, you can't put the filex class after main().
Put the class between the showit class and the start of main().
I did that with your code plus removing k,e,v,i,n from the end of the showit class definition. I compiled the code, and verified that, in fact, it wrote to the disc file just fine.
it is worked after move class filex between showit and start of main. I will try to read the file same way.
thank you so much :)
Demir
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Tino |
last post by:
Hi,
Is it possible to have a php script that creates a text file on it's
host server... something like a print to file or write to file?
What I want to do is have a php script that creates a...
|
by: burdeen |
last post by:
I can't find any way of writing a unicode, or UTF-8 format text file.
Right now i have a Unicode string that i write to the text file and
the unicode characters are replaced with ANSI question...
|
by: NewToPHP |
last post by:
I am COMPLETELY NEW TO PHP
San someone supply me with a PHP script to read and write to a text
file on the server
Also any suggestion on a free PHP editor?
|
by: Ravikanth[MVP] |
last post by:
Hi
Use the following code.
string sJoblogFileName = "TextFile.txt";
System.IO.StreamWriter LogFile =new System.IO.StreamWriter
(sJoblogFileName,true);
LogFile.WriteLine("blah blah blah");...
|
by: Niron kag |
last post by:
Hello !
With c# , I want to write to a text file in a specific font and color.
Any ideas ?
Thanks...
|
by: Atran |
last post by:
Hello:
In this article: You will learn to Write or Read A Text File.
Let's Begin:
First Create a new project (ConsoleApp or WinApp).
And Make sure your program uses these namespaces:
using...
|
by: someone28485 |
last post by:
hi,i need 2 write code in C#.Net to read a text file with text as
22.13
21.65
25.16
31.22
and so on, i need 2 read this text and then take mean of the 4 values and write that mean result in...
|
by: mvvdsteen |
last post by:
Hello all,
I'm quite new to c++. I made a small program that will help me analyse wind tunnel data. But now I want this program to write to a text file. This works just fine, except it discards...
|
by: Keith G Hicks |
last post by:
I'm trying to read a text file and alter the contents of specific lines in
the file. I know how to use streamreader to read each line of a file. I'm
doing that already to get the data into a...
|
by: solarwind3 |
last post by:
I am familiar with two ways of writing to a text file so the data persists when you access it again. Adding more data (from textbox and label) will be necessary for future transactions. I'd like to...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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,...
|
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...
| |