|
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 |
//#include <condefs> #include <stdio.h> #include <stdlib.h> #include <iostream> #include <fstream> #include <conio.h> using namespace std; #pragma hdrstop int main(int argc, char** argv) { char buff[81]; //漢字大丈夫? ifstream infile; infile.open("readfile.cpp"); if (!infile) return 0; while (!infile.eof()) { infile.getline(buff, sizeof(buff)); cout << buff << endl; } infile.close(); cout << endl << "Press any key to continue..."; //getch(); return 0; } |
上はC++Builder入門のサンプルコード。自分自身を表示します。
|
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
/* Chotto.c multifile cat. procedures Designed & created by N.Mita 1986/05/28 Copyright Core Dump Co,.Ltd. */ #include <stdio.h> #include <ctype.h> #include <stdlib.h> #include <string.h> #define CLS cls() //画面を消去する関数を呼ぶマクロ #define MAXFILES 128 //このプログラムで扱えるファイル数の最大値256 #define MAXLINES 256 //このプログラムで扱える1行の文字数の最大値 static char file_name[MAXFILES][40]; //ファイル名を入れておくバッファ FILE* fp; //ファイル構造体へのポインタ void cls() { //・・画面を消去する関数(どの処理系でも対応できるように、 25 行空行を送るだけにしてある) int i; for (i = 0; i < 25; ++i) printf("\n"); } int open_file(fname) //ファイルをオープンする関数 char fname[]; { if (NULL == (fp = fopen(fname, "r"))) return(0); else return(1); } void close_file() //ファイルをクローズする関数 { fclose(fp); } void cat_file(line) // ......... ファイルの内容を指定行数だけ表示する関数 int line; /* number of display lines*/ { int i, j; char line_buff[MAXLINES]; /* display line-buffer */ for (i = 0; i < line; ++i) //....... 1行を処理するループ { if (NULL == fgets(line_buff, MAXLINES, fp)) // 1行を読み込み、エラーが起きた { //かどうか判断する if (feof(fp)) break; //ファイルの終わりならば for ループを抜ける if (ferror(fp)) { printf(">>>>>>> Read-ERROR on FILE <<<<<<<\n"); break; } } //エラーであれば 処理を中断して終了 for (j = 0; j < MAXLINES; ++j) //......... ファイルから読み込んだ行を表示する { if ((int)NULL == line_buff[j]) break; //読めない文字があった場合は、 .ピリオド if ((isspace(line_buff[j])) || (' ' <= line_buff[j])) putchar(line_buff[j]); else putchar('.'); } } } int disp_1file(fname, n) //指定されたファイルを表示する関数 char fname[]; /* file-names for display */ int n; /* number of display-lines */ { if (!open_file(fname)) { printf("******* Cannot open file: %20s ******\n", fname); return(0); } else { printf("-------- FILE : %20s --------\n", fname); cat_file(n); printf("\n\n"); } close_file(); return (1); } void disp_file(m, n) //・指定された複数ファイルを表示する関数 int m; /* Number of files */ int n; /* Number of lines */ { int i; char dummy[40]; for (i = 0; i < m; ++i) { disp_1file(&file_name[i][0], n); puts("Press return..........."); gets(dummy); } } int input_file() //ファイル名の入力関数 { int i; CLS; for (i = 0; i < MAXFILES; ++i) { printf("%5d: FILE-NAME=", i + 1); gets(&file_name[i][0]); if (0 >= strlen(&file_name[i][0])) break; } return(i); } int input_line() //表示行数の入力関数 { char i_str[40]; printf(" : LINES ? = "); gets(i_str); return(atoi(i_str)); } int main() //・・・・・・メインの関数. できるだけシンプルになっていることが望ましい { int i, j; if (0 >= (i = input_file())) //.....・ファイル名の入力 { printf("******* No files *******\n"); exit(0); } else { if (0 >= (j = input_line())) // 表示行数の入力 { printf("******* No lines *******¥n"); exit(0); } disp_file(i, j); //......... 実際の処理 } printf("------- Complete. -------\n"); //作業終了の表示 return 0; } |
上は三田典玄さんのサンプルコード。表示するファイル名をstaticで確保してます。
|
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
#include <stdio.h> #include <stdlib.h> #include <iostream> #include <fstream> #include <conio.h> #include <string.h> using namespace std; #pragma hdrstop #define MAXFILES 5 #define MAXLINES 10 class FileNameArray { public: FileNameArray(); ~FileNameArray(); char* getFname() { return fname; } void setFname(char* fn) { strcpy(fname, fn); } private: char fname[40]; }; FileNameArray::FileNameArray() { strcpy(fname, "end"); } FileNameArray::~FileNameArray() { } int InputFileName(FileNameArray* fnm[]) { for (int i = 0; i < (MAXFILES - 1); i++) { char w[40]; cout << i + 1 << " FileName: "; cin >> w; fnm[i]->setFname(w); if (strcmp(w, "end") == 0) { return -1; } } return 0; } void PrintFileName(FileNameArray* fnm[]) { for (int i = 0; i < MAXFILES - 1; i++) { cout << i + 1 << " :FileName: "; cout << fnm[i]->getFname() << endl; if (strcmp(fnm[i]->getFname(), "end") == 0) break; } } int PrintFileStop(FileNameArray* ptr[]) { char buff[81]; for (int i = 0; i < MAXFILES - 1; i++) { ifstream infile; char w[40]; int lcnt = 0; strcpy(w, ptr[i]->getFname()); if (strcmp(w, "end") != 0) { infile.open(w); if (!infile) return -1; while (!infile.eof()) { infile.getline(buff, sizeof(buff)); cout << buff << endl; lcnt++; if (lcnt > MAXLINES) break; } } else { infile.close(); break; } infile.close(); } return 0; } int main(int argc, char** argv) { FileNameArray* fnab[MAXFILES]; for (int i = 0; i < (MAXFILES - 1); i++) fnab[i] = new FileNameArray(); InputFileName(fnab); char msg[40]; (PrintFileStop(fnab) != 0) ? (strcpy(msg, "異常")) : (strcpy(msg, "正常")); cout << msg << "終了しました。"; cout << endl << "Press any key to continue..."; return 0; } |
上は見様見真似で弄り回した自分で書いたもの。ファイル名を入れておくところをclassで作って、mainに置いてます。それらはポインタの配列で、必要なルーティンへ渡します。PrintFileStopはほぼ、C++Builder入門のコードと同じです。