パソコン活用研究シリコンバレー(C、C++、の活用研究)

テキストファイルを読みこんで表示する
(準備中)



DOSコマンドのTYPEコマンドの簡易版としてテキストファイルを10行だけ読み込んで表示する
プログラム。名付けてTYPE10とします。


/* TYPE10.C ver0.01
Read 10 lines of text file
Written by tsuyoshi Kasai
for LSI-C */

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define maxf 256

static char filename[50];
FILE *fp;

main()
{ char line[maxf];
int i;
printf("File Name? ");
gets(filename);
if (strlen(filename)<=0) exit(0);
fp=fopen(filename,"r");
if (fp == NULL){ printf("File cannot be opened");fclose(fp);exit(0);}
for (i=0 ; i<10; i++){
if (fgets(line,maxf,fp)==NULL){
if(feof(fp)){fclose(fp);exit(0);}
fclose(fp);exit(0);}
printf("%s",line);}
fclose(fp);
}


TYPEコマンドを拡張したもの、lessコマンドの簡易版のようなは、
ファイル入出力(ランダムアクセス関数)」に説明があります。



TopPage