Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CPlusPlusNotesForProfessionals.pdf
Скачиваний:
47
Добавлен:
20.05.2023
Размер:
5.11 Mб
Скачать

{

//Store data here std::string data;

//Convert object to string

operator std::string const&() const {return data;} // Read a line from a stream.

friend std::istream& operator>>(std::istream& stream, Line& line)

{

return std::getline(stream, line.data);

}

};

std::ifstream file("file3.txt");

// Read the lines of a file into a container. std::vector<std::string> v(std::istream_iterator<Line>{file},

std::istream_iterator<Line>{});

Section 12.10: Copying a file

std::ifstream src("source_filename", std::ios::binary); std::ofstream dst("dest_filename", std::ios::binary); dst << src.rdbuf();

Version ≥ C++17

With C++17 the standard way to copy a file is including the <filesystem> header and using copy_file:

std::fileystem::copy_file("source_filename", "dest_filename");

The filesystem library was originally developed as boost.filesystem and finally merged to ISO C++ as of C++17.

Section 12.11: Closing a file

Explicitly closing a file is rarely necessary in C++, as a file stream will automatically close its associated file in its destructor. However, you should try to limit the lifetime of a file stream object, so that it does not keep the file handle open longer than necessary. For example, this can be done by putting all file operations into an own scope ({}):

std::string const prepared_data = prepare_data();

{

// Open a file for writing. std::ofstream output("foo.txt");

// Write data.

output << prepared_data;

}// The ofstream will go out of scope here.

// Its destructor will take care of closing the file properly.

Calling close() explicitly is only necessary if you want to reuse the same fstream object later, but don't want to keep the file open in between:

//Open the file "foo.txt" for the first time. std::ofstream output("foo.txt");

//Get some data to write from somewhere.

std::string const prepared_data = prepare_data();

GoalKicker.com – C++ Notes for Professionals

62

//Write data to the file "foo.txt". output << prepared_data;

//Close the file "foo.txt". output.close();

//Preparing data might take a long time. Therefore, we don't open the output file stream

//before we actually can write some data to it.

std::string const more_prepared_data = prepare_complex_data();

//Open the file "foo.txt" for the second time once we are ready for writing. output.open("foo.txt");

//Write the data to the file "foo.txt".

output << more_prepared_data;

// Close the file "foo.txt" once again. output.close();

Section 12.12: Reading a `struct` from a formatted text file

Version ≥ C++11

struct info_type

{

std::string name; int age;

float height;

// we define an overload of operator>> as a friend function which // gives in privileged access to private data members

friend std::istream& operator>>(std::istream& is, info_type& info)

{

// skip whitespace is >> std::ws;

std::getline(is, info.name); is >> info.age;

is >> info.height; return is;

}

};

void func4()

{

auto file = std::ifstream("file4.txt"); std::vector<info_type> v;

for(info_type info; file >> info;) // keep reading until we run out

{

// we only get here if the read succeeded v.push_back(info);

}

for(auto const& info: v)

{

std::cout << " name: " << info.name << '\n';

std::cout << " age: " << info.age << " years" << '\n'; std::cout << "height: " << info.height << "lbs" << '\n'; std::cout << '\n';

}

}

GoalKicker.com – C++ Notes for Professionals

63

file4.txt

Wogger Wabbit 2 6.2

Bilbo Baggins 111 81.3

Mary Poppins 29 154.8

Output:

name: Wogger Wabbit age: 2 years

height: 6.2lbs

name: Bilbo Baggins age: 111 years

height: 81.3lbs

name: Mary Poppins age: 29 years

height: 154.8lbs

GoalKicker.com – C++ Notes for Professionals

64