开发者

How to read a word between "" with ifstream?

开发者 https://www.devze.com 2023-01-26 06:26 出处:网络
ini file that contains: ad开发者_如何转开发dress=\"localhost\" username=\"root\" password=\"yourpassword\"

ini file that contains: ad开发者_如何转开发dress="localhost" username="root" password="yourpassword" database="yourdatabasename"

and I need to find the word between the two "" with ifstream, and put it in a char.

Is there a way to do this??


I would approach it as follows:

  • Create a class that represents the name-value pair
  • Use std::istream& operator>>( std::istream &, NameValuePair & );

You can then do something like:

ifstream inifile( fileName );
NameValuePair myPair;
while( ifstream >>  myPair )
{
   myConfigMap.insert( myPair.asStdPair() );
}

If your ini file contains sections, each of which contains named-value pair, then you need to read to end-of-section so your logic would not be using stream failure but would use some kind of abstract factory with a state machine. (You read something then determine what it is thus determining your state).

As for implementing the stream read into your name-value pair it could be done with getline, using the quote as a terminator.

std::istream& operator>>( std::istream& is, NameValuePair & nvPair )
{
   std::string line;
   if( std::getline( is, line, '\"' ) )
   {
     // we have token up to first quote. Strip off the = at the end plus any whitespace before it
     std::string name = parseKey( line );
     if( std::getline( is, line, '\"' ) ) // read to the next quote.
     {
        // no need to parse line it will already be value unless you allow escape sequences
        nvPair.name = name;
        nvPair.value = line;
     }
  }
  return is;
}

Note that I did not write into nvPair.name until we had fully parsed the token. If the streaming failed we don't want to partially write.

The stream will be left in a fail state if either getline failed. This will happen naturally at end-of-file. We don't want to throw an exception if it fails for that reason because that is the wrong way to handle end-of-file. You could throw if it fails between the name and the value, or if the name did not have the trailing = sign (but is not empty) because that is not a natural occurrence.

Note that this allows spaces and even newlines between the quotes. Whatever is between them is read other than another quotes. You would have to use an escape sequence to allow those (and parse the value).

If you used \" as the escape sequence then when you get the value you must "loop" if it ends with a \ (as well as changing it to a quote), and concatenate them together.


If there are newlines between each couple you can do the following.

std::string line; //string holding the result
char charString[256]; // C-string

while(getline(fs,line)){ //while there are lines, loop, fs is your ifstream
    for(int i =0; i< line.length(); i++) {
        if(line[i] != '"') continue; //seach until the first " is found

        int index = 0;
        for(int j= i+1; line[j] != '"'; j++) {
            charString[index++] = line[j];
        }
        charString[index] = '\0'; //c-string, must be null terminated

        //do something with the result
        std::cout << "Result : " << charString << std::endl;

        break; // exit the for loop, next string
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消