Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading object from input file

Status
Not open for further replies.

csripriya1

Programmer
May 13, 2003
35
US
Hi I am new to C++ programing. I have probelm in reading data from a file.
I have declared a class like this
class person
{
private:
char *name ;
int age, phone_number;
float wage;

public:
void write_class()
{
cout<< &quot;Name :&quot;<<this.name;
cout <<&quot; Age:&quot; << this.age;
cout <<&quot;wage:&quot;<< this.wage;
}
};
I want to read the data(a class) from a file like this
Jhon 27 500.35
James 32 680.32
.......
where first column gives the name, second gives the age, third gives wages.
Can you pl tell me how to do this.I am using Visual C++ for compiling and running my programs
Thanks for your time and help.
 
Use fstreams.

Create a stream, and attach it to your file.
just read in data (can't remember the exact syntax) as

std::fstream *fs = new std::fstream;

//Convert the filenumber into a text string

//Create the full Name of the File that is Required
std::string fn = <insert filename here>

//Ask the Filestream Object to throw an exception on Failure
fs->exceptions(std::ios::badbit|std::ios::failbit);

try
{
fs->open(fn.c_str(),std::ios::in|std::ios::binary);
//read in Character Data
*fs>>this.name;

//Read in integer data
fs->setf(std::ios_base::dec);
*fs>>this.age;
*fs>>this.wage;

//Read in the floating point data
fs->setf(std::ios_base::fixed);

*fs>>rTheta;
*fs>>rPhi;

catch(std::ios::failure)
{
printf (&quot;Exception Thrown.\nCannot Find File %s\n&quot;,fn.c_str());
}

fs->close();
delete fs;
}

you can also read in the entire structure in one go, but it depends on your compiler as to how accurate this will be (if it pads your class for efficiency, you will get strange results if the data is not a multiple of 16 bytes. (I think 16 is the magic number)

Hope this helps, and sorry it's a bit garbled.
Post back if you need more info, and when I get home I will have more time to expand on what I have written.

Cheers,
K
 
Dear Kalisto,

Thanks for your advice.
I am so novice to this C++ programming. I would be very thankful to you if you can just explain what these lines mean.


Create a stream, and attach it to your file.
just read in data (can't remember the exact syntax) as

std::fstream *fs = new std::fstream;

//Convert the filenumber into a text string

//Create the full Name of the File that is Required
std::string fn = <insert filename here>
I can understand that fstream is a member function of std. But is this std a class included in iostream.h?.IS this a derived class of ios? Sorry for such a stupid question.

Thanks a lot
 
For starting C++ programming i highly recommend using a book that is written for beginners. Trying to learn C++ by piecing together information on the net or in a message based conversation will take FOREVER.

Most of the books for beginners try to cover the fundamentals of C++ programming. These fundamentals are absolutely necessary and attempting to skip over them will not produce a desirable result.


-pete
 
Hi all,
Thanks Kalisto,I could figure out the things myself.Thanks for palbano too. I have a TurboC++ book. actually I was working through one example. I couldn't get that example work in my visual C++. Then I asked for help.
Anyway thanks gain. This web site had been always helpful to me.
 
No Problem, Just post in here if you have more questions (I have marked this thread, so if you post I will know)

Else, Good luck

K
 
Hi (Kalisto can u pl help me again),
I have some problem in formatting my outfile.I am trying to do this in C++.
I have data like this.
-4.3765 0.0449 0.0000 C
-4.3765 -1.4549 0.0000 C
where the first column is xcrd, second is ycrd,3rd is zcrd.
I am trying to write the same in an out file using this code

while(in<natoms[fgno])
{
*fs<<setw(10)<<setprecision(4)
<<showpoint
<<xcrd[fgno][in]
<<setw(10)<<setprecision(4)
<<showpoint
<<ycrd[fgno][in]
<<setw(10)<<setprecision(4)
<<showpoint
<<zcrd[fgno][in]
<<noshowpoint
<<setw(3)<<element[fgno][in] <<endl;
in++;
}
I am getting this output
-4.377 0.04490 0.0000 C
-4.377 -1.455 0.0000 C
where the xcrd precision is like 3 and the ycrd precision is like 5.I am wondering why.can you pl help me with this.
Thanks.

 
I can't see anything immediate, but I am tierd, I'll check again in the morning (I haven't looked into streams for a while so I need to check my books).

Once you have set the field width and precision, you don't need to change it, so you shouldn't need all those
&quot;<<setw(10)<<setprecision(4)&quot;, just the first one.

But like I said, I'll check my book in the morning, and let you know what I find. (can you check in memory that the data is being stored to 4 decimal places, (the Y coord is 5 in the first line and 3 in the second, so there is something strange going on)

I'll post again tomorrow, unless I forget !

Good luck

K
 
Thanks kalisto,
Again I got the problem fixed myself. Thanks a lot
 
Sorry for the delay in replying :-/

Personally I would just set the float type to fixed, and the precision to 4 once (you do realise that a precision of 4 is 3 decimal places, so to see your data as you might expect, make the precision 5)
Also, what are you trying to achieve with <<showpoint ?

if I recall correctly, all you need is

//Set up the floatingpoint format
*fs.setf(std::ios_base::fixed, std::ios_base::floatfield);

*fs<<setprecision(5)<<setw(10)<<xcrd[fgno][in]<<ycrd[fgno][in]<<zcrd[fgno][in];

But if this is wrong, please tell me, as I will probably be using streams again soon, and if I can't remember this I need to dig the 'bible' out.


 
u are right about the strings as far as I learnt in this few days. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top