CPRIMER CHAP13

First, file

  C the documents as a series of contiguous bytes, each byte is read individually.

  Provides two access path to the file: binary and text mode.

  Standard file: standard input, standard output and standard error output

  Standard input function: getchar, scanf

  1.1 getchar()

  C library function  int getchar (void)  from the stdin Gets a character (an unsigned char). This is equivalent to  getc  with stdin as a parameter.

  The function unsigned char cast returns an int for reading character, if the end of file or a read error occurs, the EOF is returned.  

1 #include <stdio.h>
2 int main(void)
3 {
4     char ch;
5     ch = getchars();
6     printf("%c", ch);
7 }

  1.2 scanf()

  C library function  int scanf (const char * format, ...)  from the stdin read formatted input.

  parameter

  • format  - this is a C string containing one or more of the following: a space character, non-space character  and the  format specifier .
    format specifier form  [=% [*] [width] [modifiers] = type] , explain in detail as follows:
参数 描述
* 这是一个可选的星号,表示数据是从流 stream 中读取的,但是可以被忽视,即它不存储在对应的参数中。
width 这指定了在当前读取操作中读取的最大字符数。
modifiers 为对应的附加参数所指向的数据指定一个不同于整型(针对 d、i 和 n)、无符号整型(针对 o、u 和 x)或浮点型(针对 e、f 和 g)的大小: h :短整型(针对 d、i 和 n),或无符号短整型(针对 o、u 和 x) l :长整型(针对 d、i 和 n),或无符号长整型(针对 o、u 和 x),或双精度型(针对 e、f 和 g) L :长双精度型(针对 e、f 和 g)
type 一个字符,指定了要被读取的数据类型以及数据读取方式。具体参见下一个表格。

  scanf type specifier:

类型 合格的输入 参数的类型
c 单个字符:读取下一个字符。如果指定了一个不为 1 的宽度 width,函数会读取 width 个字符,并通过参数传递,把它们存储在数组中连续位置。在末尾不会追加空字符。 char *
d 十进制整数:数字前面的 + 或 - 号是可选的。 int *
e,E,f,g,G 浮点数:包含了一个小数点、一个可选的前置符号 + 或 -、一个可选的后置字符 e 或 E,以及一个十进制数字。两个有效的实例 -732.103 和 7.12e4 float *
o 八进制整数。 int *
s 字符串。这将读取连续字符,直到遇到一个空格字符(空格字符可以是空白、换行和制表符)。 char *
u 无符号的十进制整数。 unsigned int *
x,X 十六进制整数。 int *
  • Additional parameters  - depending on the format string, the function may need a series of additional parameters, each containing a value to be inserted, replacing the format specified in the parameter% each tag. The number of parameters to be equal to the number of tags%.

  return value

  If successful, the function returns the number of successfully matched and assigned. If you reach the end of the file or read error occurs, it returns EOF.

  Note: When the format mismatch scanf read, the reading width constant gap, line breaks, tabs, the reading is stopped, and read the contents stored in the specified address space.

  

1 #include <stdio.h>
2 int main(void)
3 {
4     char name[20];
5     printf("enter your name:\n");
6     scanf("%s", name);
7     printf("your name is %s\n", name);
8     return 0;
9 }

 

  Standard output functions: putchar, puts, printf

  1.3 putchar()

  C library function  int putchar (int char)  The char parameter specified character (unsigned char a) is written to standard output stdout.

  The function to return unsigned char cast in the form of written characters int, EOF is returned if an error occurs.

1 #include <stdio.h>
2 
3 int main(void)
4 {
5     char ch;
6     for (ch = 'A'; ch <= 'Z'; ch++)
7         putchar(ch);
8     return 0;
9 }

 

  

Guess you like

Origin www.cnblogs.com/xiaofeiIDO/p/6999458.html
13