Unix设计哲学KISS:keep it simple stupid
调用系统函数要判断出错:0成功,-1不成功;指针不成功返回NULL;
stdin stdout stderr三个文件指针系统默认打开;
ch = getchar() 要遇到\n才被读取; ^D->EOF;
printf ("....\n"); 需要加回车才会被真正输出;
文本文件: ASCII码或Unicode字符, ASCII每个字符对应一个字节,范围0-127,最高位始终为0;
二进制文件: 按二进制编码来存放;
#include
FILE *fp;
FILE *fopen(const char *path, const char *mode) ; 如: fp = fopen( " file.a ", "r"); 如果出错,返回NULL.
mode 含义
"r" 只读,须存在
"w" 只写,重新创建
"a" 追加,不存在则创建
"r+" 读写,须存在
"w+" 读写,重新创建
"a+" 读追加,不存在则创建
标准输入,标准输出,标准错误文件的FILE*指针分别为stdin, stdout, stderr;
UNIX的一个重要特点: everything is a file;
文件关闭: int fclose (FILE *fp); 正常完成关闭,返回0, 非零为错误;
int fgetc(FILE *stream); 应用: int ch = fgetc(fp); while(ch != EOF);
int fputc(int c, FILE *stream); 正常返回写入字符,否则返回EOF; 应用: fputc(ch, fp);
getchar() 相当于 fgetc(stdin); putchar(ch) 相当于 fputc(ch, stdout);
exit (0); 包含在库
文件内部位置指针定位函数:
void rewind(FILE *fp); 把fp所指的文件的内部位置指针移到文件头
long ftell(FILE *fp); 返回位置指针的当前位置, 也就是文件开头起多少个字节;调用出错返回-1 L;
int fseek(FILE *fp, long offset, int whence); 其中offset为正表示向文件尾移动,为负表示向文件头移动;
whence 位置指针如何移动
SEEK_SET 从文件开头移动offset字节
SEEK_CUR 从当前位置移动offset字节
SEEK_END 从文件末尾移动offset字节
char *fgets(char *s, int size, FILE *stream); s是缓冲区首地址; 返回值是s;
若行太长,则读取size -1字符并加'\0'存入缓冲区, 剩下的部分下次继续读;
若在size-1内已经读取到'\n'或文件末尾,则结束,并在缓冲区末尾添加'\0';
int fputs(const char *s, FILE *stream); s是以'\0'结尾的字符串,不需以'\n'结尾, 写入文件的内容不含'\0';
fgetc, fputc, fgets, fputs几个例子:
/*filename: fputs.c*/
#include
#include
int main()
{
FILE *fp;
int ch;
if ((fp = fopen("file2", "wt+"))==NULL)
{
printf ("Cannot open file\n");
exit(1);
}
printf ("input a string:\n");
ch = getchar();
while (ch != '\n')
{
fputc(ch, fp);
ch = getchar();
}
rewind(fp);
ch = fgetc(fp);
while (ch != EOF)
{
putchar(ch);
ch = fgetc(fp);
}
printf( "\n");
fclose(fp);
return 0;
}
/*fcopys.c*/
#include
#include
int main(int argc, char *argv[])
{
FILE *fp1, *fp2;
char* ch;
char a[100];
if (argc != 3)
{
printf ("usage: command file1 file2\n");
exit(0);
}
if ((fp1 = fopen(argv[1], "rt")) == NULL)
{
printf ("Cannot open %s\n", argv[1]);
exit(1);
}
if ((fp2 = fopen(argv[2], "wt+")) == NULL)
{
printf ("Cannot open %s\n", argv[2]);
exit(1);
}
//ch = fgets(a, 100, fp1);
while ( fgets(a, 100, fp1) != NULL)
{
fputs(a, fp2);
}
fclose(fp1);
fclose(fp2);
return 0;
}
/*fcopy.c*/
#include
#include
int main(int argc, char *argv[])
{
FILE *fp1, *fp2;
int ch;
if (argc != 3)
{
printf ("usage: command file1 file2\n");
exit(0);
}
if ((fp1 = fopen(argv[1], "rt")) == NULL)
{
printf ("Cannot open %s\n", argv[1]);
exit(1);
}
if ((fp2 = fopen(argv[2], "wt+")) == NULL)
{
printf ("Cannot open %s\n", argv[2]);
exit(1);
}
while ((ch = fgetc(fp1)) != EOF)
fputc(ch, fp2);
fclose(fp1);
fclose(fp2);
return 0;
}
时间函数使用:
#include
#include
#include
int main()
{
FILE *fp;
int n = 0;
int tmpsec = 0;
struct tm *t;
time_t var;
char s[50];
time(&var);
t = localtime(&var);
if((fp = fopen("timerecord", "at+")) == NULL)
{
printf ("open file failed!\n");
exit(1);
}
if (!feof(fp))
{
while (fgets(s, 50, fp))
n++;
}
while (n<100)
{
if (tmpsec != t->tm_sec)
{
tmpsec = t->tm_sec;
n++;
sprintf(s, "%d, %d-%d-%d %d:%d:%d\n", n, \
1900+t->tm_year, 1+t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
puts(s);
fputs(s, fp);
fflush(fp);
}
time(&var);
t = localtime(&var);
}
fclose(fp);
}
结果如下:
1, 2007-11-9 14:41:38
2, 2007-11-9 14:41:39
3, 2007-11-9 14:41:40
4, 2007-11-9 14:41:41
5, 2007-11-9 14:41:42
6, 2007-11-9 14:41:43
点滴:
apue.2e的编译:
vi Make.defines.linux
更改:wkDIR = ~/work/posix/apue.2e
make
阅读(687) | 评论(0) | 转发(0) |