如何在c++中将文件内容读入字符串

How to read contents of file into string in C++

本文关键字:字符串 文件 c++      更新时间:2023-10-16

我想将一个文件的所有内容读入一个字符串或其他一些允许我比较两个文件的方法。我正在寻找精确匹配的文件。

只是我如何打开文件并读取它有多少字符的一小段代码。我用它来与其他文件进行比较,以检查是否有必要比较整个内容。

是否有一种方法可以比较两个缓冲区?

 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;
  }
}