Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ basic file input

Name: Anonymous 2007-01-18 8:48

Someone just post me up the code to read in data from a file.

the file is like this:

24 234.32

i need to set the int to a variable and the double to another, i cant find very many places to do this.. they all use get.line etc.

Think i am made of fail

anyways HALP ME 4chan

Name: Anonymous 2007-01-18 13:49

>>1

#include <fstream>
#include <iostream>

int main( ) {
   using namespace std;

   ifstream file("data.txt");
   streambuf *pbuf;
   int foo;
   double bar;

   try {
      pbuf = file.rdbuf();
      cin.rdbuf(pbuf);
      cin >> foo >> bar;
      cout << foo << ' ' << bar << endl;

      file.close();
   }
   catch( ... ) {
      cerr << "We're fucked... quitting... " << endl;
   }

   return 0;
}


$ ./a
24 234.32

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List