| | |
Variable pointers or References for .conf parser?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2006
Posts: 6
Reputation:
Solved Threads: 0
hello I am new to C++. I understand pointers and references a little and I must be missing something in conf.cpp. Would you help by pointing out my errors for my pointers coding errors related to my problem? Main reason I am asking is because I get a segment fault where 'servername' is.
system header and conf.h's relative include:
the example varible and an shortcut I picked up the source conf.h:
termed source of conf.cpp for veiwing purposes:
system header and conf.h's relative include:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> using namespace std;
the example varible and an shortcut I picked up the source conf.h:
C++ Syntax (Toggle Plain Text)
#define E extern E string &servername;
termed source of conf.cpp for veiwing purposes:
C++ Syntax (Toggle Plain Text)
string &servername; //parse line int cur; int dirint; int smspace = 1; void parse(string line) { string sub; string pram; string subval; int len = line.length(); for (int i = 0; i < len; i++) { if (line[i] == '\t') { } else if (line[i] == '"') { if (line[i + 1] == ';') { cout << "|\";|"<< endl; smspace = 1; cur = 0; } else { cur = 1; cout << "|\"|"; smspace = 0; } }else if(smspace == 1 && line[i] == ' ') { } else { if (sub == "servername"){ if (cur == 0) servername = "test"; cout << &servername; } if (cur == 1) { pram += line[i]; } else { sub += line[i]; } } } } void read_conf() { ifstream in ( CONF_FILE ); if ( in.is_open() ) { string line; int ret = 0; int linenum = 0; while ( getline ( in, line ) ) { string::size_type i = line.find_first_not_of ( " \t\n\v" ); linenum++; if ( i != string::npos && line[i] == '#' ) { continue; } if (ret == 1) { if ( i != string::npos && line[i] == '*' ) { int ptr = i + 1; if ( i != string::npos && line[ptr] == '/' ) { ret = 0; } continue; } } if ( i != string::npos && line[i] == '/' ) { int ptr = i + 1; if ( i != string::npos && line[ptr] == '*' ) { ret = 1; continue; } }; parse(line); } }; };
Last edited by mknight; Nov 26th, 2006 at 7:05 am. Reason: bad format
•
•
Join Date: Nov 2006
Posts: 6
Reputation:
Solved Threads: 0
sorry! I just realized I forgot the most important detail the .conf file:
C++ Syntax (Toggle Plain Text)
/* general server configurations */ ################# #Server Settings# ################# servername "NightServ.NightIRC.org"; description "NightIRC service an rare constellation of stars"; myaddress "127.0.0.1"; port "8087";
Last edited by mknight; Nov 26th, 2006 at 7:16 am.
•
•
•
•
sorry! I just realized I forgot the most important detail the .conf file:
C++ Syntax (Toggle Plain Text)
/* general server configurations */ ################# #Server Settings# ################# servername "NightServ.NightIRC.org"; description "NightIRC service an rare constellation of stars"; myaddress "127.0.0.1"; port "8087";
Ok I assume that's your input file. Can you give a brief description of what your program will do.
*Voted best profile in the world*
•
•
Join Date: Nov 2006
Posts: 6
Reputation:
Solved Threads: 0
Most certainly! My program is going to be an IRC Services server. At this stage of engineering I am as you see working on the configurations file routings. getting the pram variable data into the an variable that will be accessible through out the program is my problem. As for the main.cpp it is only calling the read_conf(); in conf.cpp.
Last edited by mknight; Nov 26th, 2006 at 7:29 am.
•
•
•
•
Most certainly! My program is going to be an IRC Services server. At this stage of engineering I am as you see working on the configurations file routings. getting the pram variable data into the an variable that will be accessible through out the program. As for the main.cpp it is only calling the read_conf(); in conf.cpp.
My input file looks like this:
server.txt
C++ Syntax (Toggle Plain Text)
/* general server configurations */ ################# #Server Settings# ################# servername "NightServ.NightIRC.org"; description "NightIRC service an rare constellation of stars"; myaddress "127.0.0.1"; port "8087";
I want my program to go through the file and store information from the file into string variables.
i.e
string servername = "NightServ.NightIRC.org";
That would help me help you better now wouldn't it?
*Voted best profile in the world*
Ok I think I understand this better now
Here is the most important information:
Yep, so first we need to split the .txt file into lines.
Storing each line as a string.
Next we need to establish what the first word is of each line is. Then we can match this word with our string variables.
i.e.
Finally once we have identified which line contains which variable we just need to extract the information between the quotes. And then store that into the corresponding variable.
Is this right so far?
Here is the most important information:
C++ Syntax (Toggle Plain Text)
servername "NightServ.NightIRC.org"; description "NightIRC service an rare constellation of stars"; myaddress "127.0.0.1"; port "8087";
Yep, so first we need to split the .txt file into lines.
Storing each line as a string.
Next we need to establish what the first word is of each line is. Then we can match this word with our string variables.
i.e.
C++ Syntax (Toggle Plain Text)
string sub; = servername string pram; = description string subval; = myaddress port???
Finally once we have identified which line contains which variable we just need to extract the information between the quotes. And then store that into the corresponding variable.
Is this right so far?
Last edited by iamthwee; Nov 26th, 2006 at 7:51 am.
*Voted best profile in the world*
•
•
Join Date: Nov 2006
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
Ok I think I understand this better now
Here is the most important information:
Yep, so first we need to split the .txt file into lines.C++ Syntax (Toggle Plain Text)
servername "NightServ.NightIRC.org"; description "NightIRC service an rare constellation of stars"; myaddress "127.0.0.1"; port "8087";
Storing each line as a string.
Next we need to establish what the first word is of each line is. Then we can match this word with our string variables.
i.e.
Finally once we have identified which line contains which variable we just need to extract the information between the quotes. And then store that into the corresponding variable.C++ Syntax (Toggle Plain Text)
string sub; = servername string pram; = description string subval; = myaddress port???
Is this right so far?
you got the idea of what I am trying todo just that the sub is the directive word and pram is the value. As shown
C++ Syntax (Toggle Plain Text)
string sub = servername string pram = NightServ.NightIRC.org
C++ Syntax (Toggle Plain Text)
void parse(string line) { string sub; string pram; int len = line.length(); for (int i = 0; i < len; i++) { if (line[i] == '\t') { } else if (line[i] == '"') { if (line[i + 1] == ';') { cout << "|\";|"<< endl; smspace = 1; cur = 0; } else { cur = 1; cout << "|\"|"; smspace = 0; } }else if(smspace == 1 && line[i] == ' ') { } else { if (sub == "servername"){ if (cur == 0) servername = "test"; cout << &servername; } if (cur == 1) { pram += line[i]; } else { sub += line[i]; } } } }
C++ Syntax (Toggle Plain Text)
if (cur == 1) { pram += line[i]; } else { sub += line[i]; }
Last edited by mknight; Nov 26th, 2006 at 8:46 am. Reason: learnning the way this site works I goofed again :)
Normally I don't do this but I don't have time to explain.
It might not do exactly as you want but it should give a starting point.
C++ Syntax (Toggle Plain Text)
#include <fstream> #include <iostream> #include <string> using namespace std; string quoteExtract( string ); string extractFirstWord( string ); int main() { string line; ifstream open ( "c:\\yourfile.txt" ); while (getline(open,line,'\n')) //read in each line from the file { if (extractFirstWord(line)== "servername") { cout << quoteExtract (line); } } open.close(); //close file cin.get(); //pause program } /* * Input: std::string * Returns: std::string * Purpose: To extract the characters between only ONE pair of quotes */ string quoteExtract (string line) { int start,len; //Declare variables //string line = "description \"NightIRC service an rare constellation of stars\";"; start = line.find_first_of( "\"" ); //stores position of first quote len = line.rfind( "\"" ); //stores position of last quote //cout << "Found first quote at:"; //cout << start; //cout << "\nFound second quote at:"; //cout << len << "\n"; string extract; extract = line.substr( start + 1, ( len -( start + 1 ) ) ); //extracts the characters between quotes return extract; //cin.get(); } /* Extracts the first word in a file * Assumes no preceding whitespace before the * first word */ string extractFirstWord( string line ) { int start; start = line.find_first_of( " " ); //stores position of first space string extract; extract = line.substr( 0, start ); //extracts the first word return extract; }
It might not do exactly as you want but it should give a starting point.
Last edited by iamthwee; Nov 26th, 2006 at 10:25 am.
*Voted best profile in the world*
![]() |
Other Threads in the C++ Forum
- Previous Thread: Using a Database with C
- Next Thread: Using functions and arrays to create a program for grades
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






