Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
#include <stdlib.h>
#include <string>   // for string, substring
#include <sstream>  // for stringstream
#include <iostream> // for cout, puts etc.
#include <stdio.h>  // for  sprintf, printf( "%lf\n", accum );
#include <time.h>   // time
#include <fcntl.h>  // open()
#include <unistd.h> // read()
#include <iomanip>  // for setw
#include <fstream>  // for ifstream

using namespace std; // allows get rid of std:: prefix.

...

Code Block
std::string s("121 151 225 456");
std::stringstream ss(s);
T val;
do { ss >> val; cout << val << endl; } while( ss.good() ); 

 

...

Check if file exists

Code Block
#include <fstream>  // for ifstream

  std::ifstream f("file.txt");
  if(not f.good()) ...

 

Input from file value by value

Code Block
#include <fstream>  // for ifstream

  std::ifstream f("file.txt");
  std::string val;
  while (f>>val) {
      std::cout << "val: " << val << '\n';
  }

  f.close();

Input from file line by line

...

Code Block
#include <fstream>  // for ifstream

  std::ifstream f("file.txt");
  std::string line;
  while (std::getline(f, line)) {
      std::cout << "line: " << line << '\n';
  }

  f.close();

 

 

...

Print long space or repeated character

...