如何在c++中将文件内容读入字符串
How to read contents of file into string in C++
我想将一个文件的所有内容读入一个字符串或其他一些允许我比较两个文件的方法。我正在寻找精确匹配的文件。
只是我如何打开文件并读取它有多少字符的一小段代码。我用它来与其他文件进行比较,以检查是否有必要比较整个内容。
是否有一种方法可以比较两个缓冲区?
char buf[1000];
string str;
ssize_t numread;
cout<<"Enter in the full file path: ";
getline(cin,str);
int f1= open(str.c_str(), O_RDONLY);
if(f1 ==-1){
cout<<"file did not open"<<'t'<<errno<<str<<endl;
}
int size= tell(f1);
lseek(f1,0,SEEK_SET);
numread= read(f1,buf,size);
if(numread==-1){
cout<<errno<<endl;
}
cout<<'t'<<":"<<str<<" #Chars:"<<numread<<endl;
close(f1);
为此使用内存映射文件。UNIX中的mmap()和Windows中的MapViewOfFile()。这将给你一个内存中的指针,映射到文件的内容。为什么这是个好主意?您不需要使用malloc()或new()为文件分配空间。文件可以是任何大小,ok 32位限制,但应该有一个64位版本。您可以使用
比较这些文件memcmp(file1, file2, sizeoffile1)
享受……
编辑-在C
中添加了一些代码#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
int
cmpfile(char *file1, char *file2)
{
int result = -1;
int fd1, fd2;
off_t size1, size2;
char *ptr1, *ptr2;
fd1 = open(file1, O_RDONLY);
if (fd1 == -1) {
fprintf(stderr, "Failed to open file '%s' - %sn", file1, strerror(errno));
goto error1;
}
fd2 = open(file2, O_RDONLY);
if (fd2 == -1) {
fprintf(stderr, "Failed to open file '%s' - %sn", file2, strerror(errno));
goto error2;
}
size1 = lseek(fd1, 0, SEEK_END);
if (size1 == (off_t)-1) {
fprintf(stderr, "Failed to seek to end of file '%s' - %sn", file1, strerror(errno));
goto error3;
}
size2 = lseek(fd2, 0, SEEK_END);
if (size2 == (off_t)-1) {
fprintf(stderr, "Failed to seek to end of file '%s' - %sn", file2, strerror(errno));
goto error4;
}
if (size1 != size2) {
fprintf(stderr, "File sizes mimatchedn");
goto error5;
}
ptr1 = mmap((void *)0, size1, PROT_READ, MAP_SHARED, fd1, 0);
if (ptr1 == MAP_FAILED) {
fprintf(stderr, "Failed to map file '%s' - %sn", file1, strerror(errno));
goto error6;
}
ptr2 = mmap((void *)0, size2, PROT_READ, MAP_SHARED, fd2, 0);
if (ptr2 == MAP_FAILED) {
fprintf(stderr, "Failed to map file '%s' - %sn", file2, strerror(errno));
goto error7;
}
if (memcmp(ptr1, ptr2, size1) == 0) {
result = 0;
}
munmap(ptr2, size2);
error7:
munmap(ptr1, size1);
error6:
error5:
error4:
error3:
close(fd2);
error2:
close(fd1);
error1:
return result;
}
int main(int argc, char **argv)
{
int result = -1;
if (argc == 3) {
result = cmpfile(argv[1], argv[2]);
if ( result == 0 ) {
printf("Files matchn");
} else {
printf("Files don't matchn");
}
} else {
fprintf(stderr, "Usage: %s <file1> <file2>n", argv[0]);
}
return result;
}
您可以使用istreambuf_iterator
,如cplusplus.com上的示例。
#include <iostream>
#include <iterator>
#include <string>
#include <stdexcept>
#include <fstream>
int main (int ac, char **av) {
if(ac != 3)
throw(std::runtime_error("Usage: progname file1 file2"));
if(std::equal(
std::istreambuf_iterator<char>(
std::ifstream(av[1], std::ios::binary).rdbuf()),
std::istreambuf_iterator<char>(),
std::istreambuf_iterator<char>(
std::ifstream(av[2], std::ios::binary).rdbuf()))) {
std::cout << "samen";
return 0;
} else {
std::cout << "differentn";
return 1;
}
}
相关文章:
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 有没有办法从非C/C++文件中读取C++原始字符串文字的内容
- 如何打开并写入一个名称取自C++中字符串的文件
- 如何从二进制文件中读取字符串
- 如何在 c++ 中使用 getline 从文件中读取字符串?
- 将字符串与文件上的数据进行比较
- 用户输入字符串的文件附加问题..C++
- 如何从文件中读取两个字符串和数字数组,并将它们存储在对象向量中
- 如何使字符串出现在编译的二进制可执行文件的开头?
- 使用文件内容作为硬编码字符串
- 将文件复制到自定义位置,存在字符串转换问题
- 将二进制字符串/文件内容从 c++ 传递到节点 js
- 在字符串.h文件中调用函数而不带对象
- 将带有引号和逗号的字符串文件读入字符串数组
- 如何从字符串/文件中分离字符和整数
- 将字符串文件存储到动态数组中
- 如何用C++将字符串文件Txt解析成数组
- 如何用Boost::Spirit解析复杂的字符串文件
- 如何从C语言的第二行读取字符串文件
- 一个很好的性能替代PHP-字符串/文件操作