C++ 如何在C+中将文件内容读入字符串+;
我想把一个文件的所有内容读入一个字符串或其他方法中,以便比较两个文件。我正在寻找完全匹配的文件 只是我如何打开文件并读取文件中有多少字符的一小部分。我使用它与其他文件进行比较,以检查是否有必要比较整个内容 有没有办法比较两个缓冲区C++ 如何在C+中将文件内容读入字符串+;,c++,file,compare,buffer,C++,File,Compare,Buffer,我想把一个文件的所有内容读入一个字符串或其他方法中,以便比较两个文件。我正在寻找完全匹配的文件 只是我如何打开文件并读取文件中有多少字符的一小部分。我使用它与其他文件进行比较,以检查是否有必要比较整个内容 有没有办法比较两个缓冲区 char buf[1000]; string str; ssize_t numread; cout<<"Enter in the full file path: "; getline(cin,str); int f1=
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);
charbuf[1000];
字符串str;
西泽努姆雷德;
不能为此使用内存映射文件。
使用UNIX mmap()和Windows MapViewOfFile()。
这将在内存中为您提供一个指向文件内容的指针。
为什么这是个好主意?
您不需要使用malloc()或new()为文件分配空间。
文件可以是任意大小,可以是32位限制,但应该有64位版本。
您可以使用
memcmp(文件1、文件2、大小办公室1)
享受
编辑-在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' - %s\n", file1, strerror(errno));
goto error1;
}
fd2 = open(file2, O_RDONLY);
if (fd2 == -1) {
fprintf(stderr, "Failed to open file '%s' - %s\n", 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' - %s\n", 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' - %s\n", file2, strerror(errno));
goto error4;
}
if (size1 != size2) {
fprintf(stderr, "File sizes mimatched\n");
goto error5;
}
ptr1 = mmap((void *)0, size1, PROT_READ, MAP_SHARED, fd1, 0);
if (ptr1 == MAP_FAILED) {
fprintf(stderr, "Failed to map file '%s' - %s\n", 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' - %s\n", 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 match\n");
} else {
printf("Files don't match\n");
}
} else {
fprintf(stderr, "Usage: %s <file1> <file2>\n", argv[0]);
}
return result;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int
cmpfile(char*file1,char*file2)
{
int结果=-1;
int-fd1,fd2;
尺寸1、尺寸2;
字符*ptr1,*ptr2;
fd1=打开(仅限文件1);
如果(fd1==-1){
fprintf(stderr,“无法打开文件“%s”-%s\n”,文件1,strerror(errno));
转到错误1;
}
fd2=打开(仅限文件2);
如果(fd2==-1){
fprintf(stderr,“无法打开文件“%s”-%s\n”,文件2,strerror(errno));
转到错误2;
}
size1=lseek(fd1,0,搜索结束);
如果(size1==(off\u t)-1){
fprintf(stderr,“未能查找到文件“%s”的结尾-%s\n”,文件1,strerror(errno));
转到错误3;
}
size2=lseek(fd2,0,搜索结束);
如果(size2==(off\u t)-1){
fprintf(stderr,“未能查找到文件“%s”的结尾-%s\n”,文件2,strerror(errno));
转到错误4;
}
如果(尺寸1!=尺寸2){
fprintf(stderr,“文件大小匹配”);
转到错误5;
}
ptr1=mmap((void*)0,大小1,保护读取,映射共享,fd1,0);
如果(ptr1==MAP_失败){
fprintf(stderr,“未能映射文件“%s”-%s\n”,文件1,strerror(errno));
转到错误6;
}
ptr2=mmap((void*)0,大小2,保护读取,映射共享,fd2,0);
如果(ptr2==MAP_失败){
fprintf(stderr,“未能映射文件“%s”-%s\n”,文件2,strerror(errno));
转到错误7;
}
if(memcmp(ptr1、ptr2、size1)==0){
结果=0;
}
munmap(ptr2,尺寸2);
错误7:
munmap(ptr1,尺寸1);
错误6:
错误5:
错误4:
错误3:
关闭(fd2);
错误2:
关闭(fd1);
错误1:
返回结果;
}
int main(int argc,字符**argv)
{
int结果=-1;
如果(argc==3){
结果=cmpfile(argv[1],argv[2]);
如果(结果==0){
printf(“文件匹配\n”);
}否则{
printf(“文件不匹配\n”);
}
}否则{
fprintf(stderr,“用法:%s\n”,argv[0]);
}
返回结果;
}
为此使用内存映射文件。
使用UNIX mmap()和Windows MapViewOfFile()。
这将在内存中为您提供一个指向文件内容的指针。
为什么这是个好主意?
您不需要使用malloc()或new()为文件分配空间。
文件可以是任意大小,可以是32位限制,但应该有64位版本。
您可以使用
memcmp(文件1、文件2、大小办公室1)
享受
编辑-在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' - %s\n", file1, strerror(errno));
goto error1;
}
fd2 = open(file2, O_RDONLY);
if (fd2 == -1) {
fprintf(stderr, "Failed to open file '%s' - %s\n", 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' - %s\n", 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' - %s\n", file2, strerror(errno));
goto error4;
}
if (size1 != size2) {
fprintf(stderr, "File sizes mimatched\n");
goto error5;
}
ptr1 = mmap((void *)0, size1, PROT_READ, MAP_SHARED, fd1, 0);
if (ptr1 == MAP_FAILED) {
fprintf(stderr, "Failed to map file '%s' - %s\n", 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' - %s\n", 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 match\n");
} else {
printf("Files don't match\n");
}
} else {
fprintf(stderr, "Usage: %s <file1> <file2>\n", argv[0]);
}
return result;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int
cmpfile(char*file1,char*file2)
{
int结果=-1;
int-fd1,fd2;
尺寸1、尺寸2;
字符*ptr1,*ptr2;
fd1=打开(仅限文件1);
如果(fd1==-1){
fprintf(stderr,“无法打开文件“%s”-%s\n”,文件1,strerror(errno));
转到错误1;
}
fd2=打开(仅限文件2);
如果(fd2==-1){
fprintf(stderr,“无法打开文件“%s”-%s\n”,文件2,strerror(errno));
转到错误2;
}
size1=lseek(fd1,0,搜索结束);
如果(size1==(off\u t)-1){
fprintf(stderr,“未能查找到文件“%s”的结尾-%s\n”,文件1,strerror(errno));
转到错误3;
}
size2=lseek(fd2,0,搜索结束);
如果(size2==(off\u t)-1){
fprintf(stderr,“未能查找到文件“%s”的结尾-%s\n”,文件2,strerror(errno));
转到错误4;
}
如果(尺寸1!=尺寸2){
fprintf(stderr,“文件大小匹配”);
转到错误5;
}
ptr1=mmap((void*)0,大小1,保护读取,映射共享,fd1,0);
如果(ptr1==MAP_失败){
fprintf(stderr,“未能映射文件“%s”-%s\n”,文件1,strerror(errno));
转到错误6;
}
ptr2=mmap((void*)0,大小2,保护读取,映射共享,fd2,0);
如果(ptr2==MAP_失败){
fprintf(stderr,“未能映射文件“%s”-%s\n”,文件2,strerror(errno));
转到错误7;
}
if(memcmp(ptr1、ptr2、size1)==0){
结果=0;
}
munmap(ptr2,尺寸2);
错误7:
munmap(ptr1,尺寸1);
错误6:
错误5:
错误4:
错误3:
关闭(fd2);
错误2:
关闭(fd1);
错误1:
返回结果;
}
int main(int argc,字符**argv)
{
int结果=-1;
如果(argc==3){
结果=cmpfile(argv[1],argv[2]);
如果(结果==0){
printf(“文件匹配\n”);
}否则{
printf(“文件不匹配\n”);
}
}否则{
fprintf(stderr,“用法:%s\n”,argv[0]);
}
返回结果;
}
您可以像在cplusplus.com上一样使用istreambuf\u迭代器
#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 << "same\n";
return 0;
} else {
std::cout << "different\n";
return 1;
}
}
#包括
#包括
#包括
#包括
#包括
内部主(内部ac,字符**av){
如果(ac!=3)
抛出(std::runtime_错误(“用法:progname file1 file2”);
如果(标准::相等(
std::istreambuf_迭代器(
std::ifstream(av[1],std::ios::binary).rdbuf()),
std::istreambuf_迭代器(),
std::istreambuf_迭代器(
std::ifstream(av[2],std::ios::binary).rdbuf()){
std::cout您可以使用一个istreambuf_迭代器,就像在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 << "same\n";
return 0;
} else {
std::cout << "different\n";
return 1;
}
}
#包括
#包括
#包括
#包括
#包括
内部主(内部ac,字符**av){
如果(ac!=3)
抛出(std::runtime_错误(“用法:progname file1 file2”);
如果(标准::相等(
std::istreambuf_迭代器(
标准::ifstream(av[1],标准::i