首页 文章

使用>> operator读取文件和读取具有读取功能的文件有什么区别?

提问于
浏览
1
#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中的读取函数有什么区别?

1 回答

  • 3

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

    例如:

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

    使用给定的输入 12345678901234567890 ,结果是

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

相关问题