龙空技术网

优先使用iostream进行I / O

面向对象思考 181

前言:

目前姐妹们对“c语言gets_s的用法”大致比较关心,小伙伴们都想要学习一些“c语言gets_s的用法”的相关知识。那么小编同时在网摘上网罗了一些关于“c语言gets_s的用法””的相关内容,希望你们能喜欢,我们一起来学习一下吧!

SL.io.3: Prefer iostreams for I/OSL.io.3:优先使用iostream进行I / O

Reason(原因)

iostreams are safe, flexible, and extensible.

iostream安全,灵活且可扩展。

Example(示例)

// write a complex number:complex<double> z{ 3, 4 };cout << z << '\n';

complex is a user-defined type and its I/O is defined without modifying the iostream library.

complex是用户定义的类型,无需修改iostream库即可定义其I / O。

Example(示例)

// read a file of complex numbers:for (complex<double> z; cin >> z; )    v.push_back(z);

Exception(例外)

??? performance ???

??? 性能???

Discussion: iostreams vs. the printf() family

讨论:iostreams与printf()函数群

It is often (and often correctly) pointed out that the printf() family has two advantages compared to iostreams: flexibility of formatting and performance. This has to be weighed against iostreams advantages of extensibility to handle user-defined types, resilient against security violations, implicit memory management, and locale handling.

人们经常(经常正确地)指出,与iostream相比,printf()函数群具有两个优点:格式化的灵活性和性能。必须权衡iostream的优势:处理用户定义的类型时的可扩展性,可抵御安全性违规,隐式内存管理和区域设置处理。

If you need I/O performance, you can almost always do better than printf().

如果需要保证I / O性能,iostream几乎可以总是比printf()更好。

gets(), scanf() using %s, and printf() using %s are security hazards (vulnerable to buffer overflow and generally error-prone). C11 defines some "optional extensions" that do extra checking of their arguments. If present in your C library, gets_s(), scanf_s(), and printf_s() might be safer alternatives, but they are still not type safe.

使用%s的gets(),scanf()和使用%s的printf()都有安全隐患(很容易造成缓冲区溢出并且通常容易出错)。C11定义了一些“可选扩展”,对它们的参数进行了额外的检查。如果是在使用C语言库,gets_s(),scanf_s()和printf_s()可能是更安全的选项,但它们仍然不是类型安全的。

Enforcement(实施建议)

Optionally flag <cstdio> and <stdio.h>.

(可选)标记<cstdio>和<stdio.h>。

原文链接

新书介绍

《实战Python设计模式》是作者最近出版的新书,拜托多多关注!

本书利用Python 的标准GUI 工具包tkinter,通过可执行的示例对23 个设计模式逐个进行说明。这样一方面可以使读者了解真实的软件开发工作中每个设计模式的运用场景和想要解决的问题;另一方面通过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。

对设计模式感兴趣而且希望随学随用的读者通过本书可以快速跨越从理解到运用的门槛;希望学习Python GUI 编程的读者可以将本书中的示例作为设计和开发的参考;使用Python 语言进行图像分析、数据处理工作的读者可以直接以本书中的示例为基础,迅速构建自己的系统架构。

觉得本文有帮助?请分享给更多人。

关注微信公众号【面向对象思考】轻松学习每一天!

面向对象开发,面向对象思考!

标签: #c语言gets_s的用法