文件输入输出函数详细说明

 

fgets函数

从流中读一行或指定个字符,  原型是char *fgets(char *s, int n, FILE *stream);  从流中读取n-1个字符,除非读完一行,参数s是来接收字符串,如果成功则返回s的指针,否则返回NULL。  形参注释:*s结果数据的首地址;n-1:一次读入数据块的长度,其默认值为1k,即1024;stream文件指针  例:如果一个文件的当前位置的文本如下  Love ,I Have  But ........  如果用   fgets(str1,4,file1);  则执行后str1="Lov",读取了4-1=3个字符,  而如果用   fgets(str1,23,file1);

 

  则执行str="Love ,I Have",读取了一行(包括行尾的'/n',并自动加上字符串结束符'/0')。

 

gets

  【1】函数:gets(字符指针)  【2】头文件: string.h(c中),c++不需包含此头文件  【3】原型: char  *gets(  char  *buffer  );  【4】功能:从stdin流中读取字符串,直至接受到换行符或EOF时停止,并将读取的结果存放在buffer指针所指向的字符数组中。换行符不作为读取串的内容,读取的换行符被转换为null值,并由此来结束字符串。  【5】注意:本函数可以无限读取,不会判断上限,所以程序员应该确保buffer的空间足够大,以便在执行读操作时不发生溢出。如果溢出,多出来的字符将被写入到缓冲区后面的内存位置,这将破坏一个或多个不相关变量的值。这个事实导致gets函数只适用于玩具程序。  【6】示例:  C:#include"stdio.h"  void main()  {  char str1[5];  gets(str1);  printf("%s/n",str1);  }  C++:  #include<iostream>  using namespace std;  int main()  {  char str[100];  gets(str);  cout<<str;  return 0;  }  gets()函数用来从标准输入设备(键盘)读取字符串直到换行符结束,但换行符会被丢弃,然后在末尾添加'/0'字符。其调用格式为:  gets(s);  其中s为字符串变量(字符串数组名或字符串指针)。  gets(s)函数与scanf("%s:",&s)/*  scanf("%s",s) */相似,但不完全相同,使用scanf("%s",&s);函数输入字符串时存在一个问题,就是如果输入了空格会认为字符串结束,空格后的字符将作为下一个输入项处理,但gets()函数将接收输入的整个字符串直到遇到换行为止。  说明:  gets(s);函数中的变量s为一字符串。如果为单个字符,编译连接不会有错误,但运行后会出现”Null pointer asignment"的错误。
  【7】宽字符版本,当使用unicode宽字符文本时,使用这个函数 _getws();

fputs

  函数名: fputs  功 能: 送一个字符串到一个流中  用 法: int fputs(char *string, FILE *stream);  参数: string是要写入文件的字符串。  stream 很明显是一个FILE对象。  程序例:  #include < stdio.h>  int main(void)  {  FILE *fpout=fopen("1.txt","wr");//  /* write a string to standard output */  fputs("Hello world/n", fpout);  return 0;
  }

puts

  功 能: 送一字符串到流stdout中  用 法: int puts(char *string);  程序例:  #include < stdio.h>  int main(void)  {  char string[] = "This is an example output string/n";  puts(string);  return 0;  }  初学者要注意以下例子  #include <stdio.h>  #include <conio.h>  int main(void)  {  int i;  char string[20];  for(i=0;i<10;i++)  string[i]='a';  puts(string);  getch();  return 0;  }  从此例中可看到puts输出字符串时要遇到'/0’也就是字符结束符才停止。如上面的程序加上一句 string[10]='/0';  #include <stdio.h>  #include <conio.h>  int main(void)  {  int i;  char string[20];  for(i=0;i<10;i++)  string[i]='a';  string[10]='/0';  puts(string);  getch();  return 0;  }  运行就正确了  此 外 puts 和 printf 的用法一样  (1)puts()函数用来向标准输出设备(屏幕)写字符串并换行,其调用方式为,puts(s);  其中s为字符串字符(字符串数组名或字符串指针)。  puts()函数的作用与语句“printf("%s/n",s);的作用相同。  说明:  puts()函数只能输出字符串,不能数值或进行格式变换。
  可以将字符串直接写入puts()函数中,输出至屏幕。如:puts("Hello,Turo C2.0:");

putc

  功 能: 输出一字符到指定流中  用 法: int putc(int ch, FILE *stream);  程序例:  #include <stdio.h>  int main(void)  {  char msg[] = "Hello world/n";  int i = 0;  while (msg[i])  putc(msg[i++],stdout);  return 0;  }

