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个位置处改成小写
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