Last active
February 28, 2017 12:18
-
-
Save muzudho/4f2f15aa25a1d05e88bcdeb53922c54a to your computer and use it in GitHub Desktop.
[C++] テキストファイル読書き (C#プログラマ向け) ref: http://qiita.com/muzudho1/items/fa1446b85ae64d01a5ca
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Console_Kifuwarabe.cpp : Defines the entry point for the console application. | |
// | |
#include "stdafx.h" | |
#include <iostream> | |
#include "UtilFile.h" | |
int main() | |
{ | |
std::cout << "Hello, World." << std::endl; | |
UtilFile::Write("test.txt", "ほっほ☆(^▽^)\nどうだぜ☆(^~^)?"); | |
std::string contents = UtilFile::Read("test.txt"); | |
std::cout << contents << std::endl; | |
// 横軸はアルファベットにする☆(1桁で済む) Iを飛ばすのは 少なくともわたしはグニュー碁1.2(1995年)では見かけた昔からある習慣☆ 縦棒と区別するぜ☆(^▽^) | |
std::cout << " |ABCDEFGHJKLMNOPQRST|" << std::endl | |
<< "--+-------------------+" << std::endl | |
<< " 1| |" << std::endl | |
<< " 2| |" << std::endl | |
<< " 3| |" << std::endl | |
<< " 4| |" << std::endl | |
<< " 5| |" << std::endl | |
<< " 6| |" << std::endl | |
<< " 7| |" << std::endl | |
<< " 8| |" << std::endl | |
<< " 9| |" << std::endl | |
<< "10| |" << std::endl | |
<< "11| |" << std::endl | |
<< "12| |" << std::endl | |
<< "13| |" << std::endl | |
<< "14| |" << std::endl | |
<< "15| |" << std::endl | |
<< "16| |" << std::endl | |
<< "17| |" << std::endl | |
<< "18| |" << std::endl | |
<< "19| |" << std::endl | |
<< "--+-------------------+" << std::endl | |
<< "何かボタンを押せだぜ☆(^▽^)" << std::endl; | |
getchar(); | |
//system("pause"); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <iostream> | |
#include <string> | |
#include <fstream> | |
/** | |
* テキストファイルの読書き | |
* 参照 : 「[C++] ファイル入出力の覚書」 (Qiita) http://qiita.com/lazynick/items/8aede589bb9ca0c8b64f | |
* 参照 : 「文字列の結合方法による速度差」 (Qiita) http://qiita.com/ampersand/items/0322e0820badf2c020be | |
* 参照 : 空文字列で初期化するには : 「文字列の初期化」 (手を動かしてさくさく理解する C++ 文字列クラス std::string 入門) http://vivi.dyndns.org/tech/cpp/string.html | |
*/ | |
class UtilFile | |
{ | |
public: | |
UtilFile(); | |
~UtilFile(); | |
static void Write(std::string filename, std::string contents); | |
static std::string Read(std::string filename); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdafx.h" | |
#include "UtilFile.h" | |
UtilFile::UtilFile() | |
{ | |
} | |
UtilFile::~UtilFile() | |
{ | |
} | |
void UtilFile::Write(std::string filename, std::string contents) | |
{ | |
std::ofstream writing_file; | |
writing_file.open(filename); | |
writing_file << contents; | |
} | |
std::string UtilFile::Read(std::string filename) | |
{ | |
// ファイルの内容全文 | |
std::string contents; | |
std::ifstream file; | |
file.open(filename); | |
std::string line; | |
while (!file.eof()) | |
{ | |
// 1行ずつ読み取ります | |
std::getline(file, line); | |
contents += line; | |
// 次行があるなら改行を追加 | |
if (file.eof()) { contents += "\n"; } | |
} | |
return contents; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <iostream> | |
#include <string> | |
#include <fstream> | |
/** | |
* テキストファイルの読書き | |
* | |
* 自著 : [C++] テキストファイル読書き (C#プログラマ向け) (Qiita) http://qiita.com/muzudho1/items/fa1446b85ae64d01a5ca | |
* 参照 : 「[C++] ファイル入出力の覚書」 (Qiita) http://qiita.com/lazynick/items/8aede589bb9ca0c8b64f | |
* 参照 : 「文字列の結合方法による速度差」 (Qiita) http://qiita.com/ampersand/items/0322e0820badf2c020be | |
* 参照 : 空文字列で初期化するには : 「文字列の初期化」 (手を動かしてさくさく理解する C++ 文字列クラス std::string 入門) http://vivi.dyndns.org/tech/cpp/string.html | |
* 参照 : 「lambda式を引数として受け取る関数の自作はできますか?」 (Teratail) https://teratail.com/questions/37610 | |
*/ | |
namespace UtilFile | |
{ | |
/** | |
* ファイルへ文字列を書き出します。 | |
*/ | |
void Write(std::string filename, std::string contents); | |
/** | |
* ファイルから文字列を読み取ります。 | |
*/ | |
std::string Read(std::string filename); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool UtilFile::Read(std::string filename, std::string& contents) | |
{ | |
std::ifstream file; | |
file.open(filename); | |
if (file.fail()) | |
{ | |
//std::cout << "ファイル[" << filename << "]が開けなかった☆(^~^)!オワタ~☆(>_<)" << std::endl; | |
return false; | |
} | |
std::string line; | |
while (!file.eof()) | |
{ | |
// 1行ずつ読み取ります | |
std::getline(file, line); | |
contents += line; | |
// 次行があるなら改行を追加 | |
if (file.eof()) { contents += "\n"; } | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment