Advertisement

How to use fscanf with streams/strings

Started by February 25, 2004 07:59 PM
3 comments, last by Corrail 21 years ago
Hi all! I want to write a text file reader in C++ using the standart C++ library (ifstream, string, ...). But how can I convert a code like:

char buf0[100];
char buf1[100];
int tmp0, tmp1;

fscanf(file, "%s - %d : %d type: %s", buf0, &tmp0, &tmp1, buf1);
How can I do this with the C++ standart library? Thanks a lot [EDIT] pressed too early create threat :-) -------------------------------------------------------- There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable. There is another theory which states that this has already happened... [edited by - Corrail on February 25, 2004 9:02:05 PM]
--------------------------------------------------------There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.There is another theory which states that this has already happened...
#include <iostream.h>#include <fstream.h>char buf0[100];char buf1[100];int tmp0, tmp1;char c=0;                // temporary charifstream file(filename); // open file stream for inputfile.getline(buf0,'-');  // read a string from the file till a '-' character is foundfile>>tmp0;              // read an integer from the filefile>>tmp1;              // read another integer from the filewhile(c!=':'){           // get a character untill it is ':'  file>>c;};file.getline(buf1);      // get another string and stop when it finds a newline character (default)


You might want to remove a space in buf1 (as there should be one according to your scanf code)
So, it isn't that efficient but it is possible.
EDIT: typo

[edited by - Tree Penguin on February 26, 2004 5:57:50 AM]
Advertisement
I would read the file line per line and would make a sscanf afterward.
sscanf isnt c++ standard libary thou is it??
I have this very same problem with my .obj loader. I want to make it all c++ standard which means removing my usage of sscanf
AP:
Why? If the sscanf() method works for you, why change it? Unless you want to see if you CAN do it or further your comprehension of the language, it''s pretty much pointless (and time-consuming) to change it AFAIK. (Same applies to OP on all points)

But then again, I don''t know it all. All I know is that ''if it ain''t broke, don''t fix it''.
Things change.

This topic is closed to new replies.

Advertisement