You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

Using STD lib

Useful includes

#include <stdlib.h>
#include <string>   // for string, substring
#include <sstream>  // for streamstring
#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

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

Formatting of the string

Formatting of the string

double val = 12345.26374859560;
std::stringstream ss; ss << std::setw(8) << std::setprecision(8) << std::right << val; 
std::string s = ss.str();

Print long space

std::cout << std::setw(80) << " " << std::setw(4);

Check if the element is in the vector or list

  T element = <instatiation>;
  std::vector<T> v; v=<initialization>;
  if (std::find(v.begin(), v.end(), element) != v.end()) {/*element is in v*/}
  else {/* is not there */}

Add the element to the vector or list if it is not there

  T element = <instatiation>;
  std::vector<T> v; v=<initialization>;
  if (std::find(v.begin(), v.end(), element) == v.end()) v.push_back(element);

Time

#include <stdio.h> // for printf
#include <time.h>
#include <iostream> // for cout
using namespace std;

int main ()
{ 
  // Get current time in seconds since January 1, 1970:
  time_t seconds;
  seconds = time (NULL);
  cout << seconds << " sec  since January 1, 1970\n";

  // Another form of the time access method:
  time_t  time_sec;
  time ( &time_sec );
  struct tm* timeinfo; timeinfo = localtime ( &time_sec ); 

  cout << "time_sec = " << time_sec << endl;
  cout << "ctime - converts time_sec in char*    : " << ctime (&time_sec);

  char* p_buf = asctime(timeinfo);  
  cout << "asctime - converts struct tm* in char*: " << p_buf;  

  char c_time_buf[80];  strftime (c_time_buf,80,"%Y-%m-%d %H:%M",timeinfo);
  cout << "strftime - user-formatted time char*  : " << c_time_buf << endl;  

  printf ( "struct tm* members:\n");
  printf ( "sec  = %d\n", timeinfo->tm_sec  );
  printf ( "min  = %d\n", timeinfo->tm_min  );
  printf ( "hour = %d\n", timeinfo->tm_hour );
  printf ( "mday = %d\n", timeinfo->tm_mday );
  printf ( "mon  = %d\n", timeinfo->tm_mon  );
  printf ( "year = %d\n", timeinfo->tm_year );
  printf ( "wday = %d\n", timeinfo->tm_wday );
  printf ( "yday = %d\n", timeinfo->tm_yday );
  printf ( "isdst= %d\n", timeinfo->tm_isdst);
 
  return ( EXIT_SUCCESS ); 
}

Precise time

Use another time structure, which accounts for sec and nsec.

#include <time.h>   // time
  struct timespec start, stop;
  int gettimeStatus = clock_gettime( CLOCK_REALTIME, &start );

  printf ( "gettimeStatus: %d \n", gettimeStatus );
  printf ( "start.tv_sec = %d  start.tv_nsec = %d \n", start.tv_sec, start.tv_nsec );

      gettimeStatus = clock_gettime( CLOCK_REALTIME, &stop );
  printf ( " stop.tv_sec = %d   stop.tv_nsec = %d \n", stop.tv_sec,  stop.tv_nsec );
  printf ( "      dt_sec = %d        dt_nsec = %d \n", stop.tv_sec - start.tv_sec, 
                                                       stop.tv_nsec- start.tv_nsec);

Using boost

Iterate over files in the directory

#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;

  fs::path path = "/reg/d/psdm/CXI/cxi49012/xtc/e158-r0150-s00-c01.xtc";
  fs::path dir  = path.parent_path(); // i.e.: /reg/d/psdm/CXI/cxi49012/xtc

  typedef fs::directory_iterator dir_iter;
  for (dir_iter dit = dir_iter(dir); dit != dir_iter(); ++ dit) {
    std::cout << dit->path().stem().string() << "\n";  
  } 

Other methods of fs::path

  fs::path path = m_path;
  std::cout << "stem          = " << path.stem().string() << "\n";            // e158-r0150-s00-c00
  std::cout << "extension()   = " << path.extension().string() << "\n";       // .xtc
  std::cout << "filename()    = " << path.filename().string() << "\n";        // e158-r0150-s00-c00.xtc
  std::cout << "parent_path() = " << path.parent_path().string() << "\n";     // /reg/d/psdm/CXI/cxi49012/xtc
  • No labels