[LC++]scanning files

Torsten Rennett Torsten at Rennett.de
Tue Sep 28 04:22:02 UTC 2004


On Montag, 27. September 2004 14:01, Julien Claassen wrote:
>   I have a simple probelm, but seem to be completely stupid. I have a
> file, with a simple format, imagine that:
>   Filename is myfile.cfg
>    Entry in this file is:
>   directory = /usr/share/something
>   Now I want to read this in a variable. With c I would have done:
>   ...
>   fscanf(file_pointer,"directory = %s",&my_string);
>   ...
>   How would I do this with c++ in an ellegant manner?

Try something like this:

  ifstream ifs("myfile.cfg");	// "directory = /usr/share/something"
  string my_string;
  string buf;

  getline(ifs, buf, '=');	// buf = "directory "
  getline(ifs, my_string);	// my_string = "/usr/share/something"

Or something like this:

  ifs >> buf;			// buf = "directory"
  ifs >> buf;			// buf = "="
  ifs >> my_string;		// my_string = "/usr/share/something"


Torsten

-- 
Ingenieurbuero RENNETT      -- innovative Software-Entwicklung --
Torsten Rennett                    http://www.RENNETT.de
Ludwig-Thoma-Weg 14         E-Mail:            Torsten at Rennett.de
D-85551 Heimstetten         Telefon:            +49 89 904 805 38




More information about the tuxCPProgramming mailing list