fgetc

目录

展开

编辑本段功 能

从流中读取字符。

编辑本段用法?

格式:int fgetc(FILE *stream);  意为从文件指针stream指向的文件中读取一个字符。  这个函数的返回值,是返回读取的一个字节。如果读到文件末尾返回EOF。

编辑本段程序例

#include <string.h>   #include <stdio.h>   #include <conio.h>   int main(void)   { FILE *stream;  char string[ ] = "This is a test";  char ch; /* open a file for update */  stream = fopen("DUMMY.FIL", "w+"); /* write a string into the file */  fwrite(string, strlen(string), 1, stream); /* seek to the beginning of the file */  fseek(stream, 0, SEEK_SET);  do  { /* read a char from the file */  ch = fgetc(stream); /* display the character */  putch(ch);  } while (ch != EOF);  fclose(stream);
  return 0; }

fscanf

  函数名: fscanf  功 能: 从一个流中执行格式化输入  用 法: int fscanf(FILE *stream, char *format,[argument...]);  int fscanf(文件指针,格式字符串,输入列表);  返回值:整型,数值等于[argument...]的个数  程序例:  #include <stdlib.h>  #include <stdio.h>  int main(void)  {  int i;  printf("Input an integer: ");  /* read an integer from the  standard input stream */  if (fscanf(stdin, "%d", &i))  printf("The integer read was: %d/n",  i);  else  {  fprintf(stderr, "Error reading an /  integer from stdin./n");  exit(1);  }  return 0;  }  返回EOF如果读取到文件结尾。  常用基本参数对照:  '%d' : Scan an integer as a signed decimal number.  '%i' : Scan an integer as a signed number. Similar to '%d', but interprets the number as hexadecimal when preceded by "0x" and octal when preceded by "0". For example, the string "031" would be read as 31 using '%d', and 25 using '%i'.  '%u' : Scan for decimal unsigned int, unsigned short, and unsigned char  '%f' : Scan a floating-point number in normal (fixed-point) notation.  '%g', '%G' : Scan a floating-point number in either normal or exponential notation. '%g' uses lower-case letters and '%G' uses upper-case.'%x', '%X' : Scan an integer as an unsigned hexadecimal number.  '%o' : Scan an integer as an octal number.  '%s' : Scan a character string. The scan terminates at whitespace. A null character is stored at the end of the string, which means that the buffer supplied must be at least one character longer than the specified input length.  '%c' : Scan a character (char). No null character is added.'(space)': Space scans for whitespace characters.  '%lf' : Scan as a double floating-point number.  '%Lf' : Scan as a long double floating-point number.  附:MSDN中例子  Example  /* FSCANF.C: This program writes formatted  * data to a file. It then uses fscanf to  * read the various data back from the file.  */  #include <stdio.h>  FILE *stream;  void main( void )  {  long l;  float fp;  char s[81];  char c;  stream = fopen( "fscanf.out", "w+" );  if( stream == NULL )  printf( "The file fscanf.out was not opened/n" );  else  {  fprintf( stream, "%s %ld %f%c", "a-string",  65000, 3.14159, 'x' );  /* Set pointer to beginning of file: */  fseek( stream, 0L, SEEK_SET );  /* Read data back from file: */  fscanf( stream, "%s", s );  fscanf( stream, "%ld", &l );  fscanf( stream, "%f", &fp );  fscanf( stream, "%c", &c );  /* Output data read: */  printf( "%s/n", s );  printf( "%ld/n", l );  printf( "%f/n", fp );  printf( "%c/n", c );  fclose( stream );  }
  }

fprintf

目录

编辑本段简介

c/c++语言函数: fprintf

编辑本段功 能

传送格式化输出到一个文件中

编辑本段用 法

#include <stdio.h>  int fprintf( FILE *stream, const char *format, ... );  fprintf()函数根据指定的format(格式)(格式)发送信息(参数)到由stream(流)指定的文件. fprintf()只能和printf()一样工作. fprintf()的返回值是输出的字符数,发生错误时返回一个负值.

编辑本段返回值

若成功则返回输出字符数,若输出出错则返回负值。

编辑本段程序例

