2014年3月30日日曜日

開発環境

C++実践プログラミング (スティーブ オウアルライン (著)、Steve Oualline (原著)、Steve Oualline(原著)、望月 康司(翻訳)、クイープ(翻訳) 、オライリー・ジャパン)のⅣ部(高度なプログラミング概念)の16章(ファイル入出力)、16.13(プログラミング実習)、実習 16-1.を解いてみる。

その他参考書籍

実習 16-1.

コード(BBEdit, Emacs)

sample306_1.cpp

#include <iostream>
#include <fstream>
#include <cstdlib>

int main(int argc, char *argv[])
{
  std::ifstream data_file;
  std::string line;
  int count = 0;

  if (argc <= 1) {
    std::cerr << "コマンドライン引数にファイル名を指定してください。\n";
    exit (8);
  }
  
  data_file.open(argv[1]);
  if (data_file.fail()) {
    std::cerr << "Unable to open " << argv[1] << '\n';
    exit (8);
  }

  while(std::getline(data_file, line))
    ++count;

  data_file.close();

  std::cout << count << '\n';
  return (0);
}

Makefile

#
# FSFのg++コンパイラ用のMakefile
#
CC=g++
CFLAGS=-g -Wall
all: sample306_1

sample306_1: sample306_1.cpp
 ${CC} ${CFLAGS} -o sample306_1 sample306_1.cpp

clean:
 rm sample306_1

入出力結果(Terminal)

$ make && ./sample306_1
g++ -g -Wall -o sample306_1 sample306_1.cpp
コマンドライン引数にファイル名を指定してください。
$ ./sample306_1 sample306_1.cpp
29
$ ./sample306_1 abcde
Unable to open abcde
$ ./sample306_1 sample306_1
69

0 コメント:

コメントを投稿