Skip to content

Instantly share code, notes, and snippets.

View RyosukeMiyahara's full-sized avatar

Ryosuke Miyahara RyosukeMiyahara

View GitHub Profile
@RyosukeMiyahara
RyosukeMiyahara / popen.cpp
Created July 21, 2014 05:45
system() VS popen()
#include <iostream> // cout, endl;
#include <stdio.h> // popen, pclose
int main(void) {
std::cout << "Caller starts" << std::endl;
FILE *fp = popen("./writeTest", "r");
pclose(fp);
std::cout << "Caller ends" << std::endl;
@RyosukeMiyahara
RyosukeMiyahara / fork.cpp
Last active August 29, 2015 14:03
fork() usage
#include <iostream> // cout, endl
#include <unistd.h> // getpid()
#include <stdlib.h> // exit()
int main(void) {
std::cout << "(First) Process ID is " << getpid() << std::endl;
// Create child process
int forkReturnValue = fork(); // --- Process forks here! ---
@RyosukeMiyahara
RyosukeMiyahara / replaceString.cpp
Created March 14, 2014 14:07
C++で、文字列の一部を置換する
#include <iostream> // cout, endl
std::string stringReplace(const std::string target,
const std::string from,
const std::string to);
// First command line argment is target string to replace
// Second command line argument is replaced to Third command line argument
int main(int argc, char *argv[]) {
if (argc != 4) {
@RyosukeMiyahara
RyosukeMiyahara / checkDirEmpty.cpp
Created March 13, 2014 14:30
C++で指定したディレクトリが空かどうかをチェックする
#include <iostream> // cout, endl
#include <dirent.h> // DIR, opendir, readdir, struct dirent
#include <string.h> // strcmp
#include <errno.h> // errno
#include <stdlib.h> // exit
bool checkDirEmpty(std::string targetDir);
int main(void) {
std::string emptyDirPath = "emptyDir";
@RyosukeMiyahara
RyosukeMiyahara / pthreadCallMethod.cpp
Created March 12, 2014 14:10
C++で、pthread_createで作ったスレッドから、クラスのstaticではないメンバ関数を呼びます。
#include<stdio.h>
#include<pthread.h> // pthread_create
#include<unistd.h> // sleep
extern "C" {
typedef void* (*ThreadFunc_t)(void*);
}
class Test {
private:
@RyosukeMiyahara
RyosukeMiyahara / checkExist.cpp
Created March 11, 2014 14:51
C++でファイルやフォルダが存在するかどうかを確認する
#include <iostream> // cout, endl
#include <sys/stat.h> // stat
int main(void) {
struct stat statFile;
struct stat statDirectory;
struct stat statNothing;
// if file/directory exists, stat() returns 0
// if file/directory does not exist, stat() returns -1
@RyosukeMiyahara
RyosukeMiyahara / atoi.cpp
Created March 10, 2014 13:43
C++でString (char配列)をintegerに変換します。
#include <iostream> // cout, endl
#include <cstdlib> // atoi
// Convert string (char array) to integer
int main() {
std::string string123("123"); // This is string
// Convert string (char array) to integer using atoi
if (std::atoi(string123.c_str()) == 123) {
std::cout << "std::atoi(string123.c_str()) is 123(int)" << std::endl;
@RyosukeMiyahara
RyosukeMiyahara / blink.go
Created January 22, 2014 12:05
go-blink: Achieve blink on Raspberry Pi by golang