Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何从文件中读取字节?C++;_C++_File - Fatal编程技术网

C++ 如何从文件中读取字节?C++;

C++ 如何从文件中读取字节?C++;,c++,file,C++,File,在我的桌面上有.txt文件。我需要从内存到数组读取所有字节 我尝试从文件到字符串读取文本,然后使用memcpy()从字符串读取字节,但我认为这是不正确的 Tnx ifstream文件(“C:\\Users\\Flone\\Desktop\\ass.txt”); 字符串文件\文本; //开始读取文本文件(查看下面的结束): 字符字缓冲区[30]; 对于(int i=0;i>word\u缓冲区; 对于(int i=0;i。阅读istream::Read函数。“我试过了”。。。让我们展示一下这个代码—

在我的桌面上有.txt文件。我需要从内存到数组读取所有字节

我尝试从文件到字符串读取文本,然后使用memcpy()从字符串读取字节,但我认为这是不正确的

Tnx

ifstream文件(“C:\\Users\\Flone\\Desktop\\ass.txt”);
字符串文件\文本;
//开始读取文本文件(查看下面的结束):
字符字缓冲区[30];
对于(int i=0;i<30;i++)
{
字缓冲区[i]=NULL;
}
while(File.eof()==false)
{
文件>>word\u缓冲区;
对于(int i=0;i<30;i++)
{
if(字缓冲区[i]!=NULL)
{
文件\文本+=字\缓冲区[i];
}
}
如果(File.eof()==false)文件文本+=“”;
对于(int i=0;i<30;i++)
{
字缓冲区[i]=NULL;
}
}
File.close();
//结束读取文本文件。

cout使用向量的小示例

#include <fstream>
#include <iterator>
#include <vector>
#包括
#包括
#包括
这会将文件中的字节读入向量

    std::ifstream input("d:\\testinput.txt", std::ios::binary);

    std::vector<char> bytes(
         (std::istreambuf_iterator<char>(input)),
         (std::istreambuf_iterator<char>()));

    input.close();
std::ifstream输入(“d:\\testinput.txt”,std::ios::binary);
向量字节(
(std::istreambuf_迭代器(输入)),
(std::istreambuf_迭代器());
input.close();

使用向量的小示例

#include <fstream>
#include <iterator>
#include <vector>
#包括
#包括
#包括
这会将文件中的字节读入向量

    std::ifstream input("d:\\testinput.txt", std::ios::binary);

    std::vector<char> bytes(
         (std::istreambuf_iterator<char>(input)),
         (std::istreambuf_iterator<char>()));

    input.close();
std::ifstream输入(“d:\\testinput.txt”,std::ios::binary);
向量字节(
(std::istreambuf_迭代器(输入)),
(std::istreambuf_迭代器());
input.close();

“我试过了”。。。让我们展示一下这个代码——当你尝试的时候,哪里出了问题@hlt I添加了代码。请参阅不要对二进制文件使用
运算符>>
。阅读
istream::Read
函数。“我试过了”。。。让我们展示一下这个代码——当你尝试的时候,哪里出了问题@hlt I添加了代码。请参阅不要对二进制文件使用
运算符>>
。阅读
istream::Read
函数。为什么需要将迭代器括在括号中?我的代码在没有包含它们时不会编译,但我不知道为什么。为什么需要将迭代器括在括号中?我的代码在未包含它们时不会编译,但我不确定原因。