#include <stdio.h>
#include <wchar.h>
int fwide(FILE *fp, int mode);
返回值:若流是宽定向的,返回 正值;若流是字节定向的,返回负值;若流是未定向的,返回0;</pre></div><div style="margin:0px; font-family:Helvetica,'Hiragino Sans GB',微软雅黑,'Microsoft YaHei UI',SimSun,SimHei,arial,sans-serif; font-size:15px; line-height:24px; widows:1"><span style="font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:19.5px"><span style="font-size:0.933rem">根据mode参数的不同值,fwide函数执行不同的工作。</span></span></div><div style="margin:0px; font-family:Helvetica,'Hiragino Sans GB',微软雅黑,'Microsoft YaHei UI',SimSun,SimHei,arial,sans-serif; font-size:15px; line-height:24px; widows:1"><span style="font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:19.5px"><span style="font-size:0.933rem">* 若mode参数值为负,fwide将试图使指定的流是字节定向的。</span></span></div><div style="margin:0px; font-family:Helvetica,'Hiragino Sans GB',微软雅黑,'Microsoft YaHei UI',SimSun,SimHei,arial,sans-serif; font-size:15px; line-height:24px; widows:1"><span style="font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:19.5px"><span style="font-size:0.933rem">* 若mode参数值为正,</span></span><span style="font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13.995px; line-height:19.5px">fwide将试图使指定的流是宽定向的。</span></div><div style="margin:0px; font-family:Helvetica,'Hiragino Sans GB',微软雅黑,'Microsoft YaHei UI',SimSun,SimHei,arial,sans-serif; font-size:15px; line-height:24px; widows:1"><span style="font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13.995px; line-height:19.5px">* 若mode参数为0,fwide将不试图设置流的定向,但返回标识该流定向的值。</span></div><div style="margin:0px; font-family:Helvetica,'Hiragino Sans GB',微软雅黑,'Microsoft YaHei UI',SimSun,SimHei,arial,sans-serif; font-size:15px; line-height:24px; widows:1"><span style="font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13.995px; line-height:19.5px"> 当打开一个流时,标准I/O函数fopen返回一个指向FILE对象的指针,该对象通常是一个结构,它包含了标准I/O库为管理该流所需要的所有信息,包括用于实际I/O的文件描述符、指向于该流缓冲区的指针、缓冲区的长度,当前在缓冲区中的字节数以及出错标志等。</span></div><div style="margin:0px; font-family:Helvetica,'Hiragino Sans GB',微软雅黑,'Microsoft YaHei UI',SimSun,SimHei,arial,sans-serif; font-size:15px; line-height:24px; widows:1"><span style="font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13.995px; line-height:19.5px">应用程序没有必要检验FILE对象,为了引用一个流,需要FILE指针作为参数传递给每个标准I/O函数,FILE * 为文件指针。</span></div><div style="margin:0px; font-family:Helvetica,'Hiragino Sans GB',微软雅黑,'Microsoft YaHei UI',SimSun,SimHei,arial,sans-serif; font-size:15px; line-height:24px; widows:1"><span style="font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13.995px; line-height:19.5px"></span></div><div style="margin:0px; font-family:Helvetica,'Hiragino Sans GB',微软雅黑,'Microsoft YaHei UI',SimSun,SimHei,arial,sans-serif; font-size:15px; line-height:24px; widows:1"><span style="font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13.995px; line-height:19.5px">三、标准输入、标准输出和标准错误</span></div><div style="margin:0px; font-family:Helvetica,'Hiragino Sans GB',微软雅黑,'Microsoft YaHei UI',SimSun,SimHei,arial,sans-serif; font-size:15px; line-height:24px; widows:1"><span style="font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13.995px; line-height:19.5px"> 这三个流可以自动的被进程使用,他们是:标准输入、标准输出和标准错误。这些流引用的文件与文件描述符STDIO_FILENO、STDOUT_FILENO、STDERR_FILENO所引用的相同。</span></div><div style="margin:0px; font-family:Helvetica,'Hiragino Sans GB',微软雅黑,'Microsoft YaHei UI',SimSun,SimHei,arial,sans-serif; font-size:15px; line-height:24px; widows:1"><span style="font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13.995px; line-height:19.5px">这3个标准I/O流通过预定义文件指针stdio、stdout和stderr加以引用。</span></div><div style="margin:0px; font-family:Helvetica,'Hiragino Sans GB',微软雅黑,'Microsoft YaHei UI',SimSun,SimHei,arial,sans-serif; font-size:15px; line-height:24px; widows:1"><span style="font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13.995px; line-height:19.5px"></span></div><div style="margin:0px; widows:1"><div style="font-family:Helvetica,'Hiragino Sans GB',微软雅黑,'Microsoft YaHei UI',SimSun,SimHei,arial,sans-serif; font-size:15px; line-height:24px; margin:0px; widows:1"><span style="font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13.995px; line-height:19.5px">四、缓冲</span></div><div style="font-family:Helvetica,'Hiragino Sans GB',微软雅黑,'Microsoft YaHei UI',SimSun,SimHei,arial,sans-serif; font-size:15px; line-height:24px; margin:0px; widows:1"> 标准I/O库提供缓冲的目的是尽可能的减少使用read和write调用次数。他也对每个I/O流自动的进行缓冲管理,从而避免了应用程序需要考虑这一点所带来的麻烦。</div><div style="font-family:Helvetica,'Hiragino Sans GB',微软雅黑,'Microsoft YaHei UI',SimSun,SimHei,arial,sans-serif; font-size:15px; line-height:24px; margin:0px; widows:1">标准I/O提供了一下三种类型的缓冲。</div><div style="font-family:Helvetica,'Hiragino Sans GB',微软雅黑,'Microsoft YaHei UI',SimSun,SimHei,arial,sans-serif; font-size:15px; line-height:24px; margin:0px; widows:1">(1)全缓冲。这种情况是在填满标准I/O缓冲区后才进行实际的I/O操作。驻留在磁盘上的文件通常是由标准I/O库实施全缓冲的。在一个流上执行第一次I/O操作时,相关标准I/O函数通常调用malloc获得需要使用的缓冲区。</div><div style="font-family:Helvetica,'Hiragino Sans GB',微软雅黑,'Microsoft YaHei UI',SimSun,SimHei,arial,sans-serif; font-size:15px; line-height:24px; margin:0px; widows:1"> 术语冲洗(flush)说明标砖I/O缓冲区的写操作。缓冲区可由标准I/O例程自动的冲洗(例如,当填满一个缓冲区时,)或者可以调用函数fflush冲洗一个流。在标准I/O方面,flush(冲洗)意味着将缓冲区的内容写到磁盘上,在终端驱动上面,flush(刷清)表示丢弃已经存储在缓冲区的数据。</div><div style="font-family:Helvetica,'Hiragino Sans GB',微软雅黑,'Microsoft YaHei UI',SimSun,SimHei,arial,sans-serif; font-size:15px; line-height:24px; margin:0px; widows:1">(2)行缓冲。在输入和输出中遇到换行符时,标准I/O库执行I/O操作。这允许我们一次输出一个字符,但只有在写了一行之后才进行实际I/O操作、当流涉及一个终端时,通常使用行缓冲。</div><div style="font-family:Helvetica,'Hiragino Sans GB',微软雅黑,'Microsoft YaHei UI',SimSun,SimHei,arial,sans-serif; font-size:15px; line-height:24px; margin:0px; widows:1">行缓冲限制:第一、标准I/O库用来收集每一行的缓冲区的长度是固定的,所以,只要填满了缓冲区即使还没有一个换行符,也进行I/O操作。第二,任何时候只要通过标准I/O库要求从(a)一个不带缓冲的流,或者(b)一个行缓冲的流得到输入数据,那么就会冲洗所有行缓冲输出流。一个不带缓冲的流输入需要从内核获得数据。</div><div style="font-family:Helvetica,'Hiragino Sans GB',微软雅黑,'Microsoft YaHei UI',SimSun,SimHei,arial,sans-serif; font-size:15px; line-height:24px; margin:0px; widows:1">(3)不带缓冲。标准I/O库不对字符进行缓冲存储。</div><div style="font-family:Helvetica,'Hiragino Sans GB',微软雅黑,'Microsoft YaHei UI',SimSun,SimHei,arial,sans-serif; font-size:15px; line-height:24px; margin:0px; widows:1"> 标准错误流stderr通常是不带缓冲的,这样就可以让出错信息尽可能快的显示出来。</div><div style="font-family:Helvetica,'Hiragino Sans GB',微软雅黑,'Microsoft YaHei UI',SimSun,SimHei,arial,sans-serif; font-size:15px; line-height:24px; margin:0px; widows:1">*当且仅当标准输入和标准输出并不指定交互式设备时,他们才是全缓冲的。</div><div style="font-family:Helvetica,'Hiragino Sans GB',微软雅黑,'Microsoft YaHei UI',SimSun,SimHei,arial,sans-serif; font-size:15px; line-height:24px; margin:0px; widows:1">*标准错误绝不会是全缓冲的。</div><div style="font-family:Helvetica,'Hiragino Sans GB',微软雅黑,'Microsoft YaHei UI',SimSun,SimHei,arial,sans-serif; font-size:15px; line-height:24px; margin:0px; widows:1">很多系统默认使用下列类型的缓冲:</div><div style="font-family:Helvetica,'Hiragino Sans GB',微软雅黑,'Microsoft YaHei UI',SimSun,SimHei,arial,sans-serif; font-size:15px; line-height:24px; margin:0px; widows:1">* 标准错误是不带缓冲的。</div><div style="font-family:Helvetica,'Hiragino Sans GB',微软雅黑,'Microsoft YaHei UI',SimSun,SimHei,arial,sans-serif; font-size:15px; line-height:24px; margin:0px; widows:1">* 若是指向终端设备的流。则是行缓冲的;否则是全缓冲的。</div><div style="font-family:Helvetica,'Hiragino Sans GB',微软雅黑,'Microsoft YaHei UI',SimSun,SimHei,arial,sans-serif; font-size:15px; line-height:24px; margin:0px; widows:1">更改缓冲类型的函数</div><div style="font-family:Helvetica,'Hiragino Sans GB',微软雅黑,'Microsoft YaHei UI',SimSun,SimHei,arial,sans-serif; font-size:15px; line-height:24px; margin:0px; widows:1"><pre name="code" class="cpp">#include <stdio.h>
void setbuf(FILE *restrict fp, char *restrict buf);
int setvbuf(FILE *restrict fp, char *restrict buf, int mode, size_t size);
返回值:成功返回0,出错返回非0。#include <stdio.h>
int fflush(FILE *fp);
返回值:若成功,返回0,若出错,返回EOF此函数使该流所有未写的数据都被传送至内核。如若fp是NULL,则次函数将导致所有输出流被冲洗。#include <stdio.h>
FILE *fopen(const char *restrict pathname, const char *restrict type);
FILE *freopen(const char *restrict pathname, const char *restrict type, FILE *restrict fp);
FILE *fdopen(int fd, const char *type);
三个函数的返回值:若成功,返回文件指针,出错,返回NULL。#include <stdio.h>
int fclose(FILE *fp);
返回值:若成功,返回0;若出错,返回EOF
六、读和写流
#include <stdio.h>
int getc(FILE *fp);
int fgetc(FILE *fp);
int getchar(void);
返回值:若成功,返回下一个字符;若已经到达尾端或者出错,返回EOF#include <stdio.h>
int ferror(FILE *fp);
int feof(FILE *fp);
返回值:若条件为真,返回非0(真);否则,返回0(假)
void clearerr(FILE *fp);#include <stdio.h>
int ungetc(int c, FILE *fp);
返回值:若成功,返回c;若出错,返回EOF
#include <stdio.h>
int putc(int c, FILE *fp);
int fputc(int c, FILE *fp);
int putchar(int c);
返回值:成功,则返回c;出错返回EOFputchar(c)等同于putc(c,stdout),putc可被实现为宏,fputc不能实现为宏。#include <stdio.h>
char *fgets(char *restrict buf, int n, FILE *restrict fp);
char *gets(char *buf);
返回值:若成功,返回buf;若已经到达尾端或出错,返回NULL
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE * pFile;
char mystring [100];
pFile = fopen ("myfile.txt" , "r");
if (pFile == NULL)
perror ("Error opening file");
else {
if ( fgets (mystring , 100 , pFile) != NULL )
puts (mystring);
fclose (pFile);
}
system("pause");
return 0;
}
下面函数提供每次输出一行的功能。
#include <stdio.h>
int fputs(const char *restrict str, FILE *restrict fp);
int puts(const char *str);
参数:str,这是一个数组,包含null结尾的要写入的字符序列;fp,一个文件对象标识字符串被写入流的指针。
返回值:若成功,返回非负值,若出错,返回EOF
#include <stdio.h>
int main ()
{
FILE *fp;
fp = fopen("file.txt", "w+");
fputs("This is c programming.", fp);
fputs("This is a system programming language.", fp);
fclose(fp);
return(0);
}结果为:#include <stdio.h>
size_t fread(void *restrict ptr, size_t size, size_t nobj, FILE *restrict fp);
size _t fwrite(const void *restrict ptr, size_t size, size_t nobj, FILE *restrict fp)
两个函数返回值:读或写的对象数
float data[10];
if (fwrite(&data[2], sizeof(float), 4, fp) != 4)
printf("fwrite error!");struct {
short count;
long total;
char name[32];
}item;
if (fwrite(&item, sizeof(item), 1, fp) != 1)
printf(fwrite error!);#include <stdio.h>
long ftell(FILE *fp);
返回值:若成功,返回当前文件位置指示,若出错,返回-1L;
int fseek(FILE *fp, long offset, int whence);
返回值:若成功,返回0,出错,返回-1;
void rewind(FILE *fp);#include <stdio.h>
off_t ftello(FILE *fp);
返回值:若成功,返回当前文件位置,若出错,返回(off_t-1)
int fseeko(FILE *fp, off_t offset, int whence);
返回值:若成功,返回0,若出错,返回-1;
#include <stdio.h>
int fgetpos(FILE *restrict fp, fpos_t restrict *pos);
int fsetpos(FILE *fp, const fpos_t *pos);
返回值:若成功,返回0;若出错,返回非0fgetpos将文件位置指示器的当前值存入由pos指向的对象中。在以后调用fsetpos时,可以使用此值重新定位至该位置。#include <stdio.h>
int printf(const char *restrict format, ...);
int fprintf(FILE *restrict fp, const char *restrict format,...);
int dprintf(int fd, const char *restrict format,...);
3个函数返回值:若成功,返回输出的字节数,若输出出错,返回负值。
int sprintf(char *restrict buf, const char *restrict format,...);
返回值:若成功,返回存入数组的字节数,若编码出错,返回负值
int snprintf(char *restrict buf, size_t n, const char *restrict format, ...);
返回值:若缓冲区足够大,返回将要存入数组的字节数,若编码出错,返回负值
#include< stdarg.h>
#include <stdio.h>
int vprintf(const char *restrict format, va_list arg);
int vfprintf(FILE *restrict fp, const char *restrict format, va_list arg);
int vdprintf(int fd, const char *restrict format, va_list arg);
所有3个函数返回值:若成功,返回输出字节数;若输出出错,返回负值
int vsprintf(char *restrict buf, const char *restrict format, va_list arg);
函数返回值:若成功,返回存入数组的字节数;若编码出错,返回负值
int vsnprintf(char *restrict buf, size_t n, const char *restrict format, va_list arg);
返回值:若缓冲区足够大,返回存入数组的字节数,若编码出错,返回负值。
*格式化输入
#include <stdio.h>
int scanf(const char *restrict format,...);
int fscanf(FILE *restrict buf, const char *restrict format,...);
int sscanf(const char *restrict buf, const char *srestrict format,...);
返回值:赋值的输入项数,若输入出错或在任一转换已经到达文件尾端,返回EOFscanf族用于分析输入字符串,并将字符序列转换成指定类型的变量。在格式之后的各参数包含了变量的地址,用转换结果对这些变量赋值。#include <stdarg.h>
#include <stdio.h>
int vscanf(const char *restrict format, va_list arg);
int vfscanf(FILE *restrict fp, const char *restrict format, va_list arg);
int vsscanf(const char *restrict buf, const char *restrict format, va_list arg);
返回值:指定的输入项目数,若输入出错或在任一转换前文件结束,返回EOF十二、临时文件
#include <stdio.h>
char *tmpnam(char *ptr);
返回值:指向唯一路径名的指针
FILE *tmpfile(void);
返回值:若成功,返回文件指针,若出错,返回NULL
#incldue <stdio.h>
int main(void)
{
char name[512],line[1024];
FILE *fp;
printf("%s\n",tmpnam(NULL)); /*first temp name*/
tmpnam(name); /*second temp name*/
printf("%s\n",name);
if ((fp = tmpfile()) == NULL) /*create temp file*/
printf("tmpfile error");
fputs("one line of output\n",fp); /* write to temp file */
rewind(fp); /* then read it back */
if (fgets(line, sizeof(line),fp) == NULL)
printf("fgets error");
fputs(line,stdout); /* print the line we wrote */
exit(0);
}执行后的结果为:#include <stdlib.h>
char *mkdtemp(char *template);
返回值:若成功,返回指向路径名的指针,若出错,返回NULL
int mkstemp(char *template);
返回值:若成功,返回文件描述符;若出错,返回-1
#include <stdio.h>
#include <errno.h>
void make_temp(char *template)
{
int fd;
struct stat sbuf;
if ((fd = mkstemp(template)) < 0)
printf("can't create temp file");
printf("temp name = %s\n",template);
close(fd);
if (stat(template, &sbuf) < 0)
{
if (errno == ENOENT)
printf("file doesn't exist\n");
else
printf("stat failed!\n");
}
else
{
printf("file exist!\n");
unlink(template);
}
}
int main(void)
{
char good_template[] = "/tmp/dirXXXXXX";
char *bad_template = "/tmp/dirXXXXXX";
printf("try to create first tmp file...\n");
make_temp(good_template);
printf("try to create second tmp file...\n");
make_temp(bad_template);
exit(0);
}执行结果为:两个模板字符串的声明方式不同带来了不同的运算结果。对于第一个模板,因为使用了数组,名字是在栈上分配的。但是第二种情况使用了指针,此时,只有指针自身驻留在栈上。编译器把字符串存放在可执行文件的只读段,当mkstemp函数修改字符串时,出现了段错误。
#include <stdio.h>
FILE *fmemopen(void *restrict buf, size_t size, const char *restrict type);
返回值:成功,返回流指针,出错,返回NULL
#include <stdio.h>
FILE *open_memstream(char **bufp, size_t *sizep);
#include <wchar.h>
FILE *open_wmemstream(wchar_t **bufp, size_t *sizep);
返回值:若成功,返回流指针,若出错,返回NULL
-------------------------------------------------------------------------华丽的风格线---------------------------------------------------------------------------------
1685



被折叠的 条评论
为什么被折叠?



