GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Aug 23, 2014 5:32 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: GIOChannel not working
PostPosted: Thu Aug 02, 2012 6:25 pm 
Offline
GTK+ Guru

Joined: Fri Mar 25, 2011 5:16 pm
Posts: 177
Location: USA
I'm trying a tutorial on glib which uses GIOChannel. I'm using Ubuntu 11.04 with glib-2.30.2 (gtk+-3.2.3). The program is supposed to do the following:

1) Channels are opened using g_io_channel_new_file().
2) The contents of the file are read using g_io_channel_read_chars()
3) The contents are written to a new file using g_io_channel_write_chars().

I created two text files in the same folder: test1.txt and test2.txt
test2.txt is empty. test1.txt contains the following text: text here
When the program runs I receive:
Code:
<cp SOURCE> <DESTINATION>
Press any key to exit

I entered the following then pressed enter:
Code:
cp test1.txt test2.txt

I'm just returned to the prompt and the contents of my source test file (test1.txt) is not copied to the destination (test2.txt). I'm not receiving any error or warning messages. Strangely, if I type in a text file that doesn't exist there are still no errors!
Please Help, why isn't this working?

Source: http://library.developer.nokia.com/index.jsp?topic=/GUID-E35887BB-7E58-438C-AA27-97B2CDE7E069/GUID-817C43E8-9169-4750-818B-B431D138D71A.html
Code:
/*compiled using:   
*    gcc -Wall -o IO1 `pkg-config --cflags --libs gtk+-3.0` IO1.c
*/

#include <glib.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
   GIOChannel *in_channel, *out_channel;
   gchar buf[100];
   gsize bytes_read,bytes_written;
   GError *error = NULL;
   
   if(argc != 3)
   {
      g_print("usage:<cp SOURCE> <DESTINATION>\n");
      g_print("Press any key to exit\n");
      getchar();
      return 1;
   }
      
   in_channel = g_io_channel_new_file(argv[1], "r", &error);
   
   if(!in_channel)
   {
      g_print("Unable to open the file %s to read\n", argv[1]);
      g_print("Press any key to exit\n");
      getchar();
      return 1;
   }
   
   out_channel = g_io_channel_new_file(argv[2], "w", &error);
   
   if(!out_channel)
   {
      g_print("Unable to open the file %s to write\n", argv[2]);
      g_print("Press any key to exit\n");
      getchar();
      return 1;
   }
   
   do
   {
      g_io_channel_read_chars(in_channel, buf, 100, &bytes_read, &error);
      if(error)
      {
         g_print("Error while reading file %s\n", argv[1]);
         g_print("Press any key to exit\n");
         getchar();
         return 1;
      }
      
      g_io_channel_write_chars(out_channel, buf,bytes_read, &bytes_written, &error);
      if(error)
      {
         g_print("Error while writing file %s\n", argv[2]);
         g_print("Press any key to exit\n");
         getchar();
         return 1;
      }
   }
   while(bytes_read > 0);
   
       g_io_channel_shutdown(in_channel, TRUE, &error);
   if(error)
   {
      g_print("Error has occured\n");
      g_print("Press any key to exit\n");
      getchar();
      return 1;
   }
   
   g_io_channel_shutdown(out_channel, TRUE, &error);
   if(error)
   {
      g_print("Error has occured\n");
      g_print("Press any key to exit\n");
      getchar();
      return 1;
   }
   
   g_print("File copied successfully...\n");
   getchar();
   
   return 0;
}


Top
 Profile  
 
 Post subject: Re: GIOChannel not working
PostPosted: Fri Aug 03, 2012 8:02 am 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hi.

Not sure what could be wrong here. The code is fine and when I tested it, it performed exactly as expected. Steps I took is:
  1. copy code into io.c
  2. compile the code into io using this
    Code:
    $ gcc -Wall $(pkg-config --cflags gio-2.0) -c io.c
    $ gcc -Wall $(pkg-config --libs gio-2.0) -o io io.o
  3. execute app like
    Code:
    ./io io.c new_io.c

Not sure what went wrong with your code, but I'll assume that calling cp used system cp command which refuses to overwrite files without -f flag. Maybe the code should be fixed a bit and line 17 replaced by this:
Code:
      g_print("usage: %s <SOURCE> <DESTINATION>\n", argv[0]);


Cheers,
Tadej


Top
 Profile  
 
 Post subject: Re: GIOChannel not working
PostPosted: Fri Aug 03, 2012 5:38 pm 
Offline
GTK+ Guru

Joined: Fri Mar 25, 2011 5:16 pm
Posts: 177
Location: USA
Thank you very much tadeboro.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group