473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to write class' objects to as a text file

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.
Jan 15 '14 #1
8 1497
weaknessforcats
9,208 Expert Mod 8TB
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.
Jan 15 '14 #2
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.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<fstream>
  3. #include <cstdlib>
  4. #include <math.h>
  5. #include <conio.h>
  6. using namespace std;
  7.  
  8. class showit {
  9.       int numberx;
  10.       public:
  11.              void assignx(int a){
  12.                   numberx=a;
  13.                   }
  14.       int xprint(){
  15.           return numberx;
  16.           }
  17.       }k,e,v,i,n;
  18.       int main(){
  19.           k.assignx(1);
  20.           e.assignx(2);
  21.           v.assignx(3);
  22.           i.assignx(4);
  23.           n.assignx(5);
  24.       cout << k.xprint() << endl;
  25.       cout << e.xprint() << endl;
  26.       cout << v.xprint() << endl;
  27.       cout << i.xprint() << endl;
  28.       cout << n.xprint() << endl;
  29.       system("pause");      
  30. }
  31. class filex 
  32. {
  33.       private :
  34.              showit infox;
  35.               void writeToFile()
  36.               {
  37.                   ofstream  yprint("newFile.txt", ios::app);
  38.                   yprint << infox.k << endl;
  39.                   yprint << infox.e << endl;
  40.                   yprint << infox.v << endl;
  41.                   yprint << infox.i << endl;
  42.                   yprint << infox.n << endl;
  43.                   yprint.close();
  44.                   }
  45. }
Jan 16 '14 #3
weaknessforcats
9,208 Expert Mod 8TB
OK. I fixed your code.

Let's take this in pieces. Here's the new main():

Expand|Select|Wrap|Line Numbers
  1.      int main(){
  2.           showit k,e,v,i,n;
  3.           k.assignx(1);
  4.           e.assignx(2);
  5.           v.assignx(3);
  6.           i.assignx(4);
  7.           n.assignx(5);
  8.  
  9.           filex obj;
  10.                 obj.writeToFile(k);
  11.                  obj.writeToFile(e);
  12.                   obj.writeToFile(v);
  13.                   obj.writeToFile(i);
  14.                   obj.writeToFile(n);
  15.  
  16.       cout << k.xprint() << endl;
  17.       cout << e.xprint() << endl;
  18.       cout << v.xprint() << endl;
  19.       cout << i.xprint() << endl;
  20.       cout << n.xprint() << endl;
  21.       system("pause");      
  22. }
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:

Expand|Select|Wrap|Line Numbers
  1. class filex 
  2. {
  3.       private :
  4.              showit infox;
  5.              ofstream theFile;
  6.  
  7. public:
  8.       filex();
  9.       ~filex();
  10.       void writeToFile(showit& arg);
  11.  
  12. };
  13.  
  14.  
  15. filex::filex()
  16. {
  17.     this->theFile.open ("newFile.txt", ios::app);
  18. }
  19. filex::~filex()
  20. {
  21.       this->theFile.close();
  22. }
  23. void filex::writeToFile(showit& arg)
  24. {
  25.                   theFile << arg.xprint() << endl;
  26.  
  27.  }
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:

Expand|Select|Wrap|Line Numbers
  1. filex obj;
  2.  
  3.  
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?
Jan 16 '14 #4
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

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<fstream>
  3. #include <cstdlib>
  4. #include <math.h>
  5. #include <conio.h>
  6. using namespace std;
  7.  
  8. class showit {
  9.       int numberx;
  10.       public:
  11.              void assignx(int a){
  12.                   numberx=a;
  13.                   }
  14.       int xprint(){
  15.           return numberx;
  16.           }
  17.       }k,e,v,i,n;
  18.       int main(){
  19.           showit k,e,v,i,n;
  20.           k.assignx(10);
  21.           e.assignx(20);
  22.           v.assignx(30);
  23.           i.assignx(40);
  24.           n.assignx(50);
  25.  
  26.           filex obj;
  27.                 obj.writeToFile(k);
  28.                  obj.writeToFile(e);
  29.                   obj.writeToFile(v);
  30.                   obj.writeToFile(i);
  31.                   obj.writeToFile(n);
  32.  
  33.                   cout << k.xprint() << endl;
  34.                   cout << e.xprint() << endl;
  35.                   cout << v.xprint() << endl;
  36.                   cout << i.xprint() << endl;
  37.                   cout << n.xprint() << endl;
  38.                   system("pause");     
  39. }
  40.  
  41. class filex 
  42. {
  43.       private :
  44.              showit infox;
  45.              ofstream theFile;
  46.  
  47. public:
  48.       filex();
  49.       ~filex();
  50.       void writeToFile(showit& arg);
  51. };
  52. filex::filex()
  53. {
  54.     this->theFile.open ("newFile.txt", ios::app);
  55. }
  56. filex::~filex()
  57. {
  58.       this->theFile.close();
  59. }
  60. void filex::writeToFile(showit& arg)
  61. {
  62.                   theFile << arg.xprint() << endl;
  63.  
  64.  }
  65.  
Regards

Demir
Jan 17 '14 #5
if I disable line 26 to 31 the code working but off douse it is not writing any thing.

Regards

Demir
Jan 17 '14 #6
I mean that of course:) sorry
Jan 17 '14 #7
weaknessforcats
9,208 Expert Mod 8TB
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.
Jan 17 '14 #8
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
Jan 17 '14 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
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...
3
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...
3
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?
0
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");...
14
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...
6
Atran
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...
4
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...
4
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...
4
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...
1
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...
0
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,...
0
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$) { } ...
0
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...
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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
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...

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.