By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,822 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

How to use data file in fstream

P: 2
Hi...everybody...I'm stuck in fstream...can anyone please be kind helping me out of my problem.

I don't know how to use my data file correctly ........For example now I have data file (data.data) contains r on the left hand side and theta on the right side
5 0.5235
7 0.5345
4 0.5642

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.   fstream fo;
  4.   fo.open("data.dat");
  5.   float r,theta;
  6.     cout<<"List of polar coordinate"<<endl;
  7.     cout<<endl;
  8.     cout<<"Modulus,r"<<"     "<<"Theta"<<endl;
  9.   while (fo>>r>>theta){
  10.     cout<<r<<"             "<<theta<<endl;
  11. }
  12.  
  13. //when i open my file i will get
  14. // 5   0.5235
  15. // 7   0.5345
  16. // 4   0.5642
i want to sum up all the value in left hand side 5+7+4.........but i dont know how to do it

I'm just a beginner in c++....sorry if you guys don't understand my question.
Mar 15 '07 #1
Share this Question
Share on Google+
2 Replies


P: 2
CORRECTION FROM PREVIOUS QUESTION

Hi...everybody...I'm stuck in fstream...can anyone please be kind helping me out of my problem.

I don't know how to use my data file correctly ........For example now I have data file (data.dat) contains r on the left hand side and theta on the right side
5 0.5235
7 0.5345
4 0.5642

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3. #include<fstream>
  4. #include<cmath>
  5.  
  6. int main()
  7. {
  8.   fstream fo;
  9.   fo.open("data.dat");
  10.   float r,theta;
  11.     cout<<"List of polar coordinate"<<endl;
  12.     cout<<endl;
  13.     cout<<"Modulus,r"<<"     "<<"Theta"<<endl;
  14.   while (fo>>r>>theta){
  15.     cout<<r<<"             "<<theta<<endl;
  16. }
  17.  
  18. //when i open my file i will get
  19. // 5         0.5235
  20. // 7         0.5345
  21. // 4         0.5642
i want to sum up all the value in left hand side 5+7+4.........but i dont know how to do it

I'm just a beginner in c++....sorry if you guys don't understand my question.
Mar 15 '07 #2

Ganon11
Expert 2.5K+
P: 3,652
First of all, since you are using an fstream object, I believe you have to specify that you want to read from this file when opening - instead of

Expand|Select|Wrap|Line Numbers
  1. fo.open("data.dat");
it should be

Expand|Select|Wrap|Line Numbers
  1. fo.open("data.dat", ios::in);
You can avoid this by using the more specific ifstream class, which is only for file input.

Now, since you know the file contains an integer, then a double in every line, you can use a while() loop controlled by the .eof() function in the fstream object. .eof() returns true if the end of file has been reached, false otherwise - so while you haven't gotten to the end, you will keep reading data. For each execution of the loop, you will read the integer value and add it to a sum variable, then read and discard (or otherwise process) the double value.
Mar 15 '07 #3

Post your reply

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