Подтвердить что ты не робот

Прочитайте содержимое файла в строке в С++

Возможный дубликат:
Каков наилучший способ разбить файл на std::string на С++?

В языках сценариев, таких как Perl, можно прочитать файл в переменной за один снимок.

    open(FILEHANDLE,$file);
    $content=<FILEHANDLE>;

Что было бы самым эффективным способом сделать это на С++?

4b9b3361

Ответ 1

Вот так:

#include <fstream>
#include <string>

int main(int argc, char** argv)
{

  std::ifstream ifs("myfile.txt");
  std::string content( (std::istreambuf_iterator<char>(ifs) ),
                       (std::istreambuf_iterator<char>()    ) );

  return 0;
}

Утверждение

  std::string content( (std::istreambuf_iterator<char>(ifs) ),
                       (std::istreambuf_iterator<char>()    ) );

можно разбить на

std::string content;
content.assign( (std::istreambuf_iterator<char>(ifs) ),
                (std::istreambuf_iterator<char>()    ) );

что полезно, если вы хотите просто перезаписать значение существующей переменной std::string.

Ответ 2

Самый эффективный, но не С++-способ:

   FILE* f = fopen(filename, "r");

   // Determine file size
   fseek(f, 0, SEEK_END);
   size_t size = ftell(f);

   char* where = new char[size];

   rewind(f);
   fread(where, sizeof(char), size, f);

   delete[] where;

# EDIT - 2

Просто протестирован вариант std::filebuf. Похоже, его можно назвать лучшим С++-подходом, хотя это не совсем подход на С++, а скорее оболочка. Во всяком случае, вот кусок кода, который работает почти так же быстро, как и обычный C.

   std::ifstream file(filename, std::ios::binary);
   std::streambuf* raw_buffer = file.rdbuf();

   char* block = new char[size];
   raw_buffer->sgetn(block, size);
   delete[] block;

Я сделал быстрый тест здесь, и результаты следуют. Тест проводился при чтении двоичного файла 65536K с соответствующими режимами (std::ios:binary и rb).

[==========] Running 3 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 4 tests from IO
[ RUN      ] IO.C_Kotti
[       OK ] IO.C_Kotti (78 ms)
[ RUN      ] IO.CPP_Nikko
[       OK ] IO.CPP_Nikko (106 ms)
[ RUN      ] IO.CPP_Beckmann
[       OK ] IO.CPP_Beckmann (1891 ms)
[ RUN      ] IO.CPP_Neil
[       OK ] IO.CPP_Neil (234 ms)
[----------] 4 tests from IO (2309 ms total)

[----------] Global test environment tear-down
[==========] 4 tests from 1 test case ran. (2309 ms total)
[  PASSED  ] 4 tests.

Ответ 3

Наиболее эффективным является создание буфера правильного размера, а затем чтение файла в буфер.

#include <fstream>
#include <vector>

int main()
{
    std::ifstream       file("Plop");
    if (file)
    {
        /*
         * Get the size of the file
         */
        file.seekg(0,std::ios::end);
        std::streampos          length = file.tellg();
        file.seekg(0,std::ios::beg);

        /*
         * Use a vector as the buffer.
         * It is exception safe and will be tidied up correctly.
         * This constructor creates a buffer of the correct length.
         * Because char is a POD data type it is not initialized.
         *
         * Then read the whole file into the buffer.
         */
        std::vector<char>       buffer(length);
        file.read(&buffer[0],length);
    }
}

Ответ 4

В текстовых файлах не должно быть \0.

#include<iostream>
#include<fstream>

using namespace std;

int main(){
  fstream f(FILENAME, fstream::in );
  string s;
  getline( f, s, '\0');

  cout << s << endl;
  f.close();
}

Ответ 5

Это зависит от многих вещей, таких как размер файла, каков его тип (текстовый/двоичный) и т.д. Некоторое время назад я сравнивал следующую функцию с версиями с помощью итераторов streambuf - это было примерно в два раза как быстро:

unsigned int FileRead( std::istream & is, std::vector <char> & buff ) {
    is.read( &buff[0], buff.size() );
    return is.gcount();
}

void FileRead( std::ifstream & ifs, string & s ) {
    const unsigned int BUFSIZE = 64 * 1024; // reasoable sized buffer
    std::vector <char> buffer( BUFSIZE );

    while( unsigned int n = FileRead( ifs, buffer ) ) {
        s.append( &buffer[0], n );
    }
}

Ответ 6

возможно, не самый эффективный, но считывает данные в одной строке:

#include<iostream>
#include<vector>
#include<iterator>

main(int argc,char *argv[]){
  // read standard input into vector:
  std::vector<char>v(std::istream_iterator<char>(std::cin),
                     std::istream_iterator<char>());
  std::cout << "read " << v.size() << "chars\n";
}

Ответ 7

Здесь используется метод, основанный на итераторе.

ifstream file("file", ios::binary);
string fileStr;

istreambuf_iterator<char> inputIt(file), emptyInputIt
back_insert_iterator<string> stringInsert(fileStr);

copy(inputIt, emptyInputIt, stringInsert);