GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

help me..

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
vinodh
Familiar Face


Joined: 12 Feb 2008
Posts: 8

PostPosted: Wed Mar 19, 2008 3:52 am    Post subject: help me.. Reply with quote

help me by solving this

--------------------------------------------------------------------------------

i want to display the list of partitions in a combo box reading from a file /proc/partitons.

take a look at the below code..

Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14

char *p;
n=4;
FILE *f=fopen("/proc/partitions","r");
Glist combo_items=NULL;


while(n!=0)
{
fgets(p,20,*f);
combo_items=g_list_append(combo_items,(gpointer) p);
n--;



but i cant get the expected result.
Back to top
dreblen
Familiar Face


Joined: 14 Jun 2007
Posts: 38

PostPosted: Sat Mar 22, 2008 8:12 pm    Post subject: Reply with quote

what results are you getting?
Back to top
vinodh
Familiar Face


Joined: 12 Feb 2008
Posts: 8

PostPosted: Mon Mar 24, 2008 7:33 am    Post subject: gtk combo Reply with quote

i was getting some unknown characters or the last item of the list for 5 times..


example:

let us take the file contains the list

system
windows
linux
partitions


the output iam getting is the combo box containing the text "partitions" for 4 times..
Back to top
dreblen
Familiar Face


Joined: 14 Jun 2007
Posts: 38

PostPosted: Mon Mar 24, 2008 2:33 pm    Post subject: Reply with quote

okay, the reason that you're getting the same results for every item in the list is because they are all using the same pointer (p),
so they all have the same address.
I've put together an example program that fixes this, although there is probably a better way to do this.

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
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>

int main(int argc, char **argv)
{
     char *p, *t;
    int n = 4;
    FILE *f=fopen("partitions","r");
    GList *combo_items=NULL;
   
   
    while(n!=0)
    {
    fgets(p,20,f);
    t = (char*)malloc(strlen(p) + 1);
    strcpy(t, p);
    combo_items=g_list_append(combo_items,(gpointer)t);
    n--;
    t = NULL;
    }
   
    n = 4;
    combo_items = g_list_first(combo_items);
    while(1)
    {
        printf("Address of data is: %p\n", combo_items->data);
        printf("Value of data is:   '%s'\n", (char*)combo_items->data);
        if(combo_items->next != NULL)
            combo_items = combo_items->next;
        else
            break
;
    }
   
     return 0;
}


the "partitions" file, is a file with this contents:

Code: (Plaintext)
1
2
3
4
5
system
windows
linux
partitions
Back to top
vinodh
Familiar Face


Joined: 12 Feb 2008
Posts: 8

PostPosted: Tue Mar 25, 2008 3:32 am    Post subject: linux partitions help Reply with quote

ok, thank you ..

Actually i am want to create a GUI for editing FSTAB. since i use multiple linux os and Windows. i want select the linux partitions from which i want to edit the /etc/fstab file.

so can you plz give me an idea to display only the linux partitions.

i think u can help me..
Back to top
dreblen
Familiar Face


Joined: 14 Jun 2007
Posts: 38

PostPosted: Tue Mar 25, 2008 3:49 pm    Post subject: Reply with quote

okay, my only linux system at the moment is a virtual machine, so my fstab is a bit different,
but I looked it up to get a more standard one and for my tests, I've been using this:

Code: (Plaintext)
1
2
3
4
5
6
7
/dev/hda2      /      ext2      defaults      1 1
/dev/hdb1     /home     ext2     defaults     1 2
/dev/cdrom     /media/cdrom     auto     ro,noauto,user,exec     0 0
/dev/fd0     /media/floppy     auto     rw,noauto,user,sync     0 0
proc     /proc     proc     defaults     0 0
/dev/hda1     swap     swap     pri=42     0 0

okay now, my code that I've written takes the "fstab.txt" file (you might not want to mess with the real /etc/fstab until you're sure everything is stable),
reads every char in a line (up to the '\n' char) into gchar *p and then appends p to the combo box if it contains either "ext2", "ext3" or "reiserfs" (any of which would indicate a linux system).

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <gtk/gtk.h>
#include <stdlib.h>
#include <string.h>

int strpos(char *str, char *sub);

int main(int argc, char **argv)
{
     GtkWidget *window;
     GtkWidget *combo;
     gchar *p = (gchar*)malloc(1);
     FILE *fp;
     char c;
     int i;

     gtk_init(&argc, &argv);

     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
     g_signal_connect(G_OBJECT(window), "delete_event", G_CALLBACK(gtk_main_quit), NULL);

     combo = gtk_combo_box_new_text();
     fp = fopen("fstab.txt", "r");
     do
     
{
         strcpy(p, "");
         for(i = 0;; i++)
         {
            c = fgetc(fp);
            p = (gchar*)realloc(p, strlen(p)+1);
            if(c == '\n' || c == EOF)
            {
                p[i] = '\0';
                break;
            }
            else
           
{
                p[i] = c;
            }
         }
         if((strpos(p, "ext2") != -1) || (strpos(p, "ext3") != -1) || (strpos(p, "reiserfs") != -1))
            gtk_combo_box_append_text(GTK_COMBO_BOX(combo), p);
     }while(c != EOF);
     fclose(fp);
    gtk_container_add(GTK_CONTAINER(window), combo);
    gtk_widget_show(combo);

    gtk_widget_show(window);

    gtk_main();

     return 0;
}

/* strpos returns the char index of the first occurrence of the letter of sub if sub is in str, ex:
 *
 * int x = strpos("Hello", "ll");
 * (x now equals 2)
 * int y = strpos("Hello", "l");
 * (y now equals 2, not 3)
 */
