使用运算符读取文件和使用读取功能>>文件有什么区别?

What is the difference between reading a file with >> operator and reading a file with read function?

本文关键字:gt 文件 读取 什么 区别 功能 运算符      更新时间:2023-10-16
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    ifstream obj;
    obj.open("a.txt");
    char i;
    obj.read((char *)&i, 1);
    cout << i;
    obj.close();
    return 0;
}

使用>> 运算符读取文件和在 c++ 中使用读取函数读取文件有什么区别?

read() 函数读取给定数量的字符,而operator>>()读取格式和数据解释。

例如:

char buf[11];
cin.read(buf, 10);
buf[10] = 0;
int a;
cin >> a;

对于给定的输入12345678901234567890,结果为

strcmp(buf, "1234567890") == 0
a == 1234567890