Ask questions via twitter! Message any question to @answers on twitter. We'll publish the question and send you a reply each time there's a new answer.
Next Question

Question

 
M$1 October 08, 2009 03:24 AM

I'm trying to figure out how to loop through a text file in C++, word by word for an unknown number of words. Will this work?

http://i33.tinypic.com/ftf39h.jpg

Theres what I have, but I'm having a bit of a blonde moment.

Basically I have a chunk of code at the top of a file (the variable "plyFile" in the image which is defined earlier in the code). I need to loop through it and look for a couple keywords. I either need the number that comes in "element face (number)" or "element vertex (number)" in a variable to be used later. The rest is garbage data and can be disposed of. I can stop looping when I reach "end_header".

The example my teacher gave us involves going line by line like
PlyFile >> garbage >> garbage >> garbage;
PlyFile >> garbage >> int;
However, that only applied to one example and didn't allow files with different headers to be used. Mine needs to work regardless of whats in the header (incase there are things like extra comments).

So you can see my logic...
Do-while loop until I reach "end_header".
Take the string. If it's "element" and the next is "vertex", put the next one in a variable as an int.
If it's "element" and the next is "face" , put the next in another variable.
Then, it doesn't matter if it hits the next keywords on the way by again, as the important ones are already saved in another variable, and they can just be overwritten from the garbage variable.

However, I'm a little worried that doing it like this, I'm going to get stuck in an endless loop. With my way, is it going to move onto the next string each time through the loop, or is it just going to keep testing the first word? (in most cases there are 3-4 words on a line).
If I need some sort of statement at the start or end of the do-while to move on to the next word... what do you suggest?

(if anyone is wondering and hasn't figured it out.. I'm building a ply loader for Direct X.. I can't test it yet because I have a massive amount of work left before any of this is going to work)

Help?! (I'll be at this all night if anyone's lookin' to make a couple bucks ;)
Interesting Question?  Yes (0)   No (0)   
Email to a friend | RSS
 
 

 
   No Best Answer Selected, Tip Refunded
2 answerers thought this was unfair.
 
 


Answers (2)

Sort By
 
October 08, 2009 05:51 AM
I'm not an experienced C++ programmer nor have I used the overloading of bitwise operators for extracting, but from Googling around it looks like your program should work.
Source(s):
http://www.mayukhbose.com/tutorials/overloading/bitshift.php


Tags: operator, overloading, bitshift, extractor

Helpful Answer?  (0)   (0)    Tip waulok for this answer
Permalink | Report
   Reply  
 
 
 
October 08, 2009 09:08 AM
thanks for the attempt. I found a classmate to ask and they assured me this would work :) (i hope they're right!)

Report
 
 
 
October 08, 2009 03:45 PM
A cleaner way of doing things would be to check for a null string, meaning it reached the end of the file. I'm not sure what input you're looking at, but if a line doesn't contain "end_header" exactly (or has other text after it in the same line), your program will get caught in an infinite loop and crash.

You can solve this by an easy modification at the end:
while(garbageString != "end_header" && garbageString != NULL)

Regards
Source(s):
exp


Helpful Answer?  (0)   (0)    Tip esiege for this answer
Permalink | Report
   Reply  
 
 
 
October 11, 2009 10:55 PM
Thanks, however, that doesn't work as there is more after end_header.. it's just a different format.
Turns out what I had worked perfectly, but thanks :)

Report
 
 
Buy Mahalo Dollars with Credit Card or PayPal

Top Members

This Week All Time
  • buddawiggi
    buddawiggi
    2nd Degree Black Belt
    27808 Points
    M$799.16 Earned
  • opher
    opher
    Purple Belt
    4473 Points
    M$196.22 Earned
  • annelisle
    annelisle
    Purple Belt
    3110 Points
    M$95.22 Earned
   See All
 

Most Popular Tags

mahalo(1625)
iphone(466)
music(462)
google(358)
food(321)
online(296)
beer(279)
money(262)
movies(259)
apple(251)
aotd(235)
health(220)
video(208)
free(205)
dog(205)
   See All
 

Categories

Welcome New Members


 
 
Mahalo Dollars are the currency of Mahalo Answers.

Each Mahalo Dollar costs $1.

Once you earn more than 40 Mahalo Dollars, you can request to be paid via PayPal. Each Mahalo Dollar is currently worth $0.75 when paid out via PayPal. Learn More

 
 

Please log in to use this function.