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.htmlCode:
/*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;
}