Config file read from '=' sign

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2009
Posts: 39
Reputation: kelechi96 is an unknown quantity at this point 
Solved Threads: 0
kelechi96 kelechi96 is offline Offline
Light Poster

Config file read from '=' sign

 
0
  #1
Apr 17th, 2009
Ok I need to know how to set my class to read only the values after the = sign.

Here's my class so far.

  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 90
Reputation: unbeatable0 is an unknown quantity at this point 
Solved Threads: 12
unbeatable0 unbeatable0 is offline Offline
Junior Poster in Training

Re: Config file read from '=' sign

 
1
  #2
Apr 17th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Config file read from '=' sign

 
0
  #3
Apr 17th, 2009
^ 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.)
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Config file read from '=' sign

 
0
  #4
Apr 17th, 2009
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 39
Reputation: kelechi96 is an unknown quantity at this point 
Solved Threads: 0
kelechi96 kelechi96 is offline Offline
Light Poster

Re: Config file read from '=' sign

 
0
  #5
Apr 17th, 2009
my question still hasn't been awnsered i want to read from an equals sign in my configuration file how do i do that
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Config file read from '=' sign

 
0
  #6
Apr 17th, 2009
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?...
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Config file read from '=' sign

 
0
  #7
Apr 17th, 2009
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 39
Reputation: kelechi96 is an unknown quantity at this point 
Solved Threads: 0
kelechi96 kelechi96 is offline Offline
Light Poster

Re: Config file read from '=' sign

 
0
  #8
Apr 17th, 2009
A. Whats wrong with goto
B. still dosn't awnser my origonal question
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Config file read from '=' sign

 
0
  #9
Apr 17th, 2009
>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?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 39
Reputation: kelechi96 is an unknown quantity at this point 
Solved Threads: 0
kelechi96 kelechi96 is offline Offline
Light Poster

Re: Config file read from '=' sign

 
0
  #10
Apr 17th, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC