【421】C语言输入输出函数说明
目录:
- sscanf & sprintf
- scanf & printf
- getchar & putchar
- fgets & fputs
- fscanf & fprintf
- fgetc & fputc
- fopen
- rewind
- fclose
1. sscanf & sprintf
sscanf 从字符串读取格式化输入。可以用来将字符串转换为数字,同时可以判断是否转换成功。通过返回值来判断是否转换成功。
【语法】int sscanf(const char *str, const char *format, ..pointers..)
- str:这是 C 字符串,是函数检索数据的源。【常量】
- format:这是 C 字符串,包含了以下各项中的一个或多个:空格字符、非空格字符 和 format 说明符。【常量】
format 说明符形式为 [=%[*][width][modifiers]type=] - 附加参数:这个函数接受一系列的指针作为附加参数,每一个指针都指向一个对象,对象类型由 format 字符串中相应的 % 标签指定,参数与 % 标签的顺序相同。【变量】
【返回值】如果成功,该函数返回成功匹配和赋值的个数。如果到达文件末尾或发生读错误,则返回 EOF。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #include <stdio.h> int main() { //-- string to int --// char *num_str = "100" ; int num; // get the num from string // Firstly, change string to num_str // if successing, return numbers of assignment if ( sscanf (num_str, "%d" , &num) == 1) { printf ( "The number is %d.\n" , num); } // output: The number is 100. //-- get muliple numbers --// char *date_today = "Monday July 1 2019" ; int day, year; char weekday[10], month[10]; // seperated by space if ( sscanf (date_today, "%s %s %d %d" , weekday, month, &day, &year) == 4) { printf ( "Today is %s %s %d %d.\n" , weekday, month, day, year); } // output: Today is Monday July 1 2019. // use comma // function will trim space and tab automatically // problem is that you should know the format before // so you can add the format appropriately char *date_today1 = " Monday, July, 1, 2019 " ; if ( sscanf (date_today, "%s, %s, %d, %d" , weekday, month, &day, &year) == 4) { printf ( "Today is %s %s %d %d.\n" , weekday, month, day, year); } // output: Today is Monday July 1 2019. return 0; } |
sprintf 发送格式化输出到 str 所指向的字符串。可以用来将数字(各种格式)转化为字符串,同时可以判断是否转换成功。通过返回值来判断是否转换成功。
【语法】int sprintf(char *str, const char *format, ...)
- str:这是 C 字符串,是函数检索数据的源。
- format:这是字符串,包含了要被写入到字符串 str 的文本。它可以包含嵌入的 format 标签,format 标签可被随后的附加参数中指定的值替换,并按需求进行格式化。
format 说明符形式为 [=%[*][width][modifiers]type=]
说明:格式书写的方法与 prinf 类似,可以随便写。 - 附加参数:根据不同的 format 字符串,函数可能需要一系列的附加参数,每个参数包含了一个要被插入的值,替换了 format 参数中指定的每个 % 标签。参数的个数应与 % 标签的个数相同。
【返回值】如果成功,则返回写入的字符总数,不包括字符串追加在字符串末尾的空字符。如果失败,则返回一个负数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include <stdio.h> int main() { // get the numbers int nums[5] = {1, 2, 3, 4, 5}; char str[20]; if ( sprintf (str, "{%d, %d, %d, %d, %d}" , nums[0], nums[1], nums[2], nums[3], nums[4]) > 0) { printf ( "The string is %s.\n" , str); } // output: The string is {1, 2, 3, 4, 5}. // get addresses of numbers char str_add[200]; if ( sprintf (str_add, "\nnums[0]: %p \nnums[1]: %p \nnums[2]: %p \nnums[3]: %p \nnums[4]: %p" , &nums[0], &nums[1], &nums[2], &nums[3], &nums[4]) > 0) { printf ( "The string is %s.\n" , str_add); } // output: // The string is // nums[0]: 0x7fff737bfcb0 // nums[1]: 0x7fff737bfcb4 // nums[2]: 0x7fff737bfcb8 // nums[3]: 0x7fff737bfcbc // nums[4]: 0x7fff737bfcc0. return 0; } |
2. scanf & printf
scanf 从标准输入 stdin 读取格式化输入。可以用来将读取的字符串转换为数字,同时可以判断是否转换成功。通过返回值来判断是否转换成功。
与 sscanf 的区别:scanf 是标准 stdin 读取,即控制台输入或者文件读取,而 sscanf 则是从字符串读取,只要将字符串去掉,两者就一样了。
【语法】int scanf(const char *format, ..pointers..)
- format:这是 C 字符串,包含了以下各项中的一个或多个:空格字符、非空格字符 和 format 说明符。【常量】
format 说明符形式为 [=%[*][width][modifiers]type=] - 附加参数:这个函数接受一系列的指针作为附加参数,每一个指针都指向一个对象,对象类型由 format 字符串中相应的 % 标签指定,参数与 % 标签的顺序相同。【变量】
【返回值】如果成功,该函数返回成功匹配和赋值的个数。如果到达文件末尾或发生读错误,则返回 EOF。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | // counts+.c // reads an integer from stdin and counts // prompts the user #include <stdio.h> #include <stdlib.h> int main( void ) { int num_start; int num_end; printf ( "Please input two numbers: " ); // this line added to counts.c if ( scanf ( "%d%d" , &num_start, &num_end) != 2) { fprintf (stderr, "Usage: two numbers expected\n" ); return EXIT_FAILURE; } for ( int i = num_start; i <= num_end; i++) { printf ( "%d " ,i); } printf ( "\n" ); // output: // input from terminal // alex@alex-VirtualBox:Blogs$ gcc scanf.c && ./a.out // Please input two numbers: 5 10 // 5 6 7 8 9 10 // input from file // alex@alex-VirtualBox:Blogs$ gcc scanf.c && ./a.out < input.txt // Please input two numbers: 5 6 7 8 9 10 // input from echo // alex@alex-VirtualBox:Blogs$ gcc scanf.c && echo 5 10 | ./a.out < input.txt // Please input two numbers: 5 6 7 8 9 10 return EXIT_SUCCESS; } |
printf 发送格式化输出到标准输出 stdout。可以用来将数字(各种格式)转化为控制台输出或者输出到文件,同时可以判断是否输出成功。
与 sprintf 的区别:printf 是标准 stdout 输出,即控制台输出或者文件输出,而 sprintf 则是转为字符串,只要将字符串去掉,两者就一样了。
【语法】int printf(const char *format, ...)
- format:这是字符串,包含了要被写入到字符串 str 的文本。它可以包含嵌入的 format 标签,format 标签可被随后的附加参数中指定的值替换,并按需求进行格式化。
format 说明符形式为 [=%[*][width][modifiers]type=] - 附加参数:根据不同的 format 字符串,函数可能需要一系列的附加参数,每个参数包含了一个要被插入的值,替换了 format 参数中指定的每个 % 标签。参数的个数应与 % 标签的个数相同。
【返回值】如果成功,则返回写入的字符总数,否则返回一个负数。
3. getchar & putchar
getchar 从标准输入 stdin 获取一个字符(一个无符号字符)。这等同于 getc 带有 stdin 作为参数。
【语法】int getchar(void)
【返回值】该函数以无符号 char 强制转换为 int 的形式返回读取的字符,如果到达文件末尾或发生读错误,则返回 EOF。
putchar 把参数 char 指定的字符(一个无符号字符)写入到标准输出 stdout 中。
【语法】int putchar(int char)
- char:这是要被写入的字符。该字符以其对应的 int 值进行传递。
【返回值】该函数以无符号 char 强制转换为 int 的形式返回写入的字符,如果发生错误则返回 EOF。
【注意】同时可以通过控制台输入输出以及文件输入输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <stdio.h> int main() { // getchar() will read characters one by one // for this function, it will print the same characters untill it finds '\n' char c = getchar (); while (c != '\n' ) { putchar (c); c = getchar (); } putchar ( '\n' ); // output: // alex@alex-VirtualBox:Blogs$ gcc getchar.c && ./a.out // alex lee // alex lee return 0; } |
4. fgets & fputs
fgets 从指定的流 stream 读取一行,并把它存储在 str 所指向的字符串内。当读取 (n-1) 个字符时,或者读取到换行符时,或者到达文件末尾时,它会停止,具体视情况而定。
【语法】char *fgets(char *str, int n, FILE *stream)
- str -- 这是指向一个字符数组的指针,该数组存储了要读取的字符串。
- n -- 这是要读取的最大字符数(包括最后的空字符)。通常是使用以 str 传递的数组长度。
- stream -- 这是指向 FILE 对象的指针,该 FILE 对象标识了要从中读取字符的流。
【返回值】如果成功,该函数返回相同的 str 参数。如果到达文件末尾或者没有读取到任何字符,str 的内容保持不变,并返回一个空指针。如果发生错误,返回一个空指针。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <stdio.h> #include <stdlib.h> int main(){ // read from terminal char str_terminal[20]; fgets (str_terminal, 20, stdin); // '\n' is also included printf ( "Read from terminal is %s.\n" , str_terminal); // output: // alex lee // Read from terminal is alex lee // . // read from file char str_file[20]; FILE *fp; fp = fopen ( "file.txt" , "r" ); fgets (str_file, 20, fp); printf ( "Read from file is %s.\n" , str_file); fclose (fp); // output: // alex lee // Read from terminal is alex lee return 0; } |
fputs 把字符串写入到指定的流 stream 中,但不包括空字符。
【语法】int fputs(const char *str, FILE *stream)
- str -- 这是一个数组,包含了要写入的以空字符终止的字符序列。
- stream -- 这是指向 FILE 对象的指针,该 FILE 对象标识了要被写入字符串的流。
【返回值】该函数返回一个非负值,如果发生错误则返回 EOF。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <stdio.h> #include <stdlib.h> int main(){ FILE *fp; fp = fopen ( "file.txt" , "w+" ); fputs ( "This is C programming lnaguage.\n" , fp); fputs ( "This is a kind of programming languages.\n" , fp); fclose (fp); // outputs: // alex@alex-VirtualBox:Blogs$ gcc fputs.c && ./a.out && more file.txt // This is C programming language. // This is a kind of programming languages. return 0; } |
5. fscanf & fprintf
fscanf 发送格式化输出到流 stream 中。
【语法】int fscanf(FILE *stream, const char *format, ..pointer..)
- stream:这是指向 FILE 对象的指针,该 FILE 对象标识了流。
- format:这是 C 字符串,包含了以下各项中的一个或多个:空格字符、非空格字符 和 format 说明符。
format 说明符形式为 [=%[*][width][modifiers]type=] - 附加参数:根据不同的 format 字符串,函数可能需要一系列的附加参数,每个参数包含了一个要被插入的值,替换了 format 参数中指定的每个 % 标签。参数的个数应与 % 标签的个数相同。
【返回值】如果成功,该函数返回成功匹配和赋值的个数。如果到达文件末尾或发生读错误,则返回 EOF。
【注意】其实与 scanf 或者 sscanf 都类似,只是这个要从文件读取,并且占用参数位置。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include <stdio.h> #include <stdlib.h> int main(){ char str1[10], str2[10], str3[10]; int year; FILE *fp; fp = fopen ( "file.txt" , "w+" ); fputs ( "We are in 2019" , fp); // set the file position to the beginning // of the file of the given stream rewind (fp); fscanf (fp, "%s %s %s %d" , str1, str2, str3, &year); printf ( "Read String1 |%s|\n" , str1); printf ( "Read String2 |%s|\n" , str2); printf ( "Read String3 |%s|\n" , str3); printf ( "Read String4 |%d|\n" , year); fclose (fp); // output: // Read String1 |We| // Read String2 |are| // Read String3 |in| // Read String4 |2019| return 0; } |
fprintf 发送格式化输出到流 stream 中。
【语法】int fprintf(FILE *stream, const char *format, ...)
- stream -- 这是指向 FILE 对象的指针,该 FILE 对象标识了流。
- format -- 这是 C 字符串,包含了要被写入到流 stream 中的文本。它可以包含嵌入的 format 标签,format 标签可被随后的附加参数中指定的值替换,并按需求进行格式化。format 标签属性是 %[flags][width][.precision][length]specifier
- 附加参数 -- 根据不同的 format 字符串,函数可能需要一系列的附加参数,每个参数包含了一个要被插入的值,替换了 format 参数中指定的每个 % 标签。参数的个数应与 % 标签的个数相同。
【返回值】如果成功,则返回写入的字符总数,否则返回一个负数。
【注意】包括 stderr,stdout,标准输入则为 stdin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <stdio.h> #include <stdlib.h> int main(){ FILE *fp; fp = fopen ( "file.txt" , "w+" ); fprintf (fp, "%s %s %s %d" , "We" , "are" , "in" , 2019); fclose (fp); // output: // alex@alex-VirtualBox:Blogs$ gcc fprintf.c && ./a.out && more file.txt // We are in 2019 return 0; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)