わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。

ファイルに書き込む

ifstreamのwriteメソッドファイルを読み込みます。

ファイルに書き込む
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <fstream>
 
 
 
/*
    ファイルの書き込み
*/
int _tmain
(
      int argc
    , _TCHAR* argv[]
)
{
    // 標準出力にユニコード出力する
    setlocale( LC_ALL, "Japanese" );
 
    /*
        ファイルの書き込み
    */
    {
        std::string strFile = "ファイルに書き込みます。";
 
        // ファイルのオープン
        std::ofstream ofstr( L"file.txt", std::ios::out | std::ios::binary | std::ios_base::trunc );
        if ( !ofstr ) {
 
            // ファイルがオープンできなかった
            std::wcout << L"ファイルオープン失敗" << std::endl;
            return( - 1);
        }
 
        // ファイルに書き込む
        ofstr.write( strFile.c_str(), strFile.size() );
    }
 
    // 正常終了
    return( 0 );
}

実行結果







わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。