文件输入输出总结
2016-07-29 00:00
239 查看
1基于文件指针的数据读写
基于文件指针的数据读写,通常为标准函数,在Windows与Linux下,均可以使用。数据块读写
NAMEfread,fwrite-binarystreaminput/outputSYNOPSIS#include<stdio.h>size_tfread(void*ptr,size_tsize,size_tnmemb,FILE*stream);size_tfwrite(constvoid*ptr,size_tsize,size_tnmemb,FILE*stream);
DESCRIPTIONThefunctionfread()readsnmembelementsofdata,eachsizebyteslong,fromthestreampointedtobystream,storingthemattheloca-tiongivenbyptr.Thefunctionfwrite()writesnmembelementsofdata,eachsizebyteslong,tothestreampointedtobystream,obtainingthemfromtheloca-tiongivenbyptr.
RETURNVALUEfread()andfwrite()returnthenumberofitemssuccessfullyreadorwritten(i.e.,notthenumberofcharacters).Ifanerroroccurs,ortheend-of-fileisreached,thereturnvalueisashortitemcount(orzero).
fread从文件流stream中读取nmemb个元素,写到ptr指向的内存中,每个元素的大小为size个字节。
fwrite从ptr指向的内存中读取nmemb个元素,写到文件流stream中,每个元素size个字节。
所有的文件读写函数都从文件的当前读写点开始读写,读写完以后,当前读写点自动往后移动size*nmemb个字节。
字符串读写
SYNOPSIS
#include<stdio.h>
char*fgets(char*s,intsize,FILE*stream);intfputs(constchar*s,FILE*stream);char*gets(char*s);等同于fgets(constchar*s,intsize,stdin);intputs(constchar*s);等同于fputs(constchar*s,stdout);
DESCRIPTIONfgets()readsinatmostonelessthansizecharactersfromstreamandstoresthemintothebufferpointedtobys.ReadingstopsafteranEOForanewline.Ifanew-lineisread,itisstoredintothebuffer.A'\0'isstoredafterthelastcharac-terinthebuffer.gets()readsalinefromstdinintothebufferpointedtobysuntileitheratermi-natingnewlineorEOF,whichitreplaceswith'\0'.Nocheckforbufferoverrunisperformed(seeBUGSbelow).fputs()writesthestringstostream,withoutitstrailing'\0'.puts()writesthestringsandatrailingnewlinetostdout.
gets()会忽略'\n',如果程序一执行,就按enter的话,字符串中存的就是'\0'。遇到错误或到文件结尾,返回NULL。
puts()会把'\0'换成\n输出。遇到错误返回EOF。
fgets()返回数组首地址,'\n'也存上,再加个'\0',遇到文件结尾返回NULL。遇到错误或到文件结尾,返回NULL。
fputs不会在行尾自动添加换行符。遇到错误返回EOF。
注意:从文件中读字符串,末尾都是会自动添加’\0’d的。
RETURNVALUEputs()andfputs()returnanon-negativenumberonsuccess,orEOFonerror.gets()andfgets()returnsonsuccess,andNULLonerrororwhenendoffileoccurswhilenocharactershavebeenread.
格式化读写
SYNOPSIS
#include<stdio.h>
intprintf(constchar*format,...);//相当于fprintf(stdout,format,…);
intscanf(constchar*format,…);
intfprintf(FILE*stream,constchar*format,...);intfscanf(FILE*stream,constchar*format,…);
intsprintf(char*str,constchar*format,...);intsscanf(char*str,constchar*format,…);
以f开头的将格式化后的字符串写入到文件流stream中,或者从文件流stream中读取格式化后的字符串
以s开头的将格式化后的字符串写入到字符串str中,或者从字符串str中读取格式化后的字符串
对于写函数,返回写的字符个数(不包括‘\0’),遇到错误返回一个负数。
对于读函数,返回匹配的个数。遇到错误或者到达文件结尾,返回EOF。
注意:对于读函数,匹配字符串时会忽略空格,并且会在结尾加’\0’。
单个字符读写
RETURNVALUEfgetc(),getc()andgetchar()returnthecharacterreadasanunsignedcharcasttoanintorEOFonendoffileorerror.fputc(),putc()andputchar()returnthecharacterwrittenasanunsignedcharcasttoanintorEOFonerror.puts()andfputs()returnanon-negativenumberonsuccess,orEOFonerror.
2基于文件描述符的数据读写
read与write函数是Linux系统调用,仅仅用于Linux系统。非缓冲。注意
针对管道,read的返回值有如下3种情况:1.读取正常,返回读到的字符个数
2.对方写端关闭,read返回0
3.自己的读端关闭,read出错,返回-1。
NAMEread-readfromafiledescriptorSYNOPSIS#include<unistd.h>ssize_tread(intfd,void*buf,size_tcount);DESCRIPTIONread()attemptstoreaduptocountbytesfromfiledescriptorfdintothebufferstartingatbuf.Ifcountiszero,read()returnszeroandhasnootherresults.IfcountisgreaterthanSSIZE_MAX,theresultisunspecified.RETURNVALUEOnsuccess,thenumberofbytesreadisreturned(zeroindicatesendoffile),andthefilepositionisadvancedbythisnumber.Itisnotanerrorifthisnumberissmallerthanthenumberofbytesrequested;thismayhappenforexamplebecausefewerbytesareactuallyavailablerightnow(maybebecausewewereclosetoend-of-file,orbecausewearereadingfromapipe,orfromaterminal),orbecauseread()wasinterruptedbyasignal.Onerror,-1isreturned,anderrnoissetappropriately.Inthiscaseitisleftunspecifiedwhetherthefileposition(ifany)changes.
NAMEwrite-writetoafiledescriptorSYNOPSIS#include<unistd.h>ssize_twrite(intfd,constvoid*buf,size_tcount);DESCRIPTIONwrite()writesuptocountbytesfromthebufferpointedbuftothefilereferredtobythefiledescriptorfd.RETURNVALUEOnsuccess,thenumberofbyteswrittenisreturned(zeroindicatesnothingwaswrit-ten).Onerror,-1isreturned,anderrnoissetappropriately.
注意:用read读数据到数组中时,是不会自动在末尾加’\0’的!
相关文章推荐
- 变位词问题
- 面试题之二叉树系列-1
- [LeetCode 107] Binary Tree Level Order Traversal II
- 老码农:如何写出让自己满意的代码
- C++语言的黑客行为
- Linux客户/服务器程序设计范式1——并发服务器(多进程)
- 蛇形排列
- 我的C++实践(12):函数指针与仿函数
- [LeetCode 127] Word Ladder
- 协程
- 有名管道
- 我的面试宝典(4):面试问题早准备
- 开发者需要了解的WebKit
- 机器学习之神经网络模型-下(Neural Networks: Representation)
- 编程实现计算器
- 找出一个字符串中最长重复次数的子字符串,并计算其重复次数
- Linux内核体系结构
- 产品经理七个局——破局之术
- 我的C++实践(16):引用计数实现
- 机器学习之单变量线性回归(Linear Regression with One Variable)