reading from a text file: not line by line but values seperated by a space

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2009
Posts: 29
Reputation: phoenix911 is an unknown quantity at this point 
Solved Threads: 0
phoenix911 phoenix911 is offline Offline
Light Poster

reading from a text file: not line by line but values seperated by a space

 
0
  #1
May 25th, 2009
all i can do is get each line at a time....
how can i read it to only read till the space....

plz help.... heres my lil bit of code

  1. while (getline(iofile,line))
  2. {
  3. cout << line << endl;
  4. }

plz i know i can use this aswell for line by line

  1. while (!iofile.fail())
  2. {
  3. getline(iofile,line)
  4. cout << line << endl;
  5. }

thanx in advanced
Last edited by phoenix911; May 25th, 2009 at 7:41 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 152
Reputation: amrith92 is on a distinguished road 
Solved Threads: 15
amrith92's Avatar
amrith92 amrith92 is offline Offline
Junior Poster

Re: reading from a text file: not line by line but values seperated by a space

 
0
  #2
May 25th, 2009
  1. while (getline(iofile,line))
  2. {
  3. cout << line << endl;
  4. }

Your code is on the right track. Check out the getline function's prototypes here: getline - C++ Reference. The file structure you have described is known as a "Space-delimited File". In your getline function, specify the delim parameter. (If you have followed the link, you will know what to pass as this third parameter).

Hope this helps!

NOTE: This type of question has been asked many times before, in fact some very visible in the main Index of this forum! Try to at least search these forums/or the web before posting your question. A LOT of information is available on the net on this topic.
Last edited by amrith92; May 25th, 2009 at 7:52 am.
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 29
Reputation: phoenix911 is an unknown quantity at this point 
Solved Threads: 0
phoenix911 phoenix911 is offline Offline
Light Poster

Re: reading from a text file: not line by line but values seperated by a space

 
0
  #3
May 25th, 2009
thanx...will check it out right away...

but b4 i do... im using something like this now

  1. iofile >> data1 >> data2 >> data3 >> data4;

will this help???
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 152
Reputation: amrith92 is on a distinguished road 
Solved Threads: 15
amrith92's Avatar
amrith92 amrith92 is offline Offline
Junior Poster

Re: reading from a text file: not line by line but values seperated by a space

 
0
  #4
May 25th, 2009
Originally Posted by phoenix911 View Post
thanx...will check it out right away...

but b4 i do... im using something like this now

  1. iofile >> data1 >> data2 >> data3 >> data4;

will this help???
I have no idea where you are going to put this, so I don't know what it will accomplish. If it is inside your while-loop, then, no it will not help at all. Anyway, this approach undermines the purpose of getline in the first place. Stick with your original code.
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 29
Reputation: phoenix911 is an unknown quantity at this point 
Solved Threads: 0
phoenix911 phoenix911 is offline Offline
Light Poster

Re: reading from a text file: not line by line but values seperated by a space

 
0
  #5
May 25th, 2009
so far i got this

  1.  
  2. fighter fight[80]; // this is a class with an array size
  3.  
  4. //the class looks as follows
  5. class fighter
  6. {
  7. string name, special;
  8. int str, agi, spd;
  9.  
  10. public:
  11.  
  12.  
  13. void set(string n, string s, int st, int ag, int sp)
  14. {
  15. name = n;
  16. special = s;
  17. str = st;
  18. agi = ag;
  19. spd = sp;
  20. }
  21. // end of class
  22.  
  23. while (getline(iofile,line))
  24. {
  25. iofile >> name >> special >> str >> agi >> spd;
  26.  
  27. fight[n].set(name, special, str, agi, spd);
  28. n++;
  29. }

now is what im trying to do right? read from the text file and put it into an array?
Last edited by phoenix911; May 25th, 2009 at 8:10 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: reading from a text file: not line by line but values seperated by a space

 
0
  #6
May 25th, 2009
Line by line, word by word:
  1. void WordByWord(const char* filename)
  2. {
  3. if (!filename)
  4. return;
  5. ifstream file(filename);
  6. string line, word;
  7. for (int lineno = 1; getline(file,line); lineno++) {
  8. cout << "Line #" << lineno << ":\n";
  9. istringstream words(line);
  10. while (words >> word)
  11. cout << word << '\n';
  12. }
  13. }
Think ...
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 152
Reputation: amrith92 is on a distinguished road 
Solved Threads: 15
amrith92's Avatar
amrith92 amrith92 is offline Offline
Junior Poster

Re: reading from a text file: not line by line but values seperated by a space

 
0
  #7
May 25th, 2009
Did you actually go through the links I gave, and the clue I provided in my previous posts? If so, did you not understand it? and no, your code still will read your file line-by-line.

> read from the text file and put it into an array?

A similar(if not exactly the same) question has been asked at least twice in the past week in this same forum. Search for answers first, and iff you did not understand it, or else your problem hasn't been addressed to yet, then ask the question.

>
(To ArkM): Nice Code!
Last edited by amrith92; May 25th, 2009 at 8:19 am.
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 29
Reputation: phoenix911 is an unknown quantity at this point 
Solved Threads: 0
phoenix911 phoenix911 is offline Offline
Light Poster

Re: reading from a text file: not line by line but values seperated by a space

 
0
  #8
May 25th, 2009
yes i did read ur post... and actually what i posted is working talking bout the
  1. iofile >> data1 >> data2 >> data3 >> data4;
it actually does what i want...

only prob i have now is storing it in2 an class array :/
Last edited by phoenix911; May 25th, 2009 at 8:22 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,401
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: reading from a text file: not line by line but values seperated by a space

 
0
  #9
May 25th, 2009
>>only prob i have now is storing it in2 an class array

What array? You need to post the exact class declaration instead of making us guess what you are doing.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 29
Reputation: phoenix911 is an unknown quantity at this point 
Solved Threads: 0
phoenix911 phoenix911 is offline Offline
Light Poster

Re: reading from a text file: not line by line but values seperated by a space

 
0
  #10
May 25th, 2009
heres my entire class

  1. class fighter
  2. {
  3. string name, special; // when i compile it says error is here
  4. int str, agi, spd;
  5.  
  6. public:
  7.  
  8. void set(string n, string s, int st, int ag, int sp)
  9. {
  10. name = n;
  11. special = s;
  12. str = st;
  13. agi = ag;
  14. spd = sp;
  15. }
  16. };

what im trying to do here... is display the class array
  1. for (n = 0; n < 80; n++)
  2. {
  3. cout << fight[n].name << endl; // and this is part of the error
  4. }
Last edited by phoenix911; May 25th, 2009 at 9:01 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC