C语言IO库函数

2024-06-15 23:32
文章标签 语言 io 库函数

本文主要是介绍C语言IO库函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

#include <stdio.h>
int fprintf( FILE *stream, const char *format, ... );
类似上两函数,只是该函数用于文件操作
#include <stdio.h>
int scanf( const char *format, ... );
函数以给定字符串的格式从标准输入流中读入数据(stdin)
将数据保存在给定参数中,它忽略空格(tab,spaces,etc)
跳过不符合的字符,返回被赋值的变量数,若出错返回EOF
控制符如下:
%c a single character
%d a decimal integer
%i an integer
%e, %f, %g a floating-point number
%o an octal number
%s a string
%x a hexadecimal number
%p a pointer
%n an integer equal to the number of characters read so far
%u an unsigned integer
%[] a set of characters
%% a percent sign
scanf()会将输入的数据根据参数format字符串来转换并格式化数据。Scanf()格式转换的一般形式如下
%[*][size][l][h]type
以中括号括起来的参数为选择性参数,而%与type则是必要的。
* 代表该对应的参数数据忽略不保存。
size 为允许参数输入的数据长度。 [Page]
l 输入的数据数值以long int 或double型保存。
h 输入的数据数值以short int 型保存。
[] 读取数据但只允许括号内的字符。出现其他字符则终止。如[a-z]。
[^] 读取数据但不允许中括号的^符号后的字符出现,如[^0-9].
返回值 成功则返回参数数目,失败则返回-1,错误原因存于errno中。
#include <stdio.h>
int sscanf( const char *buffer, const char *format, ... );
函数用法同scanf,只是该函数的数据是从buffer中读入的
#include <stdio.h>
int fscanf( FILE *stream, const char *format, ... );
函数类似上函数,只是该函数用于文件操作
#include <stdio.h>
char *gets( char *str );
从标准输入(stdin)中读入一行数据或是遇到错误,并且在最后加入’ ’值
#include <stdio.h>
char *fgets( char *str, int num, FILE *stream );
类似上函数,该函数用与文件操作,返回读到的字符串,如果有错返回EOF,参数num为最多能读的数据(num-1,最后一个为null值)
若连续用fgets函数读文件中的数据,则应用fseek函数移动文件指针到下一行初(fseek(file, 2, SEEK_CUR)
在windows中,换行为两个字符,即回车换行
#include <stdio.h>
int getchar( void );
从stdin中读入一个字符返回,注意返回为int型
#include <stdio.h>
int fgetc( FILE *stream );
返回文件流中的下一个字符,返回EOF如果读到文件末尾或发生错误
#include <stdio.h>
int getc( FILE *stream );
同上一个函数
#include <stdio.h>
int putchar( int ch );
在stdin中写入一个字符,返回EOF如果出错,否则返回写入的字符
#include <stdio.h>
int putc( int ch, FILE *stream );
在文件流中写入一个字符,返回EOF如果出错,否则返回写入的字符
#include <stdio.h>
int puts( char *str );
在文件中写入str字符,如果出错返回EOF值,否则返回非负数值 [Page]
#include <stdio.h>
int fread( void *buffer, size_t size, size_t num, FILE *stream );
读入文件中的数据到buffer,总共大小为num,size表明读入类型的字节大小,返回值为读入的字节数
#include <stdio.h>
int fwrite( const void *buffer, size_t size, size_t count, FILE *stream );
函数将buffer中的内容写入文件中,总共写入cout个size大小的数据,返回写入数据大小的字节数
#include <stdio.h>
int feof( FILE *stream );
如果文件没有读到末尾,返回0,否则返回非0
#include <stdio.h>
int ferror( FILE *stream );
若文件没有错误发生,返回0,否则返回非0
#include <stdio.h>
void perror( const char *str );
打印字符串str和errno相关的错误信息
#include <stdio.h>
void clearerr( FILE *stream );
重新设置stream的错误标识和EOF指示器(错误标识不会自动清除,除非调用clearerr, fseek, fsetpos, or rewind 等函数)
#include <stdio.h>
int fclose( FILE *stream );
函数关闭stream文件,释放所有和stream相关的内存资源
#include <stdio.h>
FILE *fopen( const char *fname, const char *mode );
函数fname指定的文件,若文件不存在,则新建该文件,mode表示打开文件的模式,若出错,返回NULL
/"r/" Open a text file for reading
/"w/" Create a text file for writing
/"a/" Append to a text file
/"rb/" Open a binary file for reading
/"wb/" Create a binary file for writing
/"ab/" Append to a binary file
/"r /" Open a text file for read/write
/"w /" Create a text file for read/write [Page]
/"a /" Open a text file for read/write
/"rb /" Open a binary file for read/write
/"wb /" Create a binary file for read/write
/"ab /" Open a binary file for read/write
#include <stdio.h>
int fgetpos( FILE *stream, fpos_t *position );
函数将给定文件的指针存入position变量中,函数成功返回0,否则返回非0
fpos_t类型:long integer, __int64, or structure, depending on the target platform
#include <stdio.h>
int fsetpos( FILE *stream, const fpos_t *position );
函数用于设定文件指针,其他规则同fgetpos函数
#include <stdio.h>
FILE *freopen( const char *fname, const char *mode, FILE *stream );
函数重新定向stream的文件流到指定文件的文件流,mode用于指定文件的访问方式
函数返回NULL值如果出错,否则返回新文件的文件指针
注:可用该函数打开一个文件,并一stdout,stdin做参数,此时可以用在控制台上的函数操作文件
但是有一个问题需要解决,怎样把stdout,stdin的指针重新弄回来,以使控制台的输入输出还可用
因为该函数执行后会将原来的文件流(stream)指针关闭。在VC中可以通过结合dup和fdopen函数来实现
但是在C语言函数库中还不知道用什么函数可以去实现
#include <stdio.h>
int fseek( FILE *stream, long offset, int origin );
函数设置文件流stream指针到给定的偏移量,偏移量与origin相对而言
origin可取值:
SEEK_SET Seek from the start of the file
SEEK_CUR Seek from the current location
SEEK_END Seek from the end of the file
函数返回0为成功,非0为失败,该函数可以清除EOF标记 [Page]
#include <stdio.h>
long ftell( FILE *stream );
函数返回指定文件的当前指针的位置,若出错返回-1
#include <stdio.h>
int remove( const char *fname );
函数关闭fname名字所指定文件流,返回0为成功执行函数,非0为失败
#include <stdio.h>
int rename( const char *oldfname, const char *newfname );
函数更改文件的名字,返回0为成功,非0为失败
#include <stdio.h>
void rewind( FILE *stream );
函数将指定文件的指针移动到文件的开始处,并清除文件的EOF标识
#include <stdio.h>
void setbuf( FILE *stream, char *buffer );
函数设置文件的缓存区buffer,若buffer为NULL值,则文件写入没有缓冲
#include <stdio.h>
int setvbuf( FILE *stream, char *buffer, int mode, size_t size );
函数用特定的模式设置文件的缓冲区及大小
mode可取值:
_IOFBF, which indicates full buffering
_IOLBF, which means line buffering
_IONBF, which means no buffering
#include <stdio.h>
FILE *tmpfile( void );
函数打开一个临时文件并返回这个临时文件的指针,失败则返回NULL
#include <stdio.h>
char *tmpnam( char *name );
函数创建一个临时文件的文件名,保存在name中
#include <stdio.h>
int ungetc( int ch, FILE *stream );
函数将ch字符放回stream文件中
#include <stdarg.h>
#include <stdio.h>
int vprintf( char *format, va_list arg_ptr );
int vfprintf( FILE *stream, const char *format, va_list arg_ptr );
int vsprintf( char *buffer, char *format, va_list arg_ptr ); [Page]
These functions are very much like printf(), fprintf(), and sprintf().
The difference is that the argument list is a pointer to a list of arguments.
va_list is defined in stdarg.h, and is also used by (Other Standard C Functions)
va_arg(). For example:

void error( char *fmt, ... ) {
va_list args;
va_start( args, fmt );
fprintf( stderr, /"Error: /" );
vfprintf( stderr, fmt, args );
fprintf( stderr, /"/n/" );
va_end( args );
exit( 1 );
}
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/**//*void main()
{
char setTest[10];
scanf(/"%[a-z]/", setTest);
printf(/"%s /", setTest);
}*/
/**//*void main( void )
{
int c;
//Create an error by writing to standard input.
putc( ’c’, stdin );
if( ferror( stdin ) )
{
perror( /"Write error/" );
clearerr( stdin );
}

// See if read causes an error.
printf( /"Will input cause an error? /" );
c = getc( stdin );
if( ferror( stdin ) )
{
perror( /"Read error/" );
clearerr( stdin );
}
}*/
/**//*void main()
{
char read[100];
FILE *orig_stdout = (FILE*)malloc(sizeof(FILE)); [Page]
memcpy(orig_stdout, stdout, sizeof(FILE));
FILE *filew = freopen(/"test.txt/", /"w/", stdout);
//freopen(/"test.txt/", /"w/", stdout);
if(filew == stdout)
printf(/"equal/");
printf(/"We can write datum in file test.txt use printf function with the use of freopen/");
fclose(filew);
/* memcpy(stdout, orig_stdout, sizeof(FILE));
FILE *filer = freopen(/"test.txt/", /"r/", stdin);
//freopen(/"test.txt/", /"r/", stdin);
gets(read);
fclose(filer);
printf(/"%s/", read);
printf(/"%c/", read[0]);
printf(/"jd;salkjf/");
*/
// rename(/"test.txt/", /"newname.txt/");
/

http://humanbeng.blog.163.com/blog/static/9593240120107241134477/

这篇关于C语言IO库函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1064894

相关文章

C语言中%zu的用法解读

《C语言中%zu的用法解读》size_t是无符号整数类型,用于表示对象大小或内存操作结果,%zu是C99标准中专为size_t设计的printf占位符,避免因类型不匹配导致错误,使用%u或%d可能引发... 目录size_t 类型与 %zu 占位符%zu 的用途替代占位符的风险兼容性说明其他相关占位符验证示

C语言进阶(预处理命令详解)

《C语言进阶(预处理命令详解)》文章讲解了宏定义规范、头文件包含方式及条件编译应用,强调带参宏需加括号避免计算错误,头文件应声明函数原型以便主函数调用,条件编译通过宏定义控制代码编译,适用于测试与模块... 目录1.宏定义1.1不带参宏1.2带参宏2.头文件的包含2.1头文件中的内容2.2工程结构3.条件编

Go语言并发之通知退出机制的实现

《Go语言并发之通知退出机制的实现》本文主要介绍了Go语言并发之通知退出机制的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1、通知退出机制1.1 进程/main函数退出1.2 通过channel退出1.3 通过cont

Go语言编译环境设置教程

《Go语言编译环境设置教程》Go语言支持高并发(goroutine)、自动垃圾回收,编译为跨平台二进制文件,云原生兼容且社区活跃,开发便捷,内置测试与vet工具辅助检测错误,依赖模块化管理,提升开发效... 目录Go语言优势下载 Go  配置编译环境配置 GOPROXYIDE 设置(VS Code)一些基本

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

Go语言中make和new的区别及说明

《Go语言中make和new的区别及说明》:本文主要介绍Go语言中make和new的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1 概述2 new 函数2.1 功能2.2 语法2.3 初始化案例3 make 函数3.1 功能3.2 语法3.3 初始化

Go语言中nil判断的注意事项(最新推荐)

《Go语言中nil判断的注意事项(最新推荐)》本文给大家介绍Go语言中nil判断的注意事项,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1.接口变量的特殊行为2.nil的合法类型3.nil值的实用行为4.自定义类型与nil5.反射判断nil6.函数返回的

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

Go语言代码格式化的技巧分享

《Go语言代码格式化的技巧分享》在Go语言的开发过程中,代码格式化是一个看似细微却至关重要的环节,良好的代码格式化不仅能提升代码的可读性,还能促进团队协作,减少因代码风格差异引发的问题,Go在代码格式... 目录一、Go 语言代码格式化的重要性二、Go 语言代码格式化工具:gofmt 与 go fmt(一)

Go语言中泄漏缓冲区的问题解决

《Go语言中泄漏缓冲区的问题解决》缓冲区是一种常见的数据结构,常被用于在不同的并发单元之间传递数据,然而,若缓冲区使用不当,就可能引发泄漏缓冲区问题,本文就来介绍一下问题的解决,感兴趣的可以了解一下... 目录引言泄漏缓冲区的基本概念代码示例:泄漏缓冲区的产生项目场景:Web 服务器中的请求缓冲场景描述代码