gtkmm-documentation r40 - in trunk: . examples/book/giomm examples/book/giomm/write_file
- From: murrayc svn gnome org
- To: svn-commits-list gnome org
- Subject: gtkmm-documentation r40 - in trunk: . examples/book/giomm examples/book/giomm/write_file
- Date: Fri, 28 Mar 2008 16:29:28 +0000 (GMT)
Author: murrayc
Date: Fri Mar 28 16:29:28 2008
New Revision: 40
URL: http://svn.gnome.org/viewvc/gtkmm-documentation?rev=40&view=rev
Log:
2008-03-28 Murray Cumming <murrayc murrayc com>
* configure.in:
* examples/book/giomm/Makefile.am:
* examples/book/giomm/write_file/: Added this example, showing how to
create files and how to replace their contents using a stream.
Added:
trunk/examples/book/giomm/write_file/
- copied from r39, /trunk/examples/book/giomm/read_file/
Modified:
trunk/ChangeLog
trunk/configure.in
trunk/examples/book/giomm/Makefile.am
trunk/examples/book/giomm/write_file/main.cc
Modified: trunk/configure.in
==============================================================================
--- trunk/configure.in (original)
+++ trunk/configure.in Fri Mar 28 16:29:28 2008
@@ -314,6 +314,7 @@
examples/book/giomm/getline/Makefile
examples/book/giomm/monitor_directory/Makefile
examples/book/giomm/usage/Makefile
+ examples/book/giomm/write_file/Makefile
examples/book/frame/Makefile
examples/book/helloworld/Makefile
examples/book/helloworld2/Makefile
Modified: trunk/examples/book/giomm/Makefile.am
==============================================================================
--- trunk/examples/book/giomm/Makefile.am (original)
+++ trunk/examples/book/giomm/Makefile.am Fri Mar 28 16:29:28 2008
@@ -1 +1 @@
-SUBDIRS = directory_list read_file read_file_async volumes getline monitor_directory usage
+SUBDIRS = directory_list read_file read_file_async volumes getline monitor_directory usage write_file
Modified: trunk/examples/book/giomm/write_file/main.cc
==============================================================================
--- /trunk/examples/book/giomm/read_file/main.cc (original)
+++ trunk/examples/book/giomm/write_file/main.cc Fri Mar 28 16:29:28 2008
@@ -26,30 +26,43 @@
try
{
- Glib::RefPtr<Gio::File> file = Gio::File::create_for_path("/etc/fstab");
+ Glib::RefPtr<Gio::File> file = Gio::File::create_for_path("test_output.txt");
if(!file)
std::cerr << "Gio::File::create_for_path() returned an empty RefPtr." << std::endl;
- Glib::RefPtr<Gio::FileInputStream> stream = file->read();
+ Glib::RefPtr<Gio::FileOutputStream> stream;
+
+ //If the file exists already then replace it.
+ //Otherwise, create it:
+ if(file->query_exists())
+ stream = file->replace();
+ else
+ stream = file->create_file();
+
if(!stream)
- std::cerr << "Gio::File::read() returned an empty RefPtr." << std::endl;
+ std::cerr << "Gio::File::create_file() returned an empty RefPtr." << std::endl;
- gchar buffer[1000]; //TODO: This is unpleasant.
- memset(buffer, 0, 1000);
- const gsize bytes_read = stream->read(buffer, 1000);
+ Glib::ustring contents = "This is some test output";
+ const gsize bytes_read = stream->write(contents);
if(bytes_read)
- std::cout << "File contents read: " << buffer << std::endl;
+ std::cout << "File contents written: " << contents << std::endl;
else
- std::cerr << "Gio::InputStream::read() read 0 bytes." << std::endl;
+ std::cerr << "Gio::InputStream::write() wrote 0 bytes." << std::endl;
+ //Close the stream to make sure that changes are written now,
+ //instead of just when the stream goes out of scope,
+ //though that's the same time in this simple example.
+ //For instance, when using Gio::File::replace(), the file is only
+ //actually replaced during close() or when the stream is destroyed.
+ stream->close();
+ stream.reset(); //Stream can't be used after we have closed it.
}
catch(const Glib::Exception& ex)
{
std::cerr << "Exception caught: " << ex.what() << std::endl;
}
-
return 0;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]