C

10. 输入输出

10. IO(in/outpot)

Posted by xuepro on October 30, 2017

输入输出(IO)

标准输入输出库提供了各种各样的输入输出函数,要使用这些函数,需要包含

标准文件的输入输出(读写)

  • 1).字符级的输入输出(Character level I/O)

有2个常用的函数getchr() , putchar() 分别用于输入和输出一个字符。 getchar()从标准输入stdin(键盘)中输入一个字符,putchar( )向标准输出stdout (终端输出窗口)输出一个字符。 例如:

 #include < stdio.h>
 void main()
 {
    int i, nc;
    
    nc = 0;
    i = getchar();
    while (i != EOF) {
    nc = nc + 1;
    i = getchar();
    }
    printf("Number of characters in file = %d\n", nc);
}

注: 1)EOF是一个宏常量表示输入流stdin或文件的结束字符。#define关键字用于定义一个宏常量,即给一个常量起一个名字,例如: #define PI 3.141593 表示标识符 PI 就是 数值3.141593 2)有经验的程序员,可以将上述代码写成更简洁的形式:

 #include < stdio.h>

  void main()
  {
       int c, nc = 0;
       while ( (c = getchar()) != EOF ) nc++;
       printf("Number of characters in file = %d\n", nc);
   }
  • 2).字符串级的输入输出(高层输入输出Higher-Level I/O: string level I/O)

  gets和puts分别用于读写字符串:

char *gets(char *s) 
int puts(const char *s) 

例如:

#include <stdio.h>
int main( ) {

   char str[100];

   printf( "Enter a value :");
   gets( str );

   printf( "\nYou entered: ");
   puts( str );

   return 0;
}

Unix没有gets和puts,需要用fgets和fputs函数,其中要指定相应的输入输出流是哪个?例如

#include <stdio>
void main(){
    char buf[1024] ;  /*存放输入字符串的字符数组*/
    printf("please enter a string\n");
    fgets(buf,100,stdin); /*从标准输入流stdin即键盘输入最多100个字符到buf*/
    printf("print the string in buf \n");
    fputs(buf,stdout); /* 将buf中的字符串输出到从标准输出流stdout,即屏幕上*/
}
  • 3).格式化输入输出(高层输入输出Higher-Level I/O )   scanf和printf分别用于读写字符串:
    int scanf(const char *format, ...)
    int printf(const char *format, ...)
    

    按照输入输出格式串format的格式进行输入输出(读写)。 format串可以是:

  • 普通常量字符串
  • 格式串,如%s, %d, %c, %f, 等, 分别用于输入输出字符串strings,整数integer,字符character 或浮点数 float等。
  • 转移字符,如\n,\t,\, "等,分别表示换行、制表符、斜杠、双引号等。
    #include <stdio.h>
         void main(){
               int a; 
               float b;
                scanf("%d %f", &a, &b);
                printf("%8.3f \n", a+b);
         }
    

2. 从或向字符串进行输入输出:

    sscanf(string, "format string", variables);
    sprintf(string, "format string", variables);   

其中string是一个指向字符数组的指针,这些函数用于从它指向的字符数组读取数据或向它指向的字符数组中输出数据。

sscanf示例

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main () {
   int day, year;
   char weekday[20], month[20], dtm[100];

   strcpy( dtm, "Saturday March 25 1989" );
   sscanf( dtm, "%s %s %d  %d", weekday, month, &day, &year );

   printf("%s %d, %d = %s\n", month, day, year, weekday );
    
   return(0);
}

执行后输出为:

March 25, 1989 = Saturday

sprintf示例

#include <stdio.h>
#include <math.h>

int main () {
   char str[80];

   sprintf(str, "Value of Pi = %f", M_PI);
   puts(str);
   
   return(0);
}

执行后输出为:

Value of Pi = 3.141593

3. 文件输入输出(I/O to and from files)

标准输入输出库提供了类似的读写文件的函数。
#include < stdio.h>
/*下面的是文件格式化输入输出的伪代码*/
    FILE *fp;          /*用FILE* 类型的指针存储打开的文件的指针*/                              
    fp = fopen(name, mode);    /*fopen通用于打开或创建文件*/
    fscanf(fp, "format string", variable list); /*fscanf按照输入格式串的规定,从文件中读取数据到变量列表 variable list*/
    fprintf(fp, "format string", variable list); /* fprintf按照输出格式串的规定,将变量列表 variable list写入到文件中*/
    fclose(fp );  /*关闭文件*/

注:

  • 1) fp是一个FILE类型的指针变量,用于指向FILE类型的变量,其中存储了读写文件的信息,如当前文件的读写位置等。

  • 2) 采用类似的fscanf和fprintf对文件进行读写。

  • 3) 打开的文件最后用fclose()关闭!

  • 4) 其中的name表示被打开文件的文件名,而mode则表示该文件被打开的方式,如‘r’表示读文件(reading),’w’表示写文件(writing),’a’表示在文件后面追加(append)信息.

下面的可执行代码表示想一个foo.dat的文件中写入数据:

#include < stdio.h>

void main()
{
    FILE *fp;
    int i;

    fp = fopen("foo.dat", "w");        /* open foo.dat for writing */

    fprintf(fp, "\nSample Code\n\n");  /* write some info */
    for (i = 1; i <= 10 ; i++)
    fprintf(fp, "i = %d\n", i);

    fclose(fp);                   /* close the file  */
}

支付宝打赏 微信打赏

您的打赏是对我最大的鼓励!