| report5-1.c |
|---|
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define BUF_SIZE 256
int main(void){
FILE *fp;
const char *filename1="TomSawyer1.txt";
const char *filename2="C.txt";
char buf[BUF_SIZE];
char *ch;
int cnt_and=0, cnt_not=0;
fp=fopen(filename1, "r");
if(fp==NULL){
printf("Can't Open File %s\n", filename1);
exit(1);
}
while(1){
ch=fgets(buf, BUF_SIZE, fp);
if(ch==NULL){
break;
}
if(strstr(buf, "and")!=NULL){
cnt_and++;
}
}
fclose(fp);
printf("Found %d \"and\"s\n", cnt_and);
fp=fopen(filename2, "r");
if(fp==NULL){
printf("Can't Open File %s\n", filename2);
exit(1);
}
while(1){
ch=fgets(buf, BUF_SIZE, fp);
if(ch==NULL){
break;
}
if(strstr(buf, "not")!=NULL){
cnt_not++;
}
}
fclose(fp);
printf("Found %d \"not\"s\n", cnt_not);
return 0;
}
|
report4-3.cもしくは自作の解答プログラムを改訂して、
BUF_SIZE文字以上ある行を読み込もうとした場合に、
BUF_SIZE文字目以上を表示せずに、
再び次の行頭から画面表示するようにして下さい。
できれば、次のファイルのように、短いテキストファイルでの実行結果を用いてください。
| for_report5-2.txt |
|---|
1 22 333 4444 55555 666666 7777777 88888888 999999999 |
1 22 333 4444 5555 6666 7777 8888 9999 |
| report5-2.c |
|---|
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define BUF_SIZE 5
int main(void){
FILE *fp;
const char *filename="for_report5-2.txt";
char buf[BUF_SIZE];
char *ch;
int line=1;
char *check_whole_line;
fp=fopen(filename, "r");
if(fp==NULL){
printf("Can't Open File %s\n", filename);
exit(1);
}
while(1){
ch=fgets(buf, BUF_SIZE, fp);
if(ch==NULL){
break;
}
check_whole_line=strchr(buf, '\n');
if(check_whole_line==NULL){
printf("%s\n", buf);
while(1){
ch=fgets(buf, BUF_SIZE, fp);
check_whole_line=strchr(buf, '\n');
if(check_whole_line!=NULL)break;
}
}
else{
printf("%s", buf);
}
}
fclose(fp);
return 0;
}
|