Skip to content

Instantly share code, notes, and snippets.

@knknkn1162
Last active November 16, 2016 23:40
Show Gist options
  • Save knknkn1162/f8496eb5fb6d67f07253c36e02924b28 to your computer and use it in GitHub Desktop.
Save knknkn1162/f8496eb5fb6d67f07253c36e02924b28 to your computer and use it in GitHub Desktop.
いっつも忘れるC++ ファイル入出力関数を図にしてみた ref: http://qiita.com/knknkn1162/items/47736785a3e472db5525
dec // 10 進表記
endl // 改行文字の出力
ends // 空白文字の出力
flush // ストリームのフラッシュ
hex // 16 進表記
oct // 8 進表記
resetioflags(long f) // f で指定されたフラッグを off
setbase(int base) // 基数を base にする
setfill(int ch) // 文字 h で埋める
setioflags(long f) // f で指定されたフラッグを on
// 小数点以下 p 桁(デフォルトは 6 桁.固定小数点の場合は,小数点や符号を除いた全体の桁数になり,表示できないときは浮動小数点表示となる)
setprecision(int p)
setw(int w) // 出力幅を w
ws // 先頭の空白を読み飛ばす
#include <iostream> //std::right
#include <iomanip> //std::setw(int w)
int main(){
double a=3.14159;
//混在できる
std::cout << std::setw(10) << std::right << a << std::endl; //output : " 3.14159"
return 0;
}
#include <string>
#include <iostream> //std::right, std::out, std::endl
#include <iomanip> //std::setw(int w)
#include <fstream> // ifstream, ofstream
#include <sstream> // istringstream, ostringstream
/* sample.txt
001.bmp,0,34
002.bmp,5,563
003.bmp,4,979
*/
int main()
{
//入力ストリームの作成
const std::string input = "sample.txt";
std::ifstream ifs(input, std::ios::in);
//出力ストリームの作成
const std::string output = "output.txt";
std::ofstream ofs(output, std::ios::out);
std::string buf;
char linedelimiter = ',';
//一行づつ読み込む
while (std::getline(ifs, buf))
{
std::string fn;
std::istringstream iss(buf);
getline(iss, fn, linedelimiter);
int num1, num2;
char dummy; //','をよみこませるためのdummy
iss >> num1 >> dummy >> num2;
int num = num1*num2;
//もしくは
//std::string data;
//int num = 1;
//while (std::getline(iss, data, linedelimiter))
//{
// num *= std::stoi(data);
//}
//ファイルに書き込み
//書式設定(下記)を参照のこと
ofs << fn << std::setw(10) << std::right << num << std::endl;
//ついでに標準出力にも。
std::cout << fn << std::setw(10) << std::right << num << std::endl;
}
return 0;
}
/* output.txt
001.bmp 0
002.bmp 2815
003.bmp 3916
*/
skipws = 0x0001 // ストリームの入力中,先頭の空白文字を読み飛ばす
left = 0x0002 // 出力を左寄せ
right = 0x0004 // 出力を右寄せ
internal = 0x0008 // 符号と数字の間にブランクを入れ出力幅一杯にする
dec = 0x0010 // 10進表記(デフォルト)
oct = 0x0020 // 8進表記
hex = 0x0040 // 16進表記
showbase = 0x0080 // 数値の基数の表示
showpoint = 0x0100 // 浮動小数点の出力で小数点,右端の0,eを表示
uppercase = 0x0200 // 浮動小数点のe等を大文字で表示
showpos = 0x0400 // 正の数値の前に+を表示
scientific = 0x0800 // 実数値を浮動小数点表示
fixed = 0x1000 // 固定小数点表示(デフォルト)
unitbuf = 0x2000 // 出力の度に,すべてのストリームをフラッシュ
stdio = 0x4000 // 出力の度に,stdout及びstderrをフラッシュ
int width(int <長さ>); // 出力幅制御
char fill(char <文字>); // 空白を埋める文字
// 小数点以下の桁数(デフォルトは 6 桁.固定小数点の場合は,小数点や符号を除いた全体の桁数になり,
//表示できないときは浮動小数点表示となる
int precision(int <桁数>);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment