The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 10-11-2008, 12:46 PM
MrPickle's Avatar
MrPickle MrPickle is offline
PT Admin
Join Date: Nov 2007
Location: England, Lincolnshire
Posts: 332
iTrader: (0)
MrPickle is a jewel in the roughMrPickle is a jewel in the roughMrPickle is a jewel in the rough
Stringstream repeating first word

I am tokenizing a line using a stringstream and a vector but the stringstream seems to be repeating the first word, here's my code:

Code:
std::stringstream ss;
std::vector<std::string> LineVec;
std::string Line, Word;
while(std::getline(File, Line)) {
   if(Line.empty()) { continue; }
   ss.str(Line);
   ss.seekg(std::ios::beg);
   while(ss >> Word) { LineVec.push_back(Word); }

   //Un related stuff here

   LineVec.clear();
}
Here's an example input-output
Quote:
Input: Hello, My name's MrPickle
Output: Hello,
My
name's
MrPickle
Hello,

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #2 (permalink)  
Old 10-11-2008, 05:03 PM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 116
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
There's nothing obviously wrong with the code you've posted, perhaps you could post a more complete example program which displays the problem.


As an aside, you have a couple of redundant lines of code there; namely
Code:
   ss.seekg(std::ios::beg);
will effectively do nothing, since the ss object was only just initialised in the previous statement, and will already be set at the beginning (though it shouldn't do any harm either)
also
Code:
   if(Line.empty()) { continue; }
will never yield true, since Line would only be empty if your call to getline failed - at which point, the while loop would not actually run anyway

However, neither of these will affect the output you're seeing; I suspect the problem is either related to your File, or to the way you're directing output from your program

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to Bench For This Useful Post:
MrPickle (10-12-2008)
  #3 (permalink)  
Old 10-11-2008, 06:04 PM
MrPickle's Avatar
MrPickle MrPickle is offline
PT Admin
Join Date: Nov 2007
Location: England, Lincolnshire
Posts: 332
iTrader: (0)
MrPickle is a jewel in the roughMrPickle is a jewel in the roughMrPickle is a jewel in the rough
Without the seekg it doesn't output the later lines, here's an example I made to show that: Example.zip

I don't know what I'm doing to get it to output the first character twice, here's my exact code:
Code:
template<typename T>
T FromStringTo(std::string String) { return boost::lexical_cast<T>(String); }

void StringReplace(std::string *String, std::string Strip, std::string Replace) {
	int Position = String->find(Strip);
	while(Position != String->npos) {
		String->replace(Position, 1, Replace);
		Position = String->find(Strip, Position + 1);
	}
}

Obj::Obj(std::string Filename) {
		Loaded = false;
		std::ofstream Out("Obj.txt");
		std::ifstream File(Filename.c_str(), std::ios::in);
		if(!File.is_open()) { std::runtime_error("Unable to open \"" + Filename + "\""); exit(1); }

		std::string Line, Word, Type;
		std::stringstream LStream;
		std::vector<std::string> LVector;
		Vertex V;
		Polygon P;
		while(std::getline(File, Line)) {
			if(Line.empty()) { continue; }
			StringReplace(&Line, "/", " ");
			LStream.str(Line);
			LStream.seekg(std::ios::beg);
			Out << "----------------------------------------------\n" << Line << "\n"; Out.flush();
			while(LStream >> Word) { LVector.push_back(Word); }
			for(int i = 0; i < LVector.size(); i++) Out << LVector[i] << "\n"; Out.flush();

			Type = LVector.at(0);
			Out << Type << "\n";
			if(Type == "v") {
				V.x = FromStringTo<float>(LVector.at(1));
				V.y = FromStringTo<float>(LVector.at(2));
				V.z = FromStringTo<float>(LVector.at(3));
				Vertices.push_back(V);
			} else if(Type == "f") {
				switch(LVector.size()) {
					case 4:
						for(int i = 0; i < 3; i++) {
							P.Vertices[i] = FromStringTo<int>(LVector.at(i + 1));
						}
					case 7:
						for(int i = 0; i < 3; i++) {
							P.Vertices[i] = FromStringTo<int>(LVector.at(2 * i + 1));
						}
					case 10:
						for(int i = 0; i < 3; i++) {
							P.Vertices[i] = FromStringTo<int>(LVector.at(3 * i + 1));
						}
				}
				Faces.push_back(P);
				Out << LVector.size() << "\n";
			}
			LVector.clear();
		}
		Path = Filename;
		Loaded = true;
		Out.flush();
	}

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #4 (permalink)  
Old 10-12-2008, 05:33 AM
MrPickle's Avatar
MrPickle MrPickle is offline
PT Admin
Join Date: Nov 2007
Location: England, Lincolnshire
Posts: 332
iTrader: (0)
MrPickle is a jewel in the roughMrPickle is a jewel in the roughMrPickle is a jewel in the rough
Ok, I was being stupid. I am outputting the first word of the vector a few lines down. DOH.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 08:09 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50