int strpos(char *str, char *sub)
{
    int i, j, k;
    int ret = -1;
    int done = 0;

    for(i = 0; i < strlen(str); i++)
    {
        /* first letter was a match, check the rest */
       
if(str[i] == sub[0])
        {
            for(j = 0, k = i; j <= strlen(sub); j++, k++)
            {
                if(sub[j] == '\0')
                {
                    ret = i;
                    done = 1;
                    break;
                }
                if(str[k] != sub[j])
                {
                    break;
                }
            }
        }
        if(done == 1)
            break;
    }

    return ret;
}


You could do further work on p before you append it as well (such as separating the different items based on whitespace).
I hope that this helps you...
Back to top
vinodh
Familiar Face


Joined: 12 Feb 2008
Posts: 8

PostPosted: Wed Mar 26, 2008 4:48 am    Post subject: gtk combo Reply with quote

the code which you have given before doesn't solve me check this code:
it was displaying same item in the combo box for 4 times,But it was printing the list correctly. I even tried pointer array it also not solving the problem....


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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

#include <gtk/gtk.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <gdk/gdkkeysyms.h>

GtkWidget* createcombo (void)
{
  GtkWidget *window1;
  GtkWidget *fixed1;
  GtkWidget *combo1;
  GList *combo1_items = NULL;
  FILE *f;

int n=4;
char *q;
char *t;

//the filesystem.txt contains the list of linux partitions..

f=fopen("/root/Desktop/untitled/filesystem.txt","r");

  window1 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window1), "window1");
  g_signal_connect (G_OBJECT (window1), "delete_event", G_CALLBACK (gtk_main_quit), NULL);
  gtk_widget_show (window1);

  fixed1 = gtk_fixed_new ();
  gtk_widget_show (fixed1);
  gtk_container_add (GTK_CONTAINER (window1), fixed1);

  combo1 = gtk_combo_new ();
   
  gtk_fixed_put (GTK_FIXED (fixed1), combo1, 80, 56);
  gtk_widget_set_size_request (combo1, 187, 27);
  gtk_widget_show (combo1);

while(n!=0)
{

fgets(q,15,f);
t[n]= (char *) malloc (strlen(q)+1);
printf("%s",q);
strcpy(t,q);
combo1_items = g_list_append (combo1_items, (gpointer) t);

n--;
t=NULL;
printf("%s",t);
}
gtk_combo_set_popdown_strings (GTK_COMBO (combo1), combo1_items);
g_list_free (combo1_items);

return window1;
}


int main (int argc, char *argv[])
{
  gtk_init (&argc, &argv);

  GtkWidget *window1;

window1=createcombo();
  gtk_main ();
  return 0;
}
Back to top
dreblen
Familiar Face


Joined: 14 Jun 2007
Posts: 38

PostPosted: Wed Mar 26, 2008 8:00 pm    Post subject: Reply with quote

okay, I've redone your code and it works for me currently. (I'm using my fstab file that I posted earlier)

I'd like to point out a few spots that were causing troubles in your code:
line 46: t is a char*, so t[n] is assigning the nth character in the character array, you would need a char** to do an array of strings instead of characters.
line 52,53: in 52, you set t to NULL, then in 53, you print the value of t which would cause a runtime error.

Anyway, here's my redone version:

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <gtk/gtk.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <gdk/gdkkeysyms.h>

GtkWidget* createcombo (void)
{
    GtkWidget *window1;
    GtkWidget *fixed1;
    GtkWidget *combo1;
    GList *combo1_items = NULL;
    FILE *f;

    char c;
    int i = 0, j = 0;
    char *q;

    window1 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (window1), "window1");
    g_signal_connect (G_OBJECT (window1), "delete_event", G_CALLBACK (gtk_main_quit), NULL);
    gtk_widget_show (window1);

    fixed1 = gtk_fixed_new ();
    gtk_widget_show (fixed1);
    gtk_container_add (GTK_CONTAINER (window1), fixed1);

    combo1 = gtk_combo_new ();

    gtk_fixed_put (GTK_FIXED (fixed1), combo1, 80, 56);
    gtk_widget_set_size_request (combo1, 187, 27);
    gtk_widget_show (combo1);

    /* I'm using an fstab again */
   
f = fopen("fstab.txt", "r");
    if(f == NULL)
    {
        fprintf(stderr, "Could not open file!\n");
        return NULL;
    }

    q = NULL;
    do
   
{
        if(q == NULL)
        {
            q = (char*)malloc(1);
            if(q == NULL)
            {
                fprintf(stderr, "Not enough memory!\n");
                return NULL;
            }
            strcpy(q, "");
        }
        c = fgetc(f);
        if(c != '\n')
        {
            q = (char*)realloc(q, strlen(q) + 2);
            if(q == NULL)
            {
                fprintf(stderr, "Not enough memory!\n");
                return NULL;
            }
            q[i] = c;
            q[i+1] = '\0';
        }
        else
       
{
            q[i] = '\0';
            combo1_items = g_list_append(combo1_items, (gpointer)q);
            q = NULL;
            i = j = -1;
        }

        i++; j++;
    }while(c != EOF);
    fclose(f);
    gtk_combo_set_popdown_strings (GTK_COMBO (combo1), combo1_items);
    g_list_free (combo1_items);

    return window1;
}


int main (int argc, char *argv[])
{
    gtk_init (&argc, &argv);

    GtkWidget *window1;

    window1=createcombo();
    gtk_main ();
    return 0;
}
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP