943,752 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1360
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 17th, 2009
0

Config file read from '=' sign

Expand Post »
Ok I need to know how to set my class to read only the values after the = sign.

Here's my class so far.

C++ Syntax (Toggle Plain Text)
  1.  
  2. #include <iostream>
  3. #include <string>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. class Config
  8. {
  9. int pos;
  10. string search;
  11. public:
  12. char * confname;
  13. void configsearch ()
  14. {
  15. fstream filename (confname);
  16. while (!filename.eof())
  17. {
  18. badsearch:
  19. getline (filename,search);
  20. pos = search.find("#");
  21. if (pos != -1)
  22. {
  23. goto badsearch;
  24. }
  25. cout << search;
  26. pos = search.find("=");
  27. if (pos != -1)
  28. {
  29. cout << "\n" << pos;
  30. }
  31. cout << "\n";
  32. }
  33. };
  34. };
Last edited by kelechi96; Apr 17th, 2009 at 1:17 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
kelechi96 is offline Offline
45 posts
since Apr 2009
Apr 17th, 2009
1

Re: Config file read from '=' sign

Look at the code here:
if (pos != -1)
{
	goto badsearch;
}
I think you meant:
if (pos == -1)
{
	goto badsearch;
}

Another thing is that string::search() returns string::npos if the search function does not find what it was told to find, and not -1, so the code should be:

if (pos == string::npos)
{
	goto badsearch;
}
cout << search;
pos = search.find("=");
if (pos != string::npos)
{
	cout << "\n" << pos;
}

And one last thing: you can avoid using goto in your 'if' statement by replacing it with the statement continue; - this will jump to the beginning of the while loop:

if (pos == string::npos)
{
	continue;
}
Last edited by unbeatable0; Apr 17th, 2009 at 1:57 pm.
Reputation Points: 42
Solved Threads: 13
Junior Poster in Training
unbeatable0 is offline Offline
90 posts
since Sep 2008
Apr 17th, 2009
0

Re: Config file read from '=' sign

^ nice answer.

except one thing needs a bit stronger emphasis.

never, ever use "GOTO" this is not BASIC programming. You will be tarred and feathered.


(there may be an exception to the absolute ban on using GOTO, but that's not available until you reach Level 70.)
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Apr 17th, 2009
1

Re: Config file read from '=' sign

>except one thing needs a bit stronger emphasis.
>never, ever use "GOTO" this is not BASIC programming.
Note to the OP: Avoid listening to programming advice that says you absolutely must or must not do something.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 17th, 2009
0

Re: Config file read from '=' sign

my question still hasn't been awnsered i want to read from an equals sign in my configuration file how do i do that
Reputation Points: 10
Solved Threads: 0
Light Poster
kelechi96 is offline Offline
45 posts
since Apr 2009
Apr 17th, 2009
0

Re: Config file read from '=' sign

Quote ...
Avoid listening to programming advice that says you absolutely must or must not do something.
Good metaprogramming advice
Never forget to declare C++ variables before...
To ignore or not to ignore?
Is it an advice or a requirement? Where is the border?...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Apr 17th, 2009
1

Re: Config file read from '=' sign

>Where is the border?...
Common sense, of course. Note that I said "avoid listening to" and not "never listen to". The whole reason we have this "goto is teh EBIL!!!!!" crap is because a bunch of people were mindlessly following orders rather than thinking for themselves.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 17th, 2009
0

Re: Config file read from '=' sign

A. Whats wrong with goto
B. still dosn't awnser my origonal question
Reputation Points: 10
Solved Threads: 0
Light Poster
kelechi96 is offline Offline
45 posts
since Apr 2009
Apr 17th, 2009
1

Re: Config file read from '=' sign

>A. Whats wrong with goto
Nothing. The problem is people using it in an undisciplined way such that the code becomes an unruly mess where control flow is difficult to follow.

>B. still dosn't awnser my origonal question
Be patient. When someone is ready to give you an answer, you'll get it. I notice you didn't even bother to thank unbeatable for taking the time to help you. Did you make the suggested changes?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 17th, 2009
0

Re: Config file read from '=' sign

unbeatable thanks for taking your time to awnser me however firstly I didn't realy need to change my != to == because it was supposed to go to the next line if it found a '#' because '#' is a comment sign and I changed goto to continue i don't know how it will affect execution time but realy execution time isn't realy an issue in this project
Reputation Points: 10
Solved Threads: 0
Light Poster
kelechi96 is offline Offline
45 posts
since Apr 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: BigInteger - modpow()
Next Thread in C++ Forum Timeline: deleting pointer to an object





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC