![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#31 |
|
Hobbyist Programmer
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2
![]() |
Yes i know, but as i said it was a beggining class. I have modified this way :
void StringTokenizer::findTokens()
{
ifstream in(filename, ios::in | ios::binary);
char c;
int beg = 0, length = 0;
while (!in.eof())
{
in.get(c);
if (c != ' ' && c != '\r' && c != '\n')
length++;
else beg++;
if (((c == ' ' || c == '\r') && length > 0) || in.eof())
{
Token t;
t.firstpos = beg - 1;
t.length = length;
allTokens.push_back(t);
beg += length;
length = 0;
}
else if (c == '\r' || c == '\n')
{
beg += length;
length = 0;
}
}
in.close();
numberOfTokens = allTokens.size();
}
__________________
You never test the depth of a river with both feet. The believer is happy. The doubter is wise. Free speech carries with it some freedom to listen. The next generation will always surpass the previous one. It`s one of the never ending cycles of life. |
|
|
|
|
|
#32 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I have no problem with people asking for help with simple problems. That's what the forum is for, for God's sake. Sometimes that help comes in the form of code. Sometimes it comes in the form of advice regarding HOW to solve problems. Your statement that a person doesn't have time to study the libraries is ludicrous. Far less time is taken in reviewing the methods available for opening and reading a file than is expended in writing and debugging bad code. That's why the documentation is there. That's why the tutorial section is packed with links. The C++ streams are based on classes. If they don't meet all your needs, you can flesh them out. Presenting bad code to a poster is NOT a favor, however much you may think that it is. You are, to quote your own signature, testing the depth of the water with both feet. You don't have time to do it with one foot -- you might get old.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#33 | |
|
Hobbyist Programmer
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2
![]() |
Well can u give me a hint were my code is bad so that i can improve it.
EDIT: Quote:
__________________
You never test the depth of a river with both feet. The believer is happy. The doubter is wise. Free speech carries with it some freedom to listen. The next generation will always surpass the previous one. It`s one of the never ending cycles of life. |
|
|
|
|
|
|
#34 | ||
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
This is probably more complicated than it needs to be for several reasons. I have presumed that a comment mark may be part of a token (# comment or #comment), I have presumed that a comment may appear anywhere on a line, I have presumed that arguments following the trigger word may span lines, and I have further presumed that a comment may intervene between arguments. That complicates things a little. As an exercise, you might want to derive a class from ifstream and overload the >> operator so that it strips comments automatically.
Pegasus, there's no need to whine when you receive some criticism. It's part of life and learning. Grow up. The code: #include <iostream>
#include <fstream>
#include <string>
#include <vector>
using std::string;
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::ifstream;
using std::vector;
struct argPair
{
string first;
string second;
};
string getToken (ifstream &theStream)
{
string token;
while (true)
{
theStream >> token;
if (token.substr (0, 1) == "#") getline (theStream, token);
else return token;
}
}
int biteMe (string trouble)
{
cerr << trouble << endl;
return EOF;
}
int main()
{
string trigger = "trigger";
string comment = "#";
string token;
argPair thePair;
vector <argPair> theArgs;
// Open file
ifstream theStuff ("stuff.txt");
ifstream &theStream = theStuff;
if (!theStuff.is_open ()) return biteMe ("The file didn't open");
while (true)
{
token = getToken (theStream);
if (!theStuff.good ()) break;
if (token == "trigger")
{
thePair.first = "failure";
thePair.second = "failure";
thePair.first = getToken (theStream);
thePair.second = getToken (theStream);
theArgs.push_back (thePair);
if (!theStuff.good ()) break;
}
}
if (theStuff.bad ()) return biteMe ("Serious input error");
for (unsigned i = 0; i < theArgs.size (); ++i)
{
cout << theArgs [i].first << ", " << theArgs [i].second << endl;
}
return 0;
}Quote:
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
||
|
|
|
|
|
#35 | |
|
Hobbyist Programmer
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2
![]() |
Quote:
Youth never comes back, so i`ve got to enjoy it. PS:Thanx for the code i will study more deeply the libraries.
__________________
You never test the depth of a river with both feet. The believer is happy. The doubter is wise. Free speech carries with it some freedom to listen. The next generation will always surpass the previous one. It`s one of the never ending cycles of life. |
|
|
|
|
|
|
#36 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
You're dealing in non-sequiturs. No one is telling you not to enjoy life or growing up. Someone is suggesting that if you post poor, unworking solutions, perhaps you should refrain from posting a solution.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#37 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
Hey,
Sorry im only replying now, had to do a bunch of work here at the studio... Now can get bac k to my code... Hey, well even if the solution Pegasus gave had a little bug thats fine, at least he gave a hint that I learnt alot from... I really appreciated it... Hey DaWei, thx for the example - ill take a look now... Looks interesting! ![]() Sure it does make sense to understand fully how the libraries work, rather than going in and just hacking away... Next time ill do that... BTW got the program all working... Feel my skills are getting much better ![]() Thx again for all help! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Storing BLOBs in a database - problem | jonyzz | Other Programming Languages | 8 | Jan 31st, 2007 4:38 AM |
| Changing icons problem | Pedja | C# | 8 | Mar 25th, 2006 8:03 AM |
| Huge arrays in C (game-oriented problem theme) | Rather Generic | C | 6 | Mar 19th, 2006 1:09 AM |
| cgi/perl script + IE problem | joyceshee | Perl | 2 | Jan 24th, 2006 11:10 AM |