/* Program to create backup of the  AUTOEXEC.BAT file */  #include < stdio.h>  int main(void)  {  FILE *in, *out;  if ((in = fopen("//AUTOEXEC.BAT", "rt")) == NULL)  {  fprintf(stderr, "Cannot open input file./n");  return 1;  }  if ((out = fopen("//AUTOEXEC.BAK", "wt")) == NULL)  {  fprintf(stderr, "Cannot open output file./n");  return 1;  }  while (!feof(in))  fputc(fgetc(in), out);  fclose(in);  fclose(out);  return 0;  }  举例用法:  #include <stdio.h>  #include <process.h>  FILE *stream;  void main( void )  {  int i = 10;  double fp = 1.5;  char s[] = "this is a string";  char c = '/n';  stream = fopen( "fprintf.out", "w" );  fprintf( stream, "%s%c", s, c );  fprintf( stream, "%d/n", i );  fprintf( stream, "%f/n", fp );  fclose( stream );  system( "type fprintf.out" );  }  屏幕输出:  this is a string  10  1.500000   格式化规定符  %d 十进制有符号整数  %u 十进制无符号整数  %f 浮点数  %s 字符串  %c 单个字符  %p  指针的值  %e 指数形式的浮点数  %x, %X 无符号以十六进制表示的整数  %0 无符号以八进制表示的整数  %g 自动选择合适的表示法

 

