C语言标准库(2)--#include<stdio.h>
来源:互联网 发布:三国武将排名知乎 编辑:程序博客网 时间:2024/06/09 17:35
C语言标准库(2)--#include<stdio.h>
2014/11/26 by jxlijunhao
在这个头文件中包含了,单字符处理函数和字符串处理函数,标准输入输入操作,流操作,从文件读取,写入函数,块I/O 等。下面记录的部分
标准输入输出:
int getchar ( void ); //从标准输入中读取一个字符
int putchar ( int character );//向标准输出中输出
流操作:
int putc ( int character, FILE * stream );//向一个输入流中写入字符
int getc ( FILE * stream ); //从输出流中读一个字符
跟上面功能等价的是
int fgetc ( FILE * stream );
int fputc ( int character, FILE * stream );
FILE * fopen ( const char * filename, const char * mode );//打开文件
"r" : 读取数据"w" : 假设打开的文件名本来就存在,那么原来的内容会被清空,然后重新写入新的内容"r+": 读取/更新"w+": 写入/更新,若文件存在,那么旧的内容会被清空"a+": 向文件中加入新的内容
int fclose ( FILE * stream );
下面是一个简单的例子,从一个标准输入中输入数据,并将其保存到文件中
#include<stdio.h>int main(){//从键盘中读入一组数据,若输入中有‘#’,则结束,并将其保存到文本文件中char c;FILE *pFile;pFile=fopen("myFile.txt","w+");while (1){c=getchar();if (c=='#')break;fputc(c,pFile);}fclose(pFile);//读取文件中的数据,并显示出来pFile=fopen("myFile.txt","r");if (pFile!=NULL){while (c!=EOF){c=fgetc(pFile);putchar(c);}}fclose(pFile);}
文件定位
int fseek ( FILE * stream, long int offset, int origin ); //文件定位第三个参数开始,从哪个位置开始计算 offset,有三个可选的值:
SEEK_SET: 从文件的开始
SEEK_CUR:从文件的当位置
SEEK_END: 从文件末尾 (注意是offset要设定为负数)
下面的例子:将"THIS IS A TSTE FILE"中第5个位置处改成小写
下面的例子:将"THIS IS A TSTE FILE"中第5个位置处改成小写
int main(){FILE *pFile;pFile=fopen("test.txt","wb");fputs("THIS IS A TEST FILE.",pFile);fseek(pFile,5,SEEK_SET); //从文件起始fputs("i",pFile);fclose(pFile);}
long int ftell ( FILE * stream ) //返回指针当前位置
/* ftell example : getting size of a file */#include <stdio.h>int main (){ FILE * pFile; long size; pFile = fopen ("myfile.txt","rb"); if (pFile==NULL) perror ("Error opening file"); else { fseek (pFile, 0, SEEK_END); // non-portable size=ftell (pFile); //返回文件的长度 fclose (pFile); printf ("Size of myfile.txt: %ld bytes.\n",size); } return 0;}
void rewind ( FILE * stream );//将文件指针重新指到文件的起始位置
#include <stdio.h>int main(){int n;FILE *pFile;pFile=fopen("myfile.txt","w+");for (n='A';n<='Z';n++)fputc(n,pFile);//此时,pFile已经指向了文件的最末尾,使用printf("the size of the file is %d\n",ftell(pFile));//the answer is: the size of the file is 26//使用rewindrewind(pFile);printf("the size of the file is %d\n",ftell(pFile));}
int fgetpos ( FILE * stream, const fpos_t * pos ); //记录文件操作之前的位置;int fsetpos ( FILE * stream, const fpos_t * pos );//将fgetspos得到的pos,通过其进行设定
#include <stdio.h>int main (){ FILE * pFile; fpos_t position; pFile = fopen ("myfile.txt","w"); fgetpos (pFile, &position); fputs ("That is a sample",pFile); fsetpos (pFile, &position); fputs ("This",pFile); fclose (pFile); return 0;}
输出的结果是: This is a sample
块I/O操作:fread 块读取,fwrite块存储
size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );其中第一个参数: ptr 是指向一个 大小为 size*count的内存区域,用来存储从 stream中读取出来的数据;
size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );正好跟上面的相反
#include <stdio.h>int main(){FILE *pFile;char buffer[]={'x','y','z'}; //将这个字符数组保存到文件中pFile=fopen("myfile.txt","wb");fwrite(buffer,sizeof(char),sizeof(buffer),pFile);fclose(pFile);return 0;}
/* fread example: read an entire file */#include <stdio.h>#include <stdlib.h>int main () { FILE * pFile; long lSize; char * buffer; size_t result; pFile = fopen ( "myfile.bin" , "rb" ); if (pFile==NULL) {fputs ("File error",stderr); exit (1);} // obtain file size: fseek (pFile , 0 , SEEK_END); lSize = ftell (pFile); rewind (pFile); // allocate memory to contain the whole file: buffer = (char*) malloc (sizeof(char)*lSize); if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);} // copy the file into the buffer: result = fread (buffer,1,lSize,pFile); if (result != lSize) {fputs ("Reading error",stderr); exit (3);} /* the whole file is now loaded in the memory buffer. */ // terminate fclose (pFile); free (buffer); return 0;}
0 0
- C语言标准库(2)--#include<stdio.h>
- C语言stdio库(#include<stdio.h>)
- C语言标准库 - stdio.h
- [C标准库] stdio.h
- c语言中头文件#include <stdio.h>和#include "stdio.h"有什么区别?
- C标准库学习stdio.h
- C /C++标准库 - <cstdio>(stdio.h)
- C 标准库 —— stdio.h
- #include<stdio.h>怎么理解,stdio.h是什么头文件,是不是每个C语言程序都要加?
- C语言标准库概览详述[8]-标准IO<stdio.h>
- 标准函数库(#include<stdio.h>)
- C语言之常用标准输入输出函数库<stdio.h>
- C语言标准库(1)—#include<ctype.h>
- C语言标准库(3)--#include<stdlib.h>
- linux标准库#include <unistd.h>与windows的#include <windows.h>(C语言开发)
- linux标准库#include <unistd.h>与windows的#include <windows.h>(C语言开发)
- C 语言 stdio.h 库 笔记
- C语言库stdio.h操作
- 【软件使用】解决Firefox在ubuntu和Windows之间同步问题
- Linux内核学习的第一步之README文档解读
- C#编写Windows服务程序图文教程
- c语言题:某电视台举办了低碳生活大奖赛。
- DTB设备树文件结构解析
- C语言标准库(2)--#include<stdio.h>
- 统一存储入门
- java 十字链表
- 减少oracle sql回表次数 提高SQL查询性能
- apache服务器多站点配置
- 内核编译选项详解
- smarty小案例
- [leetcode]Tree inorder traversal
- spring-framework-reference 3.1.4(十七章)