Next Question
RSS
No Best Answer Selected, Tip Refunded
2 answerers thought this was unfair.
Source(s):
http://www.mayukhbose.com/tutorials/overloading/bitshift.php
Permalink | Report
girlieq300...
You can solve this by an easy modification at the end:
while(garbageString != "end_header" && garbageString != NULL)
Regards
Source(s):
exp
Permalink | Report
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 ;)
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)
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
Permalink | Report
girlieq300...
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!)
Tip girlieq3000 for this comment
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
Permalink | Report
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
Turns out what I had worked perfectly, but thanks :)
Answer this Question
Related Questions
Ask a Question
Buy Mahalo Dollars with Credit Card or PayPal
Top Members
Most Popular Tags
Categories
- Anonymous
- Arts & Design
- Beauty & Style
- Books & Authors
- Business
- Cars & Transportation
- Consumer Electronics
- Coupons Deals
- Education
- Entertainment
- Environment
- Fitness
- Food & Drink
- From Email
- From Iphone
- From Twitter
- Health
- History
- Hobbies
- Home & Garden
- How Tos
- Humor
- Jobs
- Legal
- Local
- Love & Relationships
- Mahalo Answers Community
- Money
- Music
- News
- NSFW
- Parenting
- Pets
- Science & Mathematics
- Services
- Shopping
- Social Science
- Society & Culture
- Sports
- Technology & Internet
- Travel
- Video Games
Welcome New Members
- exportdesk, November 30, 2009 12:34 AM
- bryliepatterson, November 30, 2009 12:26 AM
- diganddig, November 30, 2009 12:22 AM
- smartypantz_99, November 30, 2009 12:19 AM
- musette, November 29, 2009 11:48 PM
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
