C语言标准库(2)--#include<stdio.h>
2014-11-26 23:30
447 查看
C语言标准库(2)--#include<stdio.h>
2014/11/26 by jxlijunhao
在这个头文件中包含了,单字符处理函数和字符串处理函数,标准输入输入操作,流操作,从文件读取,写入函数,块I/O 等。下面记录的部分
标准输入输出:
流操作:
跟上面功能等价的是
下面是一个简单的例子,从一个标准输入中输入数据,并将其保存到文件中
文件定位
SEEK_SET: 从文件的开始
SEEK_CUR:从文件的当位置
SEEK_END: 从文件末尾 (注意是offset要设定为负数)
下面的例子:将"THIS IS A TSTE FILE"中第5个位置处改成小写
输出的结果是: This is a sample
块I/O操作:fread 块读取,fwrite块存储
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个位置处改成小写
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 //使用rewind rewind(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; }
相关文章推荐
- #include “stdio.h”与#include <stdio.h>有什么区别?
- 关于#include<stdio.h > 和 #include<iostream>
- c语言中头文件#include <stdio.h>和#include "stdio.h"有什么区别?
- #include<iostream>、#include<iostream.h>和#include<stdio.h>的根本区别
- //2.使用宏将一个数二进制中的奇数位和偶数为进行交换。 #include<stdio.h> #include<stdlib.h> #define EXH(x) \ ((x & 0x5555555
- 1#include<stdio.h> #include<string.h> int deng(char a[],ch、用函数实现登陆功能(三次机会),然后在主函数中根据调用后的结果判断 登陆成功与否。
- #include<stdio.h>在C程序中的作用
- #include "stdio.h";#include <stdio.h>#include <iostream>;#include <iostream.h>区别
- C语言标准库(3)--#include<stdlib.h>
- 为什么在vs 中用include<iostream.h>不行,而include<stdio.h>可用
- #include <stdio.h> //c库要.h。printf("Input n:\n");scanf("%d",&n);
- 浅谈MDK环境下使用#include <stdio.h>的问题
- 使用ungetc()【#include<stdio.h>】过滤输入
- Android UI 优化 使用<include/>和 <merge />标签
- #include<iostream>与#include<iostream.h>以及#inclue<string> 和 #include<string.h>的区别
- #include <stdlib.h> 这个头文件是什么作用?
- C++中#include包含头文件带 .h 和不带 .h 的区别,如<iostream>和<iostream.h>
- #include <iostream> 和#include <iostream.h> 以及include " "的区别
- <jsp:include>与include指令的区别
- JSP 之 静态包含 <%@ include %>