C语言 fwirte 函数 - C语言零基础入门教程 左手的ㄟ右手 2022-09-15 00:57 162阅读 0赞 目录 * 一.fwirte函数简介 * 二.fwirte函数使用 * 三.猜你喜欢 > 零基础 C/C++ 学习路线推荐 : [C/C++ 学习目录][C_C_] >> [C 语言基础入门][C] ## 一.fwirte函数简介 ## [C 语言][C]文件读写,[fread 函数][fread]用于读取文件中的数据到指定缓冲区中,而 fwrite 函数用于把缓冲区数据写入到文件中,[fwrite 函数][fwrite]声明如下: /* *描述:写入数据到缓冲区文件 * *参数: * [in] ptr:缓冲区,用于写入到文件的数据 * [in] size:每个元素的大小(单位是字节) * [in] nmemb:要写入的元素个数 * [in] stream:文件指针句柄 * *返回值:如果成功,该函数返回一个 size_t 对象,表示写入文件的元素的总数,该对象是一个整型数据类型。如果该数字与 nmemb 参数不同,则会显示一个错误。 */ size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) > **返回成功写入文件的元素个数,若出现错误或到达文件末尾,则可能小于 nmemb ;** > > **[fwrite 函数][fwrite]不区分文件尾和错误,因此调用者必须用 [feof][] 和 [ferror][] 才能判断发生了什么;** ## 二.fwirte函数使用 ## /******************************************************************************************/ //@Author:猿说编程 //@Blog(个人博客地址): www.codersrc.com //@File:C语言教程 - C 语言 fwirte 函数 //@Time:2021/07/20 07:30 //@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! /******************************************************************************************/ #include <stdio.h> #include <string.h> int main() { FILE *fp; char c[] = "www.codersrc.com"; char buffer[2048] = {0}; /* 打开文件用于读写 */ fp = fopen("file.txt", "w+"); /* 写入数据到文件 */ fwrite(c, strlen(c) + 1, 1, fp); /* 偏移到文件文件的开头 */ fseek(fp, 0, SEEK_SET); /* 读取并显示数据 */ fread(buffer,1 , strlen(c)+1, fp); printf("%s\n", buffer); fclose(fp); return(0); } 三.猜你喜欢 1. [C语言 数组下标越界和内存溢出区别][C_] 2. [C语言 使用指针遍历数组][C_ 1] 3. [C语言 指针和数组区别][C_ 2] 4. [C语言 指针数组和数组指针区别][C_ 3] 5. [C语言 野指针][C_ 4] 6. [C语言 函数值传递和址传递][C_ 5] 7. [C语言 函数不定长参数][C_ 6] 8. [C语言 函数指针][C_ 7] 9. [C语言 指针函数][C_ 8] 10. [C语言 回调函数 callback][C_ _ callback] 11. [C语言 \#pragma once][C_ _pragma once] 12. [C语言 \#include <> 与 \#include “” 区别][C_ _include _ _ _include _] 13. [C语言 const 修饰函数参数][C_ const] 14. [C语言 const 和 define 区别][C_ const _ define] 15. [C语言 \#运算符][C_ 9] 16. [C语言 \#\#运算符][C_ 10] 17. [C语言 \_\_VA\_ARGS\_\_][C_ _VA_ARGS] 18. [C语言 \#\#\_\_VA\_ARGS\_\_][C_ _VA_ARGS 1] 19. [C语言 函数不定长参数 \#\#\_\_VA\_ARGS\_\_经典案例][C_ _ _VA_ARGS] 20. [C语言 va\_start / va\_end / va\_arg 自定义 printf 函数][C_ va_start _ va_end _ va_arg _ printf] 21. [C语言 main 函数][C_ main] 22. [C语言 main 函数参数 main(int argc, char \*argv\[\])][C_ main _ main_int argc_ char _argv] 23. [C语言 局部变量][C_ 11] 24. [C语言 全局变量][C_ 12] 25. [C语言 全局变量和局部变量区别][C_ 13] 26. [C语言 static][C_ static] 27. [C语言 extern][C_ extern] 未经允许不得转载:[猿说编程][Link 1] » [C语言 fwirte 函数][fwrite] [C_C_]: https://www.codersrc.com/c-c [C]: https://www.codersrc.com/c%e8%af%ad%e8%a8%80%e5%9f%ba%e7%a1%80 [fread]: https://www.codersrc.com/archives/10100.html [fwrite]: https://www.codersrc.com/archives/10122.html [feof]: https://www.codersrc.com/archives/10214.html [ferror]: https://www.codersrc.com/archives/10203.html [C_]: https://www.codersrc.com/archives/8331.html [C_ 1]: https://www.codersrc.com/archives/8540.html [C_ 2]: https://www.codersrc.com/archives/8564.html [C_ 3]: https://www.codersrc.com/archives/8613.html [C_ 4]: https://www.codersrc.com/archives/8700.html [C_ 5]: https://www.codersrc.com/archives/8738.html [C_ 6]: https://www.codersrc.com/archives/8780.html [C_ 7]: https://www.codersrc.com/archives/8793.html [C_ 8]: https://www.codersrc.com/archives/8809.html [C_ _ callback]: https://www.codersrc.com/archives/8832.html [C_ _pragma once]: https://www.codersrc.com/archives/8931.html [C_ _include _ _ _include _]: https://www.codersrc.com/archives/9045.html [C_ const]: https://www.codersrc.com/archives/9106.html [C_ const _ define]: https://www.codersrc.com/archives/9123.html [C_ 9]: https://www.codersrc.com/archives/9423.html [C_ 10]: https://www.codersrc.com/archives/9438.html [C_ _VA_ARGS]: https://www.codersrc.com/archives/9450.html [C_ _VA_ARGS 1]: https://www.codersrc.com/archives/9480.html [C_ _ _VA_ARGS]: https://www.codersrc.com/archives/9499.html [C_ va_start _ va_end _ va_arg _ printf]: https://www.codersrc.com/archives/9575.html [C_ main]: https://www.codersrc.com/archives/9680.html [C_ main _ main_int argc_ char _argv]: https://www.codersrc.com/archives/9687.html [C_ 11]: https://www.codersrc.com/archives/9815.html [C_ 12]: https://www.codersrc.com/archives/9836.html [C_ 13]: https://www.codersrc.com/archives/9856.html [C_ static]: https://www.codersrc.com/archives/9895.html [C_ extern]: https://www.codersrc.com/archives/9944.html [Link 1]: https://www.codersrc.com
还没有评论,来说两句吧...