1

I'm starting to write C++ and I'm trying to read a text file that is formatted like this:

32.0      12.43
503.2     3.212

etc.

In Java, I could use a Scanner with .nextFloat(), and put the contains into an Array. I am trying to achieve the same thing with C++. How is this done?

Derek
  • 9,773
  • 7
  • 29
  • 34

3 Answers3

6

To read from standard input

#include <iostream>

int main()
{
   float f1, f2, f3, f4;
   std::cin >> f1 >> f2 >> f3 >> f4;       
}

To read from a file

#include <fstream>

int main()
{
   std::ifstream fin("file.txt"); //fin is an arbitrary identifier
   float f1, f2, f3, f4;
   fin >> f1;
   fin >> f2;
   fin >> f3;
   fin >> f4;  // or fin >> f1 >> f2 >> f3 >> f4; no difference     
}

To read all floats into an array (vector) of floats, you can use this:

#include <fstream> 
#include <vector>
#include <iterator> //for istream_iterator and back_inserter
#include <algorithm> //for copy

int main()
{
    std::ifstream fin("input.txt");
    std::vector<float> v; //could also initialize with the two-iterator constructor
    std::copy(std::istream_iterator<float>(fin), 
              std::istream_iterator<float>(), 
              std::back_inserter(v)); //push_back into vector
}

Read more about stream input/output here and elsewhere. I would also strongly advise that you read a good C++ book

Community
  • 1
  • 1
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
0

C++ has many good native options to read data, e.g., std::cin for user input data and std::ifstream for files. But if you are looking for a quite similar implementation of Java Scanner you can find it in the open source project called Scanner++: http://sourceforge.net/projects/scannerpp/

Many good features of Java Scanner class are implemented there, including the nextFloat() method.

#include "Scanner.h"

using namespace scannerpp;

int main()
{
    // to work with strings
    Scanner scanner("32.0   12.43 \n 503.2   3.212");
    float x = scanner.nextFloat();

    // to work with files
    Scanner scanner(new File("test-data.txt"));
    float x2 = scanner.nextFloat();
}

Hope this helps!

DISCLAIMER: I'm co-author of Scanner++ project.

-2

You can build a String Tokenizer using string::find() or use strtok() or use Boost Tokenizer. I don't know of a pre-existing Scanner equivalent.

Also look at this thread: Converting from Java to C++ Reading a File, and parsing

Community
  • 1
  • 1
Sid
  • 7,511
  • 2
  • 28
  • 41
  • It's letting him know that there aren't many robust Scanner equivalents. And giving him a few options to write one himself. How does it not? – Sid Feb 02 '12 at 21:15
  • I did. It's a good answer but I don't think it's a Scanner equivalent. What if he has a more complex file format or a larger number of floats? – Sid Feb 02 '12 at 21:17
  • I didn't downvote you but you should've answered the answer pertaining to Q, rather than thinking about what if xyz happens next. – Mr.Anubis Feb 02 '12 at 21:21
  • That's okay. I still think this is a valid answer. If someone wants a Scanner equivalent then they would probably want to use it in other problems then the one case stated. We don't have to agree. – Sid Feb 02 '12 at 21:23
  • Note that for more complex formats, [Boost.Spirit.Qi](http://www.boost.org/libs/spirit/) would be my recommendation. – ildjarn Feb 02 '12 at 21:45