ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Im making a little program that needs to parse through its configuration file. The conf has entries like this:
Quote:
# comment
C 42
D 123
E 456
Ive written a function to parse the file, but ran into trouble. Manipulating the lines always seems to get me a string, and sofar I haven't been able to static_cast a string into int, and Im beginning to believe its impossible..
So, any ideas how I could get around this problem? Perhaps reading the whole file one char at a time instead of getline? If thats the only way, Im pretty lost with it.. Just learning file I/O and am not very confident with it yet.. (:
// parse the notation base freq values and names from the conf file and save them into the notation vector.
bool parseConfig(vector<Note>& notation)
{
ifstream config(CONF.c_str());
if (!config) { error("configOpen"); return false; }
string line = "";
int i = 0;
while (getline(config, line))
{
if (line != "" && line.at(0) != '#' ) // we ignore empty lines and lines that begin with #
{
int cutPoint = getIndex(line, ' ');
int tmpFreq = static_cast< int >(line.substr(cutPoint+1, line.size()-cutPoint-1)); // Note.baseFreq
Note tmp = {
line.substr(0, cutPoint-1), //Note.name
tmpFreq,
0, // Note.length, not needed with notation vector
1 // Note.octave, base value
};
notation.push_back(tmp);
}
++i;
}
config.close();
return true;
}
Um. Wouldn't the above just read the whole file into the variables? I need to parse the values out first before theyre ready to be shoved into variables..
Okay, im starting to see the light.. But, when reading with the >> operator, it starts from the beginning of the file.. How do I control where to read from, and most importantly, skip comment lines? I guess I could just monitor the next character up for reading and if its a #, then ignore till we find a \n.. Bu im not sur how to do that..
C++ sucks at text i/o. (It's better than C, but still sucks.) If you want to do much text i/o use a scripting language like perl, python, ruby. I recommended Ruby.
Anyhoo, that isn't what you asked.
I would get a line at a time, decide whether its a comment, if so discard, else wrap the string is a strstream and proceed as before..
will have to look intro stringstreams.. From what ive read, they seem better.. Ive managed to get comments sorted out now, but the problem persists on how to read the values correctly.. The >> operator is great, but not when it comes to a configfile that needs to be human readable.. Maybe ill just hardcode the frequency values in, or make a not-so-nice config.. *bleh*
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.