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

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

C++プログラミング入門その30 リストとファイル入出力2

>>この記事には書き直した新しいページがあります。<<


<<前  [TOP]  次>>


Visual Studioで新規プロジェクトを作り、以下の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;
}

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

		std::cout << (char *)(xp->data) << std::endl;
	}

}



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





Visual Studioで新規プロジェクトを作り、以下の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;
}

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

	while ( fin>>buf) {
		s = (char *)malloc( strlen(buf) + 1 );
		strcpy_s(s, strlen(buf) + 1, buf);
		xp=inList(&start, s);
	}
	for (xp=start; xp; xp=xp->next) {

		fout << (char *)(xp->body) << std::endl;
	}

}



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


Visual Studioで新規プロジェクトを作り、以下のcppファイルを作成して下さい。


【ForTest3.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;
}

void main() {

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

	char *s;
	char buf[512];

	int i=0;

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

	std::ifstream fin;
	fin.open("C:\\test\\Welcome.txt",std::ios::in);
	std::ofstream fout;
	fout.open("C:\\test\\test.txt", std::ios::app);
	while ( test>>buf) {
		s = (char *)malloc( strlen(buf) + 1 );
		strcpy_s(s, strlen(buf) + 1, 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(s, strlen(buf) + 1, 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の内容を確認して下さい。





<<前  [TOP]  次>>