![]() |
problem with tokenizing
I guess I am pretty rusty at the moment ;) No sorry, make that VERY rusty...
I'm having a problem with this program: Given an input file that contains: :
this is my loverly fileI only get: :
this is my loverly fileThe last word is being missed? Heres the code: :
#include <iostream>I can't exactly remember how to store the token using a pointer, its very embarassing I know :( I tried a test like this: :
char *ptr = new char[100];but when I try to print it: :
cout << ptr;i get garbage... im doing something very wrong i know! my cheeks are burning ! anyway, hope someone can help me out? Thx PS: reminder to myself to sit down and go over some "fundamentals" this weekend... hehe! |
well i just realised that all i had to do was:
:
//get tokensbut any help with the embarassing pointer problem would be greatly appreciated! :D thx! |
hi,
well i decided that writing a GetToken function would be a much better way to parse the file: :
#include <iostream>it works, but the problem is i dont know how I can get a pointer to the file contents? any suggestions? hope someone can help me out! :) thx! |
something along the lines of:
:
ifstream in("myfile",ios::in);then i can do this: :
GetToken(ptr,token); |
Just try tellg(). This member function takes no parameters and returns a value of type pos_type that is an integer which represents the current position of the get stream pointer.
|
Before i forget to move the pointer of a file to a new location use the seekg(int offs, seekdir direc).
off(offset) is an int, and means how many positions to move from the direc. direc(direction) is an enumeration (ios::beg, ios::cur, ios::end) and specifies where to start counting before moving the pointer. |
hey thx,
i already started using seekg and peek... jeez cant believe how out of touch i am! lol ta for help! |
You do realize the extraction operator has a function that tokenizes on whitespace, right? Also, if your OS is Windows, open the file in binary mode or seeks and tells won't work properly. The use of "conio" is non-standard and blows your portability, if that's of any concern to you.
|
hey DaWei, no i didnt know there was a standard tokenizer - could you give a bit more details?
well im only testing this on Windows at the moment - im only using conio for keeping the console open while i test.... well im trying to write a tokenizer class: doesnt seem to be working properly, here it is: :
class Tokenizer {:
//std dependenciesit doesnt seem to print the last character, im testing by running this: :
int main() {it returns kNull for the last token... im really battling... i want to be able to do something like this: :
while(tokenizer++ != "atoken") {but not working... damn i need way more practice! if any better suggestions/ideas please shout out! im really starting to understand the importance of practical programming, ive never really done any practical programming - and it all makes sense that "practise makes perfect".... |
it would be nice to be able to extract the n'th token from the stream, for example
:
string tok = tokenizer+=5; //get the 5th token from this onebut first im trying to figure out how to extract tokens properly.... any help/suggestions would be greatly appreciated! thx! |
| All times are GMT -5. The time now is 1:44 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC