学生向けプログラミング入門 | 無料

学生向けにプログラミングを無料で解説。Java、C++、Ruby、PHP、データベース、Ruby on Rails, Python, Django

C++ | 27 | ファイルへの書き込み

↓↓クリックして頂けると励みになります。



26 | キューとスタック】 << 【ホーム】 >> 【28 | バイナツリー

ファイルへの書き込み
ファイルへの書き込み




C++でファイルへの書き込みを行うには、標準ライブラリで提供されている ヘッダーを使用します。
ヘッダーには、ファイルの入出力をサポートするためのクラスが含まれています。
代表的なクラスには、std::ofstream(ファイルへの出力ストリーム)があります。


Visual Studio Codeで以下のcppファイルを作成して下さい。


新規作成 【FileTest1.cpp】

#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<fstream>

struct eLIST *newObj(void * data);
struct eLIST *inList( struct eLIST **pstart, void *data );

/* eLIST*/
struct eLIST {
    struct eLIST *next;
    struct eLIST *prev;
    void *data;
};

/* eLIST のオブジェクトを作り、それはdataを保持 */
struct eLIST *newObj(void *data){
    struct eLIST *p;
    p = (struct eLIST *)malloc(sizeof(struct eLIST));
    p->next=NULL;
    p->prev=NULL;
    p->data=data;
    return p;
}
/* 一般的な要素の追加、但し先頭への追加のみできず */
struct eLIST *inList( struct eLIST **pstart, void *data){
    struct eLIST *newl=NULL, *p = NULL;
    if(*pstart==NULL){       	/* リストが空なら */
        *pstart=newObj(data);
    }
	else{
        p=*pstart;
        while(p->next != NULL){
            p=p->next;
		}
		newl = newObj(data);
        newl->next = p->next;
        p->next = newl;
    }
    return newl;
}

int main() {
	struct eLIST *start=NULL;
	struct eLIST *xp;
	char *s;
	char buf[512];
	int i=0;
	std::ifstream fin;
	fin.open("Welcome.txt",std::ios::in);
	while ( fin>>buf) {
		s = (char *)malloc( strlen(buf) + 1 );
		strcpy(s, buf);
		xp=inList(&start, s);
	}
	for (xp=start; xp; xp=xp->next) {
		std::cout << (char *)(xp->data) << std::endl;
	}

}



「Welcome.txt」を読み込み専用にオープンし、すべてのテキストを読み込んだ後に標準出力に再びそれらを出力します。


このサンプルプログラムは以下の「Welcome.txt」ファイルを読み込みます。
【Welcome.txt】

#include <iostream>

int main() {

	std::cout << "ようこそ!C++の世界へ!";
	std::cout << std::endl;

}



プログラムを実行して動作を確認します。

~/Desktop/Programming/CPP $ cd "/Users/**/Desktop/Programming/CPP/" && g++ FileTest1.cpp -o FileTest1 && "/Users/**
/Desktop/Programming/CPP/"FileTest1

#include
<iostream>
int
main()
{
std::cout
<<
"ようこそ!C++の世界へ!";
std::cout
<<
std::endl;
}



Visual Studio Codeで以下のcppファイルを作成して下さい。


新規作成 【FileTest2.cpp】

#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<fstream>

struct eLIST *newObj(void * data);
struct eLIST *inList( struct eLIST **pstart,void *data );

/* eLIST*/
struct eLIST {
    struct eLIST *next;
	struct eLIST *prev;
    void *body;
};

struct eLIST *newObj(void *data){
    struct eLIST *p;
    p = (struct eLIST *)malloc(sizeof(struct eLIST));
    p->next=NULL;
    p->prev=NULL;
    p->body=data;
    return p;
}
/* 一般的な要素の追加、但し先頭への追加のみできず */
struct eLIST *inList( struct eLIST **pstart, void *data){
    struct eLIST *newl=NULL, *p = NULL;
    if(*pstart==NULL){       /* リストが空なら */
        *pstart=newObj(data);
    }
	else{
        p=*pstart;
        while(p->next != NULL){
            p=p->next;
        }
        newl = newObj(data);
        newl->next = p->next;
        p->next = newl;
    }
    return newl;
}

