2014年5月8日木曜日

開発環境

C++実践プログラミング (スティーブ オウアルライン (著)、Steve Oualline (原著)、Steve Oualline(原著)、望月 康司(翻訳)、クイープ(翻訳) 、オライリー・ジャパン)のⅤ部(言語のその他の機能)の23章(モジュールプログラミング)、23.11(プログラミング実習)、実習 23-2.を解いてみる。

その他参考書籍

実習 23-2.

コード(BBEdit, Emacs)

search_open.h

#include <iostream>
#include <string>
#include <fstream>

const int SIZE = 100;
class search_open {
 private:
  std::ifstream in_file;
  bool find;
 public:
  search_open(std::string fn[], int size) {
    int i;

    for (i = 0; i < size; ++i) {
      in_file.open(fn[i]);
      if (!in_file.fail()) break;
    }
    find = i == size ? false : true;
  }
  void read_file(int n);
  void close_file();
};

search_open.cpp

#include <iostream>
#include <string>

#include "search_open.h"

void search_open::read_file(int n)
{
  std::string s;
  int i;

  if (find)
    for (i = 0; i < n; ++i) {
      getline(in_file, s);
      std::cout << s << std::endl;
    }
}

void search_open::close_file()
{
  if (find) in_file.close();
}

test_print_pages.cpp

#include "search_open.h"

int main(int argc, char *argv[])
{
  std::string file_names[] = {"abcde", "test_search_open.cpp"};
  search_open so(file_names, 2);

  so.read_file(5);
  so.close_file();
  
  return (0);
}

Makefile

CC=g++
CFLAGS=-g -Wall
SRC=search_open.cpp test_search_open.cpp
OBJ=search_open.o test_search_open.o

all: test_search_open

test_search_open: $(OBJ)
 ${CC} ${CFLAGS} -o test_search_open $(OBJ)

test_search_open.o: search_open.h test_search_open.cpp
 ${CC} $(CFLAGS) -c test_search_open.cpp

search_open.o: search_open.h search_open.cpp
 ${CC} $(CFLAGS) -c search_open.cpp

clean:
 rm test_search_open test_search_open.o search_open.o

入出力結果(Terminal)

$ make && ./test_search_open 
g++ -g -Wall -c search_open.cpp
g++ -g -Wall -c test_search_open.cpp
g++ -g -Wall -o test_search_open search_open.o test_search_open.o
#include "search_open.h"

int main(int argc, char *argv[])
{
  std::string file_names[] = {"abcde", "test_search_open.cpp"};
$

0 コメント:

コメントを投稿