开发者

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

开发者 https://www.devze.com 2023-04-13 01:10 出处:网络
I want to read the all the contents of a file into a string or some other method that would allow me to compare two files. I am looking for files that are exact matches.

I want to read the all the contents of a file into a string or some other method that would allow me to compare two files. I am looking for files that are exact matches.

Just a little snippet of how I am opening the file and read how many characters it has. which I use to compare to other files to check if it is necessary to compare the whole contents.

Is there a way I can just compare the two buffers?

 char buf[1000];
 string str;
 ssize_t numread; 

    cout<<"Enter in the full file path: ";

    getline(cin,str);

    int f1= open(str.c_str(), 开发者_C百科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);


Use a memory mapped file for this. With UNIX mmap() With Windows MapViewOfFile(). This will give you a pointer in memory that maps to the contents of the file. Why is this a good idea? You don't need to allocate space for the file using malloc() or new(). The files can be any size, ok 32bit limit, but there should be a 64bit version. You can compare the files using

memcmp(file1, file2, sizeoffile1)

Enjoy...

edit - added some code in 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;
}


You could use an istreambuf_iterator, as this example on 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;
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号