int main() {
	struct eLIST *start=NULL;
	struct eLIST *xp;
	char *s;
	char buf[512];
	int i=0;
	std::ifstream fin;
	fin.open("Welcome.txt",std::ios::in);
	std::ofstream fout;
	fout.open("test.txt", std::ios::out);

	while ( fin>>buf) {
		s = (char *)malloc( strlen(buf) + 1 );
		strcpy(s, buf);
		xp=inList(&start, s);
	}
	for (xp=start; xp; xp=xp->next) {
		fout << (char *)(xp->body) << std::endl;
	}

}



読み込んだテキスト「Welcome.txt」を別のファイル「test.txt」に書き出すようにしています。
実行した結果新しいファイル「test.txt」が作成されることを確認してください。
実行しても何も表示されませんが、新たに「test.txt」ファイルが生成されます。
test.txtの内容は、「Welcome.txt」と同じになっています。

test.txtの内容は、「Welcome.txt」と同じ
test.txtの内容は、「Welcome.txt」と同じ



Visual Studio Codeで以下のcppファイルを作成して下さい。


新規作成 【FileTest3.cpp】

#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<fstream>

struct eLIST *newObj(void * data);
struct eLIST *inList( struct eLIST **pstart,void *data );

/* eLIST*/
struct eLIST {
    struct eLIST *next;
    struct eLIST *prev;
    void *body;
};

struct eLIST *newObj(void *data){

    struct eLIST *p;
    p = (struct eLIST *)malloc(sizeof(struct eLIST));
    p->next=NULL;
    p->prev=NULL;
    p->body=data;
    return p;
}

/* 一般的な要素の追加、但し先頭への追加のみできず */
struct eLIST *inList( struct eLIST **pstart, void *data){

    struct eLIST *newl=NULL, *p = NULL;

    if(*pstart==NULL){       /* リストが空なら */
        *pstart=newObj(data);
    }
	else{
        p=*pstart;

        while(p->next != NULL){
            p=p->next;
        }
        newl = newObj(data);
        newl->next = p->next;
        p->next = newl;
    }
    return newl;
}

int main() {

	struct eLIST *start=NULL;
	struct eLIST *xp1;
	struct eLIST *xp2;

	char *s;
	char buf[512];

	int i=0;

	std::ifstream test;
	test.open("test.txt",std::ios::in);

	std::ifstream fin;
	fin.open("Welcome.txt",std::ios::in);
	std::ofstream fout;
	fout.open("test.txt", std::ios::app);
	while ( test>>buf) {
		s = (char *)malloc( strlen(buf) + 1 );
		strcpy(s, buf);
		xp1=inList(&start, s);
	}
	for (xp1=start; xp1; xp1=xp1->next) {
		std::cout << (char *)(xp1->body) << std::endl;
	}
	while ( fin>>buf) {
		s = (char *)malloc( strlen(buf) + 1 );
		strcpy(s, buf);
		xp2=inList(&start, s);
	}
	for (xp2=start; xp2; xp2=xp2->next) {
		fout << (char *)(xp2->body) << std::endl;
	}
}



「FileTest2.cpp」で作成されたtest.txtの中身を画面に出力したあとに、「test.txt」のファイルの末尾にさらに「Welcome.txt」の中身を追加するプログラムです。


実行してtest.txtの内容を確認して下さい。

~/Desktop/Programming/CPP $ cd "/Users/**/Desktop/Programming/CPP/" && g++ tempCodeRunnerFile.cpp -o tempCodeRunnerFile 
&& "/Users/**/Desktop/Programming/CPP/"tempCodeRunnerFile

#include
<iostream>
int
main()
{
std::cout
<<
"ようこそ!C++の世界へ!";
std::cout
<<
std::endl;
}



【test.txt】

#include
<iostream>
int
main()
{
std::cout
<<
"ようこそ!C++の世界へ!";
std::cout
<<
std::endl;
}
#include
<iostream>
int
main()
{
std::cout
<<
"ようこそ!C++の世界へ!";
std::cout
<<
std::endl;
}
#include
<iostream>
int
main()
{
std::cout
<<
"ようこそ!C++の世界へ!";
std::cout
<<
std::endl;
}




26 | キューとスタック】 << 【ホーム】 >> 【28 | バイナツリー





↓↓クリックして頂けると励みになります。