如何使用低级系统调用从 stdin 和输入文件中获取字节数
How to get number of bytes from stdin and input file using low level system calls
如何使用低级系统调用从 stdin 和输入文件中获取字节数? 如 read(2) 和 write(2)
我使用 lseek 从输入文件中获取字节数,但 lseek 不适用于标准输入。
我想尝试逐字节读取文件或标准输入字节,并将它们存储到数组中并打印出文件或标准输入中的总字节数。我尝试使用 for 循环来做到这一点。 就像标准输入一样...
while((x = read(0, bf, bsize)) > 0) //reading the standard input
{
for(int i = 0; i < n; i++)
{
//try to implement getting the total amount of bytes that are in STDIN here
}
}
这就是我试图做的,但我认为我使用 for 循环做错了。
我真的不知道如何实现获取标准输入和输入文件中的字节数。有人可以帮我吗?
您无法从stdin
/std::cin
获取字节数,因为您无法转到这些流中的特定位置。此外,您无法倒带它们。
最好的选择是在读取字节时将字节存储在std::vector
中。
std::vector<char> arr;
int c;
while ( (c = std::cin.get()) != EOF )
{
arr.push_back(c);
}
size_t size = arr.size();
相关文章:
- C++文本文件输入
- C++ 中文件输入输出中的长长 v/s int
- 如何修复函数中的 fstream 文件输入以将正确的信息存储在结构数组中?
- 用空格填充 2D 数组(从文件输入)
- 流对象来表示文件输入,然后是标准输入?
- 用精神解析文件输入
- 从文件输入和输出
- 来自文本文件输入 c++ 的向量
- 如何通过键盘或分数文件输入分数
- 了解文件输入和输出,我可以写入文件,但无法从文件打印
- 词频程序 - 文件输入太大?
- 尝试从 csv 文件输入大量数据时出错
- 如何将文件从文件输入到C 中的矩阵中
- 如何从Windows CMD运行可执行文件并将其传递给文件输入的参数参数和重定向
- 如何解析文本文件并使用构造函数中的文件输入来创建对象的容器
- 这是读取文件输入的正确方法吗?
- 如何在 c++ 中使用二进制文件输入/输出读取/写入结构的字符串类型成员
- 如何创建一个具有空值、递减编号系统和.txt文件输入值的二维阵列网格
- C 文件输入流循环问题
- 使用对输入整数的文件输入