[Linux] File Operations

  • 顯示檔案,依照時間排序
ll -t
  • 查詢檔案
find targetdir targetfile
find targetdir -type mytype -name targetfile
  • 刪除檔案
rm -rf targetfile
-r:遞迴刪除資料夾內所有內容
-f:強制刪除
  • 壓縮/解壓縮/檢視
#壓縮
tar -zcvf out.tar.gz sourcedir
#解壓縮
tar -zxvf in.tar.gz -C targetdir
#檢視
tar -ztvf in.tar.gz
  • 複製檔案
#複製單一檔案並改名為targetfile
cp sourcefile targetfile
#複製單一檔案至某目錄下
cp sourcefile targetdir
#複製資料夾
cp -r sourcedir targetdir
  • 移動(更名)檔案
mv sourcefile targetfile
#一次移動多個檔案
mv -t targetdir file1 file2 ...
#格式化更名檔案,將matchfile符合exp1的字替換成exp2
rename 's/exp1/exp2/g' matchfile

[Linux] Disk Operations

  • 觀察磁碟分割狀態
lsblk
freeBSD:
camcontrol devlist

example:

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1.5G 0 rom /media/hubert/Ubuntu 16.04.3 LTS amd64
sda 8:0 0 40G 0 disk 
├─sda2 8:2 0 1K 0 part 
├─sda5 8:5 0 2G 0 part [SWAP]
└─sda1 8:1 0 38G 0 part /
  • 磁碟分割與掛載
df mydir #查看zfs分割掛載
gpart show mydisk #查看gpt分割
  • 分辨磁碟分割是GPT還是MBR
fdisk -l /dev/sda

example:

若出現:WARNING: GPT (GUID Partition Table) detected,則是GPT
若正常顯示磁碟分割,則是MBR
  • 退出CD
eject

[C++] Using msgpack-c with FIO

Output serialized objects to ofstream:

#include <msgpack.hpp>
 ofstream out("output.txt");
 msgpack::packer<std::ofstream> pk(&out);
 //packing an integer
 int n=10;
 pk.pack(n);
 //packing a integer list
 vector<int> num_list;
 num_list.push_back(1); num_list.push_back(2); num_list.push_back(3);
 pk.pack(num_list);
 //packing a integer list one by one 
 pk.pack_array(num_list.size());
 for(int j = 0;j<num_list.size();j++){
   pk.pack(num_list[i]);
 }

Input serialized object from ifstream:

ifstream in("input.txt",ios::binary);
stringstream buffer;
buffer << in.rdbuf();

msgpack::unpacker pac;
// feeds the buffer.
pac.reserve_buffer(buffer.str().size());
memcpy(pac.buffer(), buffer.str().data(), buffer.str().size());
pac.buffer_consumed(buffer.str().size());

// now starts streaming deserialization.
msgpack::object_handle oh;
while(pac.next(oh)) {
 std::cout << oh.get() << std::endl;
}

[C++] cout formatting

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
 float x = 123.123556;
 //output:123.124
 cout << fixed <<setprecision(3) << x << endl;
 return 0;
}

[C++] log(),log10()

#include<cmath>
//using natural log
cout<<log(10)<<endl;

//using 10 based log
cout<<log10(10)<<endl;

[C++] max_element()&min_element()

#include <algorithm>    // std::min_element, std::max_element
bool myfn(int a,int b){
  return a < b;
}
int myints[] = {3,7,2,5,6,4,9};

// using default comparison:
cout << "The smallest " << *min_element(myints,myints+7) << endl;
cout << "The largest "  << *max_element(myints,myints+7) << endl;

// using function myfn as comp:
cout << "The smallest " << *min_element(myints,myints+7,myfn) << endl;
cout << "The largest "  << *max_element(myints,myints+7,myfn) << endl;

[C++] map–usage

Search by key:

map<char,int>::iterator it;
it = mymap.find('b');
if (it != mymap.end())
    //found!
else
    //not found!

Traversal

for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
    cout<<iter->first<<" "<<iter->second<<endl;

Sorting:

bool compFunc(pair<char, int=""> a, pair<char, int=""> b) {
   //sort whatever you want (by key, value or both)
}
map<char, int> lan_list;
vector< pair<char, int> > sort_tmp(lan_list.begin(), lan_list.end());
sort(sort_tmp.begin(), sort_tmp.end(), compFunc);

[Python] Big5 and utf-8

中文的 windows cmd 編碼預設是Big5(cp950) ,而 Python3 的預設程式碼編碼是utf-8 (cp65001),如果在輸出時產生 「UnicodeEncodeError: ‘cp950’ codec can’t encode character … …」的錯誤必須在程式開頭加上:

# -*- coding: utf-8 -*-

若程式會在console輸出,則使用

print(mytext.encode(sys.stdin.encoding, "replace").decode(sys.stdin.encoding))

若使用檔案輸出,則必須在open中指定編碼參數:

out_file = open("File.txt","w",encoding="utf-8")

[C++] next_permutation

#include <iostream>     
#include <algorithm>    
using namespace std;
int main () {
  int myints[] = {1,2,3};

  sort (myints,myints+3);
  
  do {
    cout << myints[0] << ' ' << myints[1] << ' ' << myints[2] << endl;
  } while ( next_permutation(myints,myints+3) );

  cout << "After loop: " << myints[0] << ' ' << myints[1] 
        << ' ' << myints[2] << endl;

  return 0;
}

output:

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
After loop: 1 2 3