Hello everyone:

I'm a C++ newbie.

I have written code for performing calculations that needs data from another code. These data are in text files. I know how to read in an ENTIRE text file, but I can't figure out how to extract certain pieces of it. I would very much appreciate any help and an example would be very welcome. I thought of using ignore and getline from istream, but I can't figure out how to extract only the information I need.

Below is an example of what is in each text file. I need to extract the names of the elements only into a vector/container. So I need O, W, Re, H, He, K, Na, Y, and O--not the '' symbols. The position of each element in the vector will then be used to perform further calculations.

File is called output. Here's what is in it:

## (5) Elements section
# OVMZ: Name list of elements
<OVMZ>
'O' 'W' 'Re' 'H' 'He' 'K' 'Na' 'Y' 'O'

Hello everyone:

I'm a C++ newbie.

I have written code for performing calculations that needs data from another code. These data are in text files. I know how to read in an ENTIRE text file, but I can't figure out how to extract certain pieces of it. I would very much appreciate any help and an example would be very welcome. I thought of using ignore and getline from istream, but I can't figure out how to extract only the information I need.

Below is an example of what is in each text file. I need to extract the names of the elements only into a vector/container. So I need O, W, Re, H, He, K, Na, Y, and O--not the '' symbols. The position of each element in the vector will then be used to perform further calculations.

File is called output. Here's what is in it:

## (5) Elements section
# OVMZ: Name list of elements
<OVMZ>
'O' 'W' 'Re' 'H' 'He' 'K' 'Na' 'Y' 'O'

1) Look for first instance of a quotation mark.
2) This signifies the start of the element name.
3) Record everything until the next quotation mark, which will be the end.
4) Repeat this process until end of file.

Will your container be holding the names of the elements as strings? If so, try something like this (untested):

.
.
.
vector<string> Elements;
string element = "";
while (fin.get() != EOF){

  while (fin.get != '\'') //open quote. the escape sequence \' allows you to look for '
    {fin.ignore(1);}  //ok found the open quote, ignore that character

  while (fin.get() != '\'') //so now we're in.  record chars while no close quote
    {element += fin.get();} //push characters 1 at a time onto a string until close quote

  Elements.push_back(element);
  element.clear(); //reset the string for next time until EOF.  clear is a member function of string right?  can't remember.  Else set it to "".
}

-Greywolf

This article has been dead for over six months. Start a new discussion instead.