gpt4 book ai didi

c++ - 如何从文本文件中读取特定数量的字符

转载 作者:可可西里 更新时间:2023-11-01 18:26:46 27 4
gpt4 key购买 nike

我试过这样做

 #include <iostream>
#include <fstream>

using namespace std;

int main()
{
char b[2];
ifstream f("prad.txt");
f>>b ;
cout <<b;
return 0;
}

它应该读取 2 个字符,但它读取了整行。这适用于另一种语言,但由于某种原因不适用于 C++。

最佳答案

您可以使用 read()指定要读取的字符数:

char b[3] = "";
ifstream f("prad.txt");

f.read(b, sizeof(b) - 1); // Read one less that sizeof(b) to ensure null
cout << b; // terminated for use with cout.

关于c++ - 如何从文本文件中读取特定数量的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9518659/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com