Thread: C++ fscanf equivalent?

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    38

    C++ fscanf equivalent?

    Is there an equivalent to fscanf in c++?
    I need to read a float from an ifstream. In C, it would be something like the following:

    Code:
    fscanf(file, "%f", &dist);
    In C++, I could use >> maybe. But is there a safer way of doing this? The way I would think of doing it in C++ is:

    Code:
    float dist;
    ifstream file;
    ...
    ...
    file >> dist;
    Is there a better way than this? Or is this pretty standard?
    Thanks
    Mark

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Well fscanf isn't the best way to do it in C either - although I have
    my suspicions that >> operator for your filestream is better than
    fscanf. The ideal way to read from a file that contains a lot
    of information is to read in line by line (using getline for C++) and
    parse the data you need. thats why files typically have a
    repetitive stucture if they need to be processed by a program
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I believe that is what you are looking to use.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    C gurus recommend fgets() + sscanf() for files. Similiarly, for C++, you could use getline() and stringstream.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    there's always getline()

    EDIT: just saw jafet's post
    Last edited by twomers; 05-10-2006 at 09:25 AM.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by markucd
    Is there an equivalent to fscanf in c++?
    I need to read a float from an ifstream. In C, it would be something like the following:

    Code:
    fscanf(file, "%f", &dist);
    In C++, I could use >> maybe. But is there a safer way of doing this? The way I would think of doing it in C++ is:

    Code:
    float dist;
    ifstream file;
    ...
    ...
    file >> dist;
    Is there a better way than this? Or is this pretty standard?
    Thanks
    Mark
    using the >> operator is actually quite safe - the stream will fail if there is a problem with the data (eg, attempting to input an ASCII char into a double) - the real challenge is handling exceptions which occur when something does go wrong. eg,
    Code:
    if (!(file >> dist))
    {
       //Something happened reading from 'file'
       std::cout << "Error!" << std::endl;
       exit(EXIT_FAILURE);
    }
    // OK - data from 'file' was successfully read into 'dist'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-29-2008, 12:11 AM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 09:31 PM
  3. Pointer equivalent to array notation
    By bekkilyn in forum C Programming
    Replies: 4
    Last Post: 12-06-2006, 08:22 PM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  5. fscanf on sun's
    By brif in forum C Programming
    Replies: 2
    Last Post: 04-14-2002, 01:22 PM