Skip to content

Instantly share code, notes, and snippets.

@blippy
blippy / jup-01.ipynb
Created February 28, 2016 11:53
Share selection Nov 2015
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@blippy
blippy / input.cc
Created March 11, 2016 14:52
Example of reading in a file in C++
#include "mcstats.h"
#include <assert.h>
#include <stdlib.h>
//#include <stdio>
#include <iostream>
#include <fstream>
#include <string>
using std::cout;
@blippy
blippy / strim.cc
Created March 15, 2016 12:14
trim a string of a set of characters
const char white[] = " \t\r";
string trim(string& str)
{
if(str.length() ==0) { return str;}
size_t first = str.find_first_not_of(white);
if(first == string::npos) return "";
size_t last = str.find_last_not_of(white);
return str.substr(first, (last-first+1));
}
@blippy
blippy / mtime.cc
Last active March 15, 2016 14:57
Formatted creation time of a file
#include <sys/stat.h>
...
struct stat st;
stat("s2/yfetch.csv", &st);
time_t t = st.st_mtime;
struct tm atm;
localtime_r(&t, &atm);
char dtstamp[80];
@blippy
blippy / xterm.desktop
Created March 31, 2016 10:34
Add xterm to menus
[Desktop Entry]
Version=0.0.0
Type=Application
Name=Xterm Terminal
GenericName=Xterm Terminal
Comment=A lightweight terminal
Icon=/usr/share/icons/nuoveXT2/22x22/apps/terminal.png
Exec=xterm %F
#Terminal=false
MimeType=text/html;
@blippy
blippy / istream.cc
Last active March 13, 2018 16:08
Reading from cin or file
#include <iostream> // std::cout
#include <fstream> // std::ifstream
#include <string>
void reading()
{
std::string filename = ... ;
std::ifstream ifs;
if(filename.size()>0) ifs.open(filename);
@blippy
blippy / ostream.cc
Last active June 19, 2016 10:12
Example of writing to cout or filename
#include <fstream>
#include <iostream>
void prin_vecvec(vecvec_t & vvs, const char *sep, const char *recsep, const char *filename )
{
std::ofstream ofs;
bool use_file = strlen(filename);
if(use_file) ofs.open(filename, std::ofstream::out);
ostream &os = use_file ? ofs : cout ;
@blippy
blippy / naps.cob
Created May 16, 2016 13:48
An implementation of naps in COBOL
000100 identification division.
000200 program-id. naps.
000300 environment division.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT EPICS-FILE ASSIGN TO
"/home/mcarter/.mca/work/s3/epics.rep"
ORGANIZATION IS LINE SEQUENTIAL.
select sectors-file assign to
"sectors.txt"
@blippy
blippy / fmtd.cc
Last active August 16, 2018 20:25
Format a double for output
std::string dout(double d)
{
std::ostringstream s;
s.precision(2);
s.width(10);
s << std::fixed;
s << d;
return s.str();
}
@blippy
blippy / slurp.cc
Last active March 1, 2021 21:06
Slurp a file in C++
#include <fstream>
#include <sstream>
std::string slurp(const char *filename)
{
std::ifstream in;
in.open(filename, std::ifstream::in | std::ifstream::binary);
std::stringstream sstr;
sstr << in.rdbuf();
in.close();