Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
fstream.cpp
Go to the documentation of this file.
1
29#include <fstream> // for fstream
30#include <iostream> // for cout
31#include <string> // for string
32
33#include <examples.h>
34
35int main ( )
36{
37 // create a bi-directional fstream object
38 std::fstream inout ("test_fstream.out",
39 std::ios::in | std::ios::out | std::ios::trunc);
40
41 // write out three lines to the file
42 inout << "Dieses ist die Geschichte eines Mannes.\n"
43 << "C'est l'histoire d'un homme.\n"
44 << "This is the story of a man.\n";
45
46 std::string line;
47
48 // seek to the beginning of the file
49 inout.seekg (0);
50
51 // extract the first line
52 std::getline (inout, line);
53
54 // output the first line to standard output
55 std::cout << "\nDeutsch:\n" << line << '\n';
56
57 // obtain current position in file
58 const std::fstream::pos_type pos = inout.tellg ();
59
60 // extract the second line
61 std::getline (inout, line);
62
63 // output the second line to standard output
64 std::cout << "Francais:\n" << line << '\n';
65
66 // extract the third line
67 std::getline (inout, line);
68
69 // output the third line to standard output
70 std::cout << "English:\n" << line << '\n';
71
72 // move the put sequence before the second line
73 inout.seekp (pos);
74
75 // replace the second and third lines
76 inout << "This is the story of a man.\n"
77 << "C'est l'histoire d'un homme.";
78
79 // seek to the beginning of the file
80 inout.seekg (0);
81
82 // output the entire contents of the fstream object's buffer to stdout
83 std::cout << "\n" << inout.rdbuf () << '\n';
84
85 return 0;
86}
#define line
Definition 17.names.cpp:490
#define pos
Definition 17.names.cpp:518
int main()
Definition fstream.cpp:35