 |
GTK+ Forums Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
|
|
|
| Author |
Message |
|
|
thearunkumar Familiar Face
Joined: 09 Dec 2007 Posts: 6
|
Posted: Tue Feb 12, 2008 9:14 pm Post subject: how do I access the data type gchar**? |
|
|
In many functions, a variable of type gchar** is passed as a parameter and an array of strings get stored in it.. How do I access those strings?
Thank you..[/i] |
|
| Back to top |
|
 |
thearunkumar Familiar Face
Joined: 09 Dec 2007 Posts: 6
|
Posted: Tue Feb 12, 2008 9:20 pm Post subject: |
|
|
I tried using g_strv_length() function, which I read in glib manual, to get the number of strings and used a 'for' loop to access each string. But the program crashes..
| Code: (Plaintext) | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
GtkWidget *prompt;
gchar **output;
gchar **error;
gint *return_v;
GError **m = NULL;
gboolean return_t;
gchar string_command;
gchar temp[200];
gint count = 0;
gint output_str = 0;
GtkWidget *text_view;
GtkTextBuffer *text_buffer;
return_t = g_spawn_command_line_sync(script_path,output,error,return_v,m);
text_view = lookup_widget(GTK_WIDGET(button),"tv");
text_buffer = gtk_text_view_get_buffer(GTK_WIDGET(text_view));
if(return_t)
{
output_str = g_strv_length(output);
for(count = 0; count < output_str; count++)
g_printf("\n%s",output[count]);
}
}
| |
|
| Back to top |
|
 |
Micah Carrick Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 462 Location: Portland, OR USA
|
Posted: Wed Feb 13, 2008 1:57 pm Post subject: |
|
|
Not tested but...
| Code: (C) | 1 2 3 4 5 6
| gchar **fields;
fields = g_strsplit ("one:two", ":", 0);
if (fields == NULL || fields[0] == NULL) return 0;
g_debug("0=%s, 1=%s", fields[0], fields[1]);
g_strfreev (fields); /* free memory */ | |
|
| Back to top |
|
 |
thearunkumar Familiar Face
Joined: 09 Dec 2007 Posts: 6
|
Posted: Thu Feb 14, 2008 3:30 pm Post subject: |
|
|
Thank you.. It works. I didn't use g_debug though.. it said undefined symbol. may be i have to include some headers. I used g_print instead. I am able to execute commands with g_spawn_command_line_sync it works. I wanted to change the directory from which the child process is spawned. So I used chdir(). It didn't work. So swithced to g_spawn_sync(), in which the first argument is "working directory of the child process". When I printed the output I get neither an error nor correct output..
This is the code that I used..
| Code: (Plaintext) | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
gchar **argv;
gchar *output;
gchar *_stderr;
gboolean t;
GError *error;
argv = (gchar **)g_malloc(sizeof(gchar*)*2);
argv[0] = "/bin/ls";
argv[1] = NULL;
t = g_spawn_sync(NULL,argv,NULL,G_SPAWN_FILE_AND_ARGV_ZERO|G_SPAWN_SEARCH_PATH,NULL,NULL,&output,&_stderr,NULL,&error);
if(t)
{
g_print("\n%s",_stderr);
g_print("\n%s",output);
}
if(!t)
{
g_print("\n%s",error->message);
}
|
I am getting just new lines printed.. What might be the bug? Thank you.. |
|
| Back to top |
|
 |
Micah Carrick Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 462 Location: Portland, OR USA
|
Posted: Thu Feb 14, 2008 3:43 pm Post subject: |
|
|
The following works fine for me...
| Code: (C) | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
#include <glib-2.0/glib.h>
int main() {
gchar **argv;
gchar *output;
gchar *_stderr;
gboolean t;
GError *error;
argv = (gchar **)g_malloc(sizeof(gchar*)*2);
argv[0] = "/bin/ls";
argv[1] = NULL;
t = g_spawn_sync(NULL,argv,NULL,G_SPAWN_FILE_AND_ARGV_ZERO|G_SPAWN_SEARCH_PATH,NULL,NULL,&output,&_stderr,NULL,&error);
if(t)
{
g_print("\n%s",_stderr);
g_print("\n%s",output);
}
if(!t)
{
g_print("\n%s",error->message);
}
return 0 ;
} |
However, there is not code in there to free memory (those strings, string array, and GError should all be freed when done).
What I get is two new lines and then the output of the ls command... which makes sense. |
|
| Back to top |
|
 |
thearunkumar Familiar Face
Joined: 09 Dec 2007 Posts: 6
|
Posted: Fri Feb 15, 2008 12:49 pm Post subject: |
|
|
It didn't work for me.. Is it because I wrote it in one of the event handler functions in the file callbacks.c. I created a dialog box and had a command button in it. This was the callbacks.c I wrote for it. The handler for click event for the button "record" is
void
on_cb_record_clicked (GtkButton *button,
gpointer user_data)
It's is not displaying the ls becuase I have used it here?? I am confused.
| Code: (Plaintext) | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| # include <config.h>
#endif
#include <gtk/gtk.h>
#include "callbacks.h"
#include "interface.h"
#include "support.h"
void
on_cb_record_clicked (GtkButton *button,
gpointer user_data)
{
gchar **argv;
gchar *output;
gchar *_stderr;
gboolean t;
GError *error;
argv = (gchar **)g_malloc(sizeof(gchar*)*2);
argv[0] = "/bin/ls";
argv[1] = NULL;
t =
g_spawn_sync(NULL,argv,NULL,G_SPAWN_FILE_AND_ARGV_ZERO|G_SPAWN_SEARCH_PATH,NULL,NULL,&output,&_stderr,NULL,&error);
if(t)
{
g_print("\n%s",_stderr);
g_print("\n%s",output);
}
if(!t)
{
g_print("\n%s",error->message);
}
}
|
It's is not displaying the ls becuase I have used it here?? I am confused. |
|
| Back to top |
|
 |
|
Powered by phpBB © 2001, 2005 phpBB Group CodeBB 1.0 Beta 2
|