●FILE fopen(const char *path, const char *mode);
●int fclose(FILE *stream);
●int fgetc(FILE *stream);
●int fputc(int c, FILE *stream);
[root@rhel74 c]# cat wc-l.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
static void die(const char *s);
int
main(int argc, char *argv[])
{
int c;
FILE *f;
int cnt = 0;
f = fopen(argv[1], "r");
if (!f) die(argv[1]);
while ((c = fgetc(f)) != EOF){
if (c == '\n') cnt++;
}
fclose(f);
printf("%d\n",cnt);
exit(0);
}
static void die(const char *s)
{
perror(s);
exit(1);
}
[root@rhel74 c]# ./wc-l /etc/hosts
2
catを作る2
[root@rhel74 c]# cat mycat2.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
static void do_cat(FILE *f);
int
main(int argc, char *argv[]){
int i;
if (argc == 1){
do_cat(stdin);
} else {
for(i = 1; i < argc; i++){
FILE *f;
f = fopen(argv[i], "r");
if(!f){
perror(argv[i]);
exit(1);
}
do_cat(f);
fclose(f);
}
}
exit(0);
}
static void
do_cat(FILE *f){
int c;
while((c = fgetc(f)) != EOF){
if(putchar(c) < 0) exit(1);
}
}
●int getchar(void);
●int putchar(int c);
[root@rhel74 c]# cat getcharputchar.c
#include <stdio.h>
#include <stdlib.h>
int
main()
{
int c;
while((c = getchar()) != EOF) {
if (putchar(c) < 0) exit(1);
}
exit(0);
}
[root@rhel74 c]# cat /etc/hosts | ./getcharputchar
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
●int ungetc(int c, FILE *stream);
処理内容によっては便利らしいが、あまり使い道を感じられなかったので、省略
●gets 使わない方がよい
●scanf 使わない方がよい
●char *fgets(char *buf, int size, FILE *stream);
※bufのサイズを超える分は切り捨てられても良い場合に使用する。
※文字数制限を受けたくない場合はgetlineを使用する。
●int fputs(const char *buf, FILE *stream);
[root@rhel74 c]# cat fgetsfputs.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
static void die(const char *s);
int
main(int argc, char *argv[])
{
FILE *f;
char buf[2048];
f = fopen(argv[1], "r");
while(fgets(buf, sizeof buf, f) != NULL){
if(fputs(buf, stdout) < 0) die("fputs");
}
if (fclose(f) < 0) die(argv[1]);
exit(0);
}
static void die(const char *s)
{
perror(s);
exit(1);
}
[root@rhel74 c]# ./fgetsfputs /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
●int puts(const char *buf);
[root@rhel74 c]# cat puts.c
#include <stdio.h>
int main(int argc, char *argv[])
{
puts("puts + return");
return 0;
}
[root@rhel74 c]# ./puts
puts + return
●int printf(const char *fmt, ...);
●int fprintf(FILE *stream, const char *fmt, ...);
[root@rhel74 c]# cat fprintf.c
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("%s\n", "printf");
printf("%d\n", 11);
printf("%5d %5s\n", 11, "abc");
printf("%05d %-5s\n", 11, "abc");
fprintf(stdout, "%s\n", "fprintf");
return 0;
}
[root@rhel74 c]# ./fprintf
printf
11
11 abc
00011 abc
fprintf
●size_t fread(void *buf, size_t size, size_t nmumb, FILE *stream);
●size_t fwrite(void *buf, size_t size, size_t nmumb, FILE *stream);
[root@rhel74 c]# cat freadfwrite.c
#include <stdio.h>
int main(int argc, char *argv[])
{
char buf[100];
if(fread(buf ,10 ,2 ,stdin) < 0) return 1;
if(fwrite(buf ,10 ,1 ,stdout) < 0) return 1;
return 0;
}
[root@rhel74 c]# echo 123456789012345678901234567890 | ./freadfwrite
1234567890[root@rhel74 c]#
●int fseeko(FILE *stream, off_t offset, int whence);
lseekとほとんど同じ
fseekは32bit用なので使わない
●off_t ftello(FILE *stream);
ファイルオフセットの値を返す
●void rewind(FILE *stream);
ファイルオフセットをファイル先頭に戻す
[root@rhel74 c]# cat fseekofrellorewind.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
static void die(const char *s);
int
main(int argc, char *argv[])
{
FILE *f;
char buf[2048];
f = fopen(argv[1], "r");
printf("%u\n", ftello(f));
fseek(f, 5, SEEK_SET);
printf("%u\n", ftello(f));
rewind(f);
printf("%u\n", ftello(f));
if (fclose(f) < 0) die(argv[1]);
exit(0);
}
static void die(const char *s)
{
perror(s);
exit(1);
}
[root@rhel74 c]# ./fseekofrellorewind /etc/hosts
0
5
0
[root@rhel74 c]#
●int fflush(FILE *stream);
※出力に\nがあれば、自動でバッファはflushされるが、
\nがない場合、flushされない。これをflushしてくれる関数。
※通常、出力されるのは、プログラムが終わるため。
asdfasdfas[root@rhel74 ~]# cat fflush.c
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
char buf[100];
if(fread(buf ,10 ,2 ,stdin) < 0) return 1;
if(fwrite(buf ,10 ,1 ,stdout) < 0) return 1;
/* 5秒後に出力 */
sleep(5);
fflush(stdout);
/* 5秒後に終了 */
sleep(5);
return 0;
}
[root@rhel74 ~]# echo asdfasdfasdfasdfasdfasdfasd | ./fflush
asdfasdfas[root@rhel74 ~]#
●int feof(FILE *stream);
●int ferror(FILE *stream);
[root@rhel74 c]# cat ferrorfeof.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(int argc, char *argv[])
{
char buf[2048];
int size;
int nmemb = 10;
if((size = fread(buf, 1, nmemb, stdin)) < nmemb){
if(ferror(stdin)) puts("ferror");
if(feof(stdin)) puts("feof");
}
if(fwrite(buf, 1, size, stdout) < size) puts("fwrite error");
return 0;
}
[root@rhel74 c]# echo 12345678 | ./ferrorfeof
feof
12345678
●void clearerr(FILE *stream);
↓「tail -f」のようなもの
[root@rhel74 c]# cat clearerr.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
static void die(const char *s);
int
main(int argc, char *argv[])
{
FILE *f;
char buf[2048];
f = fopen(argv[1], "r");
while(1){
if(fgets(buf, sizeof buf, f) != NULL){
if(fputs(buf, stdout) < 0) die("fputs");
} else {
sleep(1);
clearerr(f);
}
}
if (fclose(f) < 0) die(argv[1]);
exit(0);
}
static void die(const char *s)
{
perror(s);
exit(1);
}
[root@rhel74 c]# ./clearerr ./date.out |
2019年 11月 20日 水曜日 23:07:38 JST | [root@rhel74 c]# date >>./date.out
2019年 11月 20日 水曜日 23:08:01 JST | [root@rhel74 c]# date >>./date.out
2019年 11月 20日 水曜日 23:08:03 JST | [root@rhel74 c]# date >>./date.out
2019年 11月 20日 水曜日 23:08:07 JST | [root@rhel74 c]# date >>./date.out
2019年 11月 20日 水曜日 23:08:19 JST | [root@rhel74 c]# date >>./date.out
2019年 11月 20日 水曜日 23:08:22 JST | [root@rhel74 c]# date >>./date.out
2019年 11月 20日 水曜日 23:08:23 JST | [root@rhel74 c]# date >>./date.out
^C