Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- int main()
- {
- // zapis pieciu liczb double 3.14, 4.14, 5.14 ...
- try
- {
- std::ofstream file("out.txt", std::ios::binary);
- file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
- for(int i = 0; i < 5; i++)
- {
- double pi = 3.14;
- pi += i;
- file.write( reinterpret_cast<char*>( &pi ), sizeof(pi));
- }
- }
- catch(std::ios_base::failure &fail)
- {
- std::cout << "Nie mozna zapisac pliku!" << std::endl
- << fail.what() << std::endl;
- }
- double x;
- // odczyt tego samego pliku
- try
- {
- std::ifstream in("out.txt");
- in.exceptions(std::ifstream::failbit | std::ifstream::badbit);
- while(!in.eof())
- {
- in.read(reinterpret_cast<char*>(&x), sizeof(x));
- std::cout << x << std::endl;
- }
- }
- catch(std::ios_base::failure &fail)
- {
- // handle exception here
- std::cout << "Caught an exception: " << fail.what() << std::endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement