如何在 c++ 中将整个文本文件复制到 char 数组中
How to copy a whole text file into a char array in c++
int main()
{
char ch;
char text[500];
char s[500];
ifstream fin("bitch.txt",ios::in);
while(!fin.eof())
{
fin.getline(s,500);
}
fin.close();
for (int i=0; i<500; i++)
{
cout << s[i];
}
return 0;
}
如何在 c++ 中将整个文本文件内容复制到 char 数组中考虑到文本文件包含 100 个字符长的段落我也需要阅读空格.
就这样做
#include <string>
#include <fstream>
#include <streambuf>
std::ifstream file("file.txt");
std::string str((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
然后
str.c_str();
是您查找的数组。
如果要
将文件的全部内容复制到char数组中,则无需逐行执行此操作。您可以复制文件的全部内容
您可以使用以下命令将整个文件读入字符串
std::ifstream in("FileReadExample.cpp");
std::string contents((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());
然后你可以使用 contents.c_str() 来获取字符数组
看看下面的链接。 他给出了正确的答案如何在 c++ 中将.txt文件复制到字符数组
相关文章:
- 复制文件流C++静态变量
- 如何递归复制文件和目录
- 复制文件的最佳方式是什么,以便我可以在复制过程中轻松取消复制?
- 复制文件成功,移动文件失败,参数相同 - C++
- 从 QProcess 复制文件夹的内容
- 以非常低的资源要求强制复制文件
- binding.gyp:如何使用"copies"部分在多个位置复制文件
- 如何在 Visual Studio 2015 中的构建结束时复制文件
- 自复制文件到新位置tchar不兼容
- 当我使用fs.rdbuf()复制文件时,我会丢失一些数据
- C++如何复制文件 * 指针
- 复制文件时是否可以自动创建不存在的目录?
- 复制文件时如何调用取消?(使用复制文件 ex 函数和复制进度例程回调函数)
- Linux复制文件连接到节点JS / Python / C / C 之后
- 使用C++在 Linux 中复制文件的有效方法
- 我可以在锁定文件后复制文件吗?
- 在线程中复制文件以防止冻结应用程序
- 如何使用libssh和SFTP在C/C++中复制文件
- 如何复制文件的二进制数据
- 使用 IFileOperation 复制文件 (C++)