sscanf 目录 名称: 函数原型: 头文件: 说明: 支持集合操作: 例子: 编辑本段 名称:   sscanf() - 从一个字符串中读进与指定格式相符的数据. 编辑本段 函数原型:   Int sscanf( const char *, const char *, ...);   int scanf( const char *, ...); 编辑本段 头文件:   #include 编辑本段 说明:   sscanf与scanf类似,都是用于输入的,只是后者以键盘(stdin)为输入源,前者以固定字符串为输入源。    第一个参数可以是一个或多个 {%[*] [width] [{h | l | I64 | L}]type | ' ' | '/t' | '/n' | 非%符号}   注:   1、 * 亦可用于格式中, (即 %*d 和 %*s) 加了星号 (*) 表示跳过此数据不读入. (也就是不把此数据读入参数中)   2、{a|b|c}表示a,b,c中选一,[d],表示可以有d也可以没有d。   3、width表示读取宽度。   4、{h | l | I64 | L}:参数的size,通常h表示单字节size,I表示2字节 size,L表示4字节size(double例外),l64表示8字节size。   5、type :这就很多了,就是%s,%d之类。   6、特别的:%*[width] [{h | l | I64 | L}]type 表示满足该条件的被过滤掉,不会向目标参数中写入值 编辑本段 支持集合操作:   %[a-z] 表示匹配a到z中任意字符,贪婪性(尽可能多的匹配)   %[aB'] 匹配a、B、'中一员,贪婪性   %[^a] 匹配非a的任意字符,贪婪性 编辑本段 例子:   1. 常见用法。   char buf[512] ;   sscanf("123456 ", "%s", buf);//此处buf是数组名,它的意思是将123456以%s的形式存入buf中!   printf("%s/n", buf);   结果为:123456   2. 取指定长度的字符串。如在下例中,取最大长度为4字节的字符串。   sscanf("123456 ", "%4s", buf);   printf("%s/n", buf);   结果为:1234   3. 取到指定字符为止的字符串。如在下例中,取遇到空格为止字符串。   sscanf("123456 abcdedf", "%[^ ]", buf);   printf("%s/n", buf);   结果为:123456   4. 取仅包含指定字符集的字符串。如在下例中,取仅包含1到9和小写字母的字符串。   sscanf("123456abcdedfBCDEF", "%[1-9a-z]", buf);   printf("%s/n", buf);   结果为:123456abcdedf   当输入:   sscanf("123456abcdedfBCDEF","%[1-9A-Z]",buf);   printf("%s/n",buf);   结果为:123456   5. 取到指定字符集为止的字符串。如在下例中,取遇到大写字母为止的字符串。   sscanf("123456abcdedfBCDEF", "%[^A-Z]", buf);   printf("%s/n", buf);   结果为:123456abcdedf   6、给定一个字符串iios/12DDWDFF@122,获取 / 和 @ 之间的字符串,先将 "iios/"过滤掉,再将非'@'的一串内容送到buf中   sscanf("iios/12DDWDFF@122", "%*[^/]/%[^@]", buf);   printf("%s/n", buf);   结果为:12DDWDFF   7、给定一个字符串““hello, world”,仅保留world。(注意:“,”之后有一空格)   sscanf(“hello, world”, "%*s%s", buf);   printf("%s/n", buf);   结果为:world   %*s表示第一个匹配到的%s被过滤掉,即hello被过滤了   如果没有空格则结果为NULL。   sscanf的功能很类似于正则表达式, 但却没有正则表达式强大,所以如果对于比较复杂的字符串处理,建议使用正则表达式.   //-------------------------------------------------------   用它来分隔类似这样的字符串2006:03:18:   int a, b, c;   /*sscanf("2006:03:18", "%d:%d:%d", a, b, c); */ /*错误方法, 要在变量a,b,c前加上取地址符, modified by huanmie_09*/   sscanf("2006:03:18", "%d:%d:%d", &a, &b, &c);   以及2006:03:18 - 2006:04:18:   char sztime1[16] = "", sztime2[16] = "";   sscanf("2006:03:18 - 2006:04:18", "%s - %s", sztime1, sztime2);   但是后来,我需要处理2006:03:18-2006:04:18   仅仅是取消了‘-’两边的空格,却打破了%s对字符串的界定。   我需要重新设计一个函数来处理这样的情况?这并不复杂,但是,为了使所有的代码都有统一的风格,我需要改动很多地方,把已有的sscanf替换成我自己的分割函数。我以为我肯定需要这样做,并伴随着对sscanf的强烈不满而入睡;一觉醒来,发现其实不必。   format-type中有%[]这样的type field。如果读取的字符串,不是以空格来分隔的话,就可以使用%[]。   %[]类似于一个正则表达式。[a-z]表示读取a-z的所有字符,[^a-z]表示读取除a-z以外的所有字符。   所以那个问题也就迎刃而解了:   sscanf("2006:03:18 - 2006:04:18", "%[0-9,:] - %[0-9,:]", sztime1, sztime2);   在softmse (Jake) 的问题贴http://community.csd(去掉我)n.n(去掉我)et/Expert/topic/4843/4843294.xml?temp=.4321558中 ,给出了一个很cool的sscanf用例,而后通过学习,发现sscanf真棒,现做一总结。   原问题:   iios/12DDWDFF@122   获取/和@之间的字符串怎么做   C程序里面有什么函数吗?   代码:   #include   int main()   {   const char* s = "iios/12DDWDFF@122";   char buf[20];   sscanf( s, "%*[^/]/%[^@]", buf );   printf( "%s/n", buf );   return 0;   }   结果为:12DDWDFF   sscanf与scanf类似,都是用于输入的,只是后者以屏幕(stdin)为输入源,前者以固定字符串为输入源。   函数原型:   int scanf( const char *format [,argument]... );   其中的format可以是一个或多个 {%[*] [width] [{h | l | I64 | L}]type | ' ' | '/t' | '/n' | 非%符号},   注:{a|b|c}表示a,b,c中选一,[d],表示可以有d也可以没有d。   width:宽度,一般可以忽略,用法如:   const char sourceStr[] = "hello, world";   char buf[10] = ;   sscanf(sourceStr, "%5s", buf); //%5s,只取5个字符   cout << buf<< endl;   结果为:hello   {h | l | I64 | L}:参数的size,通常h表示单字节size,I表示2字节 size,L表示4字节size(double例外),l64表示8字节size。   type :这就很多了,就是%s,%d之类。   特别的:   %*[width] [{h | l | I64 | L}]type 表示满足该条件的被过滤掉,不会向目标参数中写入值。如:   const char sourceStr[] = "hello, world";   char buf[10] = ;   sscanf(sourceStr, "%*s%s", buf); //%*s表示第一个匹配到的%s被过滤掉,即hello被过滤了   cout << buf<< endl;   结果为:world   支持集合操作:   %[a-z] 表示匹配a到z中任意字符,贪婪性(尽可能多的匹配)   %[aB'] 匹配a、B、'中一员,贪婪性   %[^a] 匹配非a的任意字符,贪婪性   是不是感觉眼熟了啊,不错,这和正则表达式很相似,而且仍然支持过滤,即可以有%*[a-z].如:   例子回顾:   const char* s = "iios/12DDWDFF@122";   char buf[20];   sscanf( s, "%*[^/]/%[^@]", buf );   printf( "%s/n", buf );   由例子3-》取到指定字符为止的字符串。如在下例中,取遇到空格为止字符串。   sscanf("123456 abcdedf", "%[^ ]", buf);   printf("%s/n", buf);   结果为:123456   所以代码总结应该为:   const char* s = "iios/12DDWDFF@122";   char buf[20];   sscanf( s, "%*[^/]/%[^@]", buf );   printf( "%s/n", buf );   先将 "iios/"过滤掉,再将到字符'@'为止的一串12DDWDFF(由例3可得此串到@为止,把@122舍掉)内容即是:12DDWDFF送到buf中,得到结果。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值