Skip to content

Instantly share code, notes, and snippets.

Avatar

Ryosuke Miyahara RyosukeMiyahara

View GitHub Profile
@RyosukeMiyahara
RyosukeMiyahara / popen.cpp
Created Jul 21, 2014
system() VS popen()
View popen.cpp
#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;
View fork.cpp
#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 Mar 14, 2014
C++で、文字列の一部を置換する
View replaceString.cpp
#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 Mar 13, 2014
C++で指定したディレクトリが空かどうかをチェックする
View checkDirEmpty.cpp
#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 Mar 12, 2014
C++で、pthread_createで作ったスレッドから、クラスのstaticではないメンバ関数を呼びます。
View pthreadCallMethod.cpp
#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 Mar 11, 2014
C++でファイルやフォルダが存在するかどうかを確認する
View checkExist.cpp
#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 Mar 10, 2014
C++でString (char配列)をintegerに変換します。
View atoi.cpp
#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 Jan 22, 2014
go-blink: Achieve blink on Raspberry Pi by golang
View blink.go