Michele De Stefano's C++ Utilities
cfile_sink_src.cpp
1 // cfile_sink_src.cpp
2 //
3 // Copyright (c) 2009 - Michele De Stefano (micdestefano@users.sourceforge.net)
4 //
5 // Distributed under the MIT License (See accompanying file LICENSE)
6 
14 #include <iostream>
16 #include <boost/iostreams/stream_buffer.hpp>
17 
18 namespace bios = boost::iostreams;
19 namespace mds_fu = mds_utils::file_utils;
20 
21 int main(int argc,char *argv[]) {
22 
23  using namespace std;
24 
25  if (argc != 2) {
26  cerr << "\nSyntax:\n\t" << argv[0] << " <file name>\n" << endl;
27  return EXIT_FAILURE;
28  }
29 
30  FILE *fp(NULL);
31 
32  // Open a file with fopen
33  fp = fopen(argv[1],"w");
34 
35  // Instantiate the sink
36  mds_fu::cFile_Sink fsink(fp);
37 
38  // Instantiate the stream_buffer
39  bios::stream_buffer<mds_fu::cFile_Sink> sbuf;
40 
41  // Instantiate an std output stream, connecting it to the buffer
42  ostream ofs(&sbuf);
43 
44  // Connect the buffer to the file sink
45  // (you could do this also before the ofs instantiation)
46  sbuf.open(fsink);
47 
48  // Activate stream exceptions
49  ofs.exceptions(ios::failbit | ios::badbit);
50 
51  double val = 45.6;
52 
53  // Write a binary value
54  ofs.write(reinterpret_cast<char*>(&val),sizeof(double));
55 
56  // Flush the value to the file
57  ofs.flush();
58 
59  // Try to disconnect the buffer from the sink
60  sbuf.close();
61 
62  // Re-connect the buffer to the sink
63  sbuf.open(fsink);
64 
65  // Write something to file
66  ofs << "This is a test string" << endl;
67 
68  // Flush before next operations
69  ofs.flush();
70 
71  // Try the seekp function
72  ofs.seekp(-5,ios::cur);
73 
74  // Overwrite from the current position
75  ofs << "word" << endl;
76 
77  // Close the file
78  fclose(fp); fp = NULL;
79 
80  // Disconnect the buffer
81  sbuf.close();
82 
83  // Re-open the file in append mode
84  fp = fopen(argv[1],"ab");
85 
86  // Re-set the file pointer into the sink
87  fsink.set_FILEp(fp);
88 
89  // Re-connect the buffer to the sink
90  sbuf.open(fsink);
91 
92  // Write something
93  ofs << "Mic" << endl;
94 
95  // Remember to flush
96  ofs.flush();
97 
98  // Try to seek in a file in append mode
99  ofs.seekp(-3,ios::end);
100 
101  ofs << "Des" << std::endl; // It is written at the end, because the file is opened
102  // in append mode
103 
104  // Close the file
105  fclose(fp); fp = NULL;
106 
107  // Disconnect the buffer
108  sbuf.close();
109 
110 
111  // Re-open in read-binary
112  fp = fopen(argv[1],"rb");
113 
114  // Instantiate a source device
115  mds_fu::cFile_Source fsource(fp);
116 
117  // Instantiate a new buffer for the source
118  bios::stream_buffer<mds_fu::cFile_Source> sbuf2;
119 
120  // Connect the buffer to the source device
121  sbuf2.open(fsource);
122 
123  // Instantiate an input stream and connect it to
124  // the prepared buffer
125  istream ifs(&sbuf2);
126 
127  // Activate stream exceptions
128  ifs.exceptions(ios::badbit | ios::failbit);
129 
130  // Try to seek
131  ifs.seekg(-4,ios::end);
132 
133  string str;
134 
135  // Extract a string
136  ifs >> str;
137 
138  cout << "Read string: " << str << endl;
139 
140  // Seek again
141  ifs.seekg(0,ios::beg);
142 
143  // Read a double
144  ifs.read(reinterpret_cast<char*>(&val),sizeof(double));
145 
146  cout << "Value read: " << val << endl;
147 
148  fclose(fp); fp = NULL;
149 
150  return EXIT_SUCCESS;
151 }
Source device for FILE* pointers.
Sink device for FILE* pointers.
Contains some Boost.Iostreams devices to use a C FILE* as a C++ std::stream.
Namespace of all Michele De Stefano&#39;s C++ file utilities.