`

C语言IO库函数

    博客分类:
  • C
CIO 
阅读更多

#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语言标准函数库源码,相当全的库函数源码!

    C语言标准库函数源码大全(24M涵盖所有库函数) ,相当全的库函数源码! 如标准 io ,字符类处理等,各种源码库函数,是学习C语言,精通C语言的必备良药!平时项目参考相当给力!

    c语言IO函数库 pdf

    我自己整理的c语言IO库函数,对于c语言IO的学习有一定的帮助。 在这里分享给大家。

    C语言标准库函数

    C语言标准库函数,包括了我们常用的数学计算函数还有文件IO,字符串的操作函数

    C语言库函数 函数 功能 用法 程序例

    C语言库函数(O类字母到W类字母)如:下例 函数名: open 功 能: 打开一个文件用于读或写 用 法: int open(char *pathname, int access[, int permiss]); 程序例: #include &lt;string.h&gt; #include &lt;stdio.h&gt; #...

    C语言库函数(供参考!)

    全面讲解C语言函数库中每一个函数的用法: c语言函数库-第一章(C标准库); c语言函数库-第二章(IO函数); c语言函数库-第三章(字符处理函数); c语言函数库-第四章(字符串函数供参考!

    《C语言库函数合集》 很全哦 相信大家会喜欢的

    #include &lt;io.h&gt; int main(void) { int handle; char msg[] = "Hello world"; if ((handle = open("TEST.$$$", O_CREAT | O_TEXT)) == -1) { perror("Error:"); return 1; } write(handle, msg,...

    C语言函数库(o版C语言实现函数调用)

    用C语言命令实现各种函数的调用 例:函数名: open 功 能: 打开一个文件用于读或写 用 法: int open(char *pathname, int access[, int permiss]); 程序例: #include &lt;string.h&gt; #include &lt;stdio.h&gt; #include ...

    STC15系列单片机库函数及使用说明文档+软件KEIL例程源码(40个).zip

    STC15系列单片机库函数及使用说明文档+软件KEIL例程源码(40个): 10-PCA-3路硬件PWM 11-PCA-PWM-软件定时-捕捉 12-读ID号-模拟串口发送 13-3个IO对等通讯 14-模拟串口 ...37-比较器做ADC-C语言-库函数版本

    C语言模拟实现Linux文件系统

    C语言模拟实现Linux文件系统 1、在内存中开辟一块空间来模拟文件系统的运行,不读写硬盘。 2、面向单用户、单任务,不考虑并发,不考虑文件属主、组等概念。 3、程序开始后,初始化并接收用户输入。若输入”enter”...

    stm32 流水灯

    基于stm32的goio操作,采用c语言库函数操作。

    C语言程序设计标准教程

     在C语言中,文件操作都是由库函数来完成的。 在本章内将介绍主要的文件操作函数。 文件打开函数fopen  fopen函数用来打开一个文件,其调用的一般形式为: 文件指针名=fopen(文件名,使用文件方式) 其中,...

    01.什么是Linux系统编程 02. Linux系统编程的基本框架

    linux系统编程 Linux系统编程也叫Linux下的高级编程; 学习Linux系统编程C语言是基础,能够在Linux系统下通过指令完成文件的创建、复制...所以在任何操作系统下,使用标准IO,也就是C库函数操作文件的方法都是相同的。

    77G 22套C语言 C++ 数据结构 程序设计视频课程合集 C丨C++相关学习视频全套视频教程

    dk2j_c_库函数.mp4 dk2j_c_数据输入.mp4 dk2j_c_数据输出.mp4 dk2j_c_整型数据.mp4 dk2j_c_标识符.mp4 dk2j_c_程序设计的基本概念.mp4 dk2j_c_算术运算符.mp4 dk2j_c_自加自减逗号运算符.mp4 dk2j_c_赋值...

    c语言编写单片机技巧

    C语言有功能丰富的库函数、运算速度快、编译效率高、有良好的可移植性,而且可以直接实现对系统硬件的控制。C语言是一种结构化程序设计语言,它支持当前程序设计中广泛采用的由顶向下结构化程序设计技术。此外,...

    xv6-improved:最低操作系统

    计划 先说说我的几个计划制定的Guideline吧。这样大家好理解我为什么那么决定。 首先,我个人认为我们...在系统调用完成的基础上,实现(或适应现有的开源实现)标准C语言库函数,细长讲现有的开源软件顺利的移植到xv6

    C_src:在C语言中,您如何实现memmove()? printf()? malloc()? Linux的list_for_each()? 这是来自知名来源的C代码的集合,当您想知道如何自己实现它时可以使用它们

    在C语言中,您如何实现memmove()? printf()? malloc()? Linux的list_for_each()? 这是来自知名来源的C代码的集合,当您想知道如何自己实现它时可以使用它们。 C源代码-信誉良好的参考 有时,我想知道我...

    《操作系统真象还原》高清完整版,baidu云

    0.16 为什么说汇编语言比C语言快 15 0.17 先有的语言,还是先有的编译器,第1个 编译器是怎么产生的 16 0.18 编译型程序与解释型程序的区别 19 0.19 什么是大端字节序、小端字节序 19 0.20 BIOS中断、DOS中断、Linux...

    13.第十三章 文件.txt

    C语言中没有输入输出语句,对文件的读写都是用库函数实现的。ANSI规定了标准输入输出函数,用它们对文件进行读写,这些函数的声明包含在头文件stdio.h中。 13.2 文件的打开与关闭 文件进行读写操作前先要打开,...

Global site tag (gtag.js) - Google Analytics