LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 12-15-2005, 02:30 PM   #1
Artanicus
Member
 
Registered: Jan 2005
Location: Finland
Distribution: Ubuntu, Debian, Gentoo, Slackware
Posts: 827

Rep: Reputation: 31
C++ file I/O, read content as int value


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.. (:

Any comments appreciated, function included below, full source available at: http://www.mythmardyl.org/~fyranthalas/beepmake.cpp

Code:
// 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;
}
 
Old 12-15-2005, 02:34 PM   #2
cyent
Member
 
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 273

Rep: Reputation: 48
string s;
int i;

config >> s;
config >> i;
 
Old 12-15-2005, 02:48 PM   #3
Artanicus
Member
 
Registered: Jan 2005
Location: Finland
Distribution: Ubuntu, Debian, Gentoo, Slackware
Posts: 827

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by cyent
string s;
int i;

config >> s;
config >> i;
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..
 
Old 12-15-2005, 03:29 PM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
nope. the >> operator does that for you.
 
Old 12-15-2005, 05:13 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
I like the approach that the C++ FAQ takes
http://www.parashift.com/c++-faq-lit....html#faq-39.2
http://www.parashift.com/c++-faq-lit....html#faq-39.3



Cheers,
Tink
 
Old 12-16-2005, 12:47 AM   #6
Artanicus
Member
 
Registered: Jan 2005
Location: Finland
Distribution: Ubuntu, Debian, Gentoo, Slackware
Posts: 827

Original Poster
Rep: Reputation: 31
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..
 
Old 12-18-2005, 02:28 PM   #7
cyent
Member
 
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 273

Rep: Reputation: 48
Ok. Here is the secret.

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..
 
Old 12-18-2005, 02:31 PM   #8
Artanicus
Member
 
Registered: Jan 2005
Location: Finland
Distribution: Ubuntu, Debian, Gentoo, Slackware
Posts: 827

Original Poster
Rep: Reputation: 31
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*

Thanks for the help all, I learned alot (:
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do i read the content of a CD? therealkilkenny Linux - Newbie 6 12-09-2005 10:43 AM
awk and change file content Ayman.mashal Programming 4 06-02-2005 07:03 AM
Read KDE clipboard content neranjana Linux - General 3 05-10-2005 01:12 PM
find the same content in the file ust Linux - Newbie 5 03-23-2005 04:03 AM
invalid types int[int] for array subscript scuzzman Programming 2 11-16-2004 10:34 PM


All times are GMT -5. The time now is 06:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration