学习控制台输入输出

《Hands-On System Programming with C++》读书笔记之六

基于流的IO

与C语言的printf()和scanf()不同,C++的输入输出基于流。
std::cout和std::wcout对象都是std::ostream类的实例,他们输出数据到标准C的stdout(即当前标准输出设备,可以是屏幕、打印机或文件),前者支持ASCII,后者支持Unicode。
类似地,std::cin和std::wcin接收来自stdin的数据。

C++基于流的IO之优劣

C++基于流的IO之优势

  • 简洁、安全的支持用户定义类型
  • 防止大量的由空间溢出引起的安全隐患
  • 隐式的提供内存管理
    后面对这些点进行了解释。

C++基于流的IO之劣势

  • 标准C的printf()往往性能更好
  • 格式限定符比的灵活性更好

Beginning with user-defined types

可以通过重载<<和>>操作符实现对自定义类型的输入输出支持。

#include <string.h>
#include <iostream>
#include <fcntl.h>

class custom_errno
{
   

};

std::ostream &operator << (std::ostream &os, const custom_errno &e)
{
   
    return os << strerror(errno);
}

int main()
{
   
    if (open("do_not_exist.txt", O_RDWR) == -1)
        std::cout << custom_errno{
   } << std::endl; // No such file or directory
}

上例中我们创造了一个空类型并重载了<<操作符对它的操作。这里用它来输出errno(一个指示错误的宏)对应的信息。
同样可以重载<<操作符。

#include <iostream>

struct object_t
{
   
    int data1;
    int data2;
};

std::ostream &operator <<(std::ostream &os, const object_t &obj)
{
   
    os << "data1: " << obj.data1 << '\n';
    os << "data2: " << obj.data2 << '\n'
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hands-On Network Programming with C: Learn socket programming in C and write secure and optimized network code Author: Lewis Van Winkle Pub Date: 2019 ISBN: 978-1789349863 Pages: 478 Language: English A comprehensive guide to programming with network sockets, implementing Internet protocols, designing IoT devices, and much more with C Network programming, a challenging topic in C, is made easy to understand with a careful exposition of socket programming APIs. This book gets you started with modern network programming in C and the right use of relevant operating system APIs. This book covers core concepts, such as hostname resolution with DNS, that are crucial to the functioning of the modern web. You’ll delve into the fundamental network protocols, TCP and UDP. Essential techniques for networking paradigms such as client-server and peer-to-peer models are explained with the help of practical examples. You’ll also study HTTP and HTTPS (the protocols responsible for web pages) from both the client and server perspective. To keep up with current trends, you’ll apply the concepts covered in this book to gain insights into web programming for IoT. You’ll even get to grips with network monitoring and implementing security best practices. By the end of this book, you’ll have experience of working with client-server applications, and be able to implement new network programs in C. The code in this book is compatible with the older C99 version as well as the latest C18 and C++17 standards. Special consideration is given to writing robust, reliable, and secure code that is portable across operating systems, including Winsock sockets for Windows and POSIX sockets for Linux and macOS. What you will learn Uncover cross-platform socket programming APIs Implement techniques for supporting IPv4 and IPv6 Understand how TCP and UDP connections work over IP Discover how hostname resolution and DNS work Interface with web APIs using HTTP and HTTPS Acquire hands-on experience with Simple Mail Transfer Protocol (SMTP) Apply network programming to the Internet of Things (IoT)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值