GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Tue Oct 21, 2014 11:59 am

All times are UTC




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: glib array help
PostPosted: Mon Sep 10, 2012 10:37 pm 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Dear friends,
I am trying to learn using glib array. From the gtk documentation, I
managed to write this piece of code:

Code:
#include <stdio.h>
#include <glib.h>
int main(int argc, char** argv) {
int i;
FILE *finp;
char *alst[10];
GArray* a = g_array_new(FALSE, FALSE, sizeof(char*));
finp=fopen("fauth","r");
for (i==0;i<=10;i++){
        fscanf(finp, "%s", &alst[i]);
        g_array_append_val(a, alst[i]);
        printf("%s", g_array_index(a, char*, i));
}
printf("There are now %d items in the array\n", a->len);
g_array_free(a, FALSE);
return 0;
}

while compiling, its giving warning:
Quote:
$ gcc -Wall auc.c `pkg-config --cflags --libs gtk+-3.0`
auc.c: In function ‘main’:
auc.c:9:1: warning: statement with no effect [-Wunused-value]
auc.c:10:9: warning: format ‘%s’ expects argument of type ‘char *’, but
argument 3 has type ‘char **’ [-Wformat]


Can you kindly help me detecting the error?
Typical file that I am using is:
Quote:
    Taylor, DW,
    {\textbf{Banerjee, Rudra}} and Mookerjee, Abhijit and Sanyal, Biplab,
    {\textbf{R. Banerjee}} and Banerjee, M. and Majumdar, AK and
    Mookerjee, A. and Sanyal, B. and Hellsvik, J. and Eriksson, O. and
    Nigam, AK,
    {\textbf{Rudra Banerjee}} and Mookerjee, Abhijit,
    {\textbf{Rudra Banerjee}} and Mookerjee, Abhijit,
    Gyorffy, B. L. and Pindor, A. J. and Staunton, J. and Stocks, G. M.
    and Winter, H.,
    Abdul-Razzaq, W. and Kouvel, JS,
    Aitken, RG and Cheung, TD and Kouvel, JS and Hurdequint, H.,
    Alam, A. and Mookerjee, A.,
    Alam, A. and Mookerjee, A.,


Top
 Profile  
 
 Post subject: Re: glib array help
PostPosted: Tue Sep 11, 2012 2:30 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
There is no error in how you are using garray but rather generic c programming errors.
Firstly the error message should clue you in. Its telling you arguement 3 of fscan has type char** when type char* is needed. I.e. it should be
Code:
fscanf(finp, "%s", alst[i]);
instead.
Secondly your for loop isn't working. That's because you have a comparison operator == instead of an assignment operator =.
Thirdly (though this is not an error) glib has portable replacements for fscanf and printf so you should use the glib versions. You won't have to include stdio then and you will also avoid all the pitfalls associated with printf & co. Have a look up of http://developer.gnome.org/glib/stable/glib-String-Utility-Functions.html and http://developer.gnome.org/glib/stable/glib-File-Utilities.html, e.g. g_snprintf and g_file_get_contents.


Top
 Profile  
 
 Post subject: Re: glib array help
PostPosted: Tue Sep 11, 2012 4:52 am 
Offline
GTK+ Guru

Joined: Fri Mar 25, 2011 5:16 pm
Posts: 177
Location: USA
I saw the fscanf and the for loop error. One other issue, though it's not contributing to the error messages, is that you opened the file with fopen() but didn't close it using fclose().

I'm not sure what the file extension of the file is your trying to read but it appears to be an .html file. Anyhow I did a test and replaced it with a .txt file.
I didn't comment because with these changes the errors go away but program seg faults and I was trying to figure out why.

I don't see a Glib replacement listed for fscanf. The only ones dealing with files are g_fprintf and g_vfprintf. Still I'd like to know why it's not working with fscanf.
If you do end up going with one of the Glib replacements be aware that:
Quote:
... you'll have to explicitly include <glib/gprintf.h> in order to use the GLib printf() functions.

http://developer.gnome.org/glib/stable/glib-String-Utility-Functions.html#glib-String-Utility-Functions.description


Top
 Profile  
 
 Post subject: Re: glib array help
PostPosted: Tue Sep 11, 2012 12:40 pm 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
With your help, and after gaining some idea about string, I edited the code:
Code:
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include <glib/gprintf.h>
int main(int argc, char** argv) {
int i;
FILE *finp;
GArray* a = g_array_new(FALSE, FALSE, sizeof(char*));
char alst[10][500], buffer[500];
finp=fopen("fauth.dat","r");
if(!finp){
        printf("finp failed\n");
}

while(fgets(buffer,500,finp)){
        strcpy(alst[i],buffer);
        i++;
    }
fclose(finp);

for (i = 0; i < 10; i++){
        g_array_append_val(a, alst[i]);
        printf("%d",i);
        printf("%s\n", alst[i]);
//        printf("%s", g_array_index(a, char*, i));
}
printf("There are now %d items in the array\n", a->len);
g_array_free(a, FALSE);
return 0;
}

Now, there is no warning, but the seg fault is still there.
The seg fault occurs at the line printf("%s", g_array_index(a, char*, i)); but I am clueless how to handle this.


Top
 Profile  
 
 Post subject: Re: glib array help
PostPosted: Tue Sep 11, 2012 1:16 pm 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
no direct fscanf replacement I know of. I use g_file_get_contents and then g_strsplit.
At the moment your %s will delimit off whitespace which will not make much sense when you are using latex tags. You'd be better off storing entire lines or for the case of latex/bibtex delimit off two consecutive EndOfLine markers (and then potentially further according to latex curly bracketing or key=value entries for bibtex). g_strsplit gives a good deal of flexibility in how you might want to split up the text.
I don't get any segfaults with things changed over to this system. Here's a little example:
Code:
#include <glib.h>
int main(int argc, char** argv)
{
   gint j;
   gchar** alst;
   gchar* cts=NULL;
   GError* Err=NULL;
   GArray* a=g_array_new(FALSE, FALSE, sizeof(gchar*));
   if (g_file_get_contents("/home/paul/test.tex", &cts, NULL, &Err))
   {
      alst=g_strsplit_set(cts, "\n", 0);
      g_free(cts);
      j=0;
      g_print("blah");
      while (alst[j])
      {
         g_array_append_val(a, alst[j]);
              g_print("%s", g_array_index(a, gchar*, j++));
      }
      g_strfreev(alst);
   }
   else
   {
      g_print("Loading failed, error: %s.", Err->message);
      g_error_free(Err);
   }
   g_print("There are now %d items in the array\n", a->len);
   g_array_free(a, FALSE);
   return 0;
}

Of course there's no real need for the garray in this example, it's just for demonstration. Though you may perform further manipulations that may make it useful over the char** case.
You might note the lack of buffers in this case through the advantage of using built in dynamic allocation of memory. Thus there are no overflows to worry about.
Not sure why you are getting segfaults yet. Will look into it.


Top
 Profile  
 
 Post subject: Re: glib array help
PostPosted: Tue Sep 11, 2012 1:37 pm 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Thanks!
Its nice, and completly on glib alone!
Thanks a lot.


Top
 Profile  
 
 Post subject: Re: glib array help
PostPosted: Tue Sep 11, 2012 7:31 pm 
Offline
Never Seen the Sunlight

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

Just for the sake of completeness, your main problem with fscanf is that you need preallocated memory in which fscanf can write to. All you did in your code is allocate ten pointers to char and they all pointed nowhere.

You probably need to get a book on C, since most of your problems is not GLib/GTK+ related at all.

Cheers,
Tadej


Top
 Profile  
 
 Post subject: Re: glib array help
PostPosted: Tue Sep 11, 2012 7:41 pm 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Quote:
You probably need to get a book on C, since most of your problems is not GLib/GTK+ related at all.

Yes, I am also feeling that I need to learn C a fresh. My C knowledge is mostly based on web tutorials(like few tutorials given in tutorial section of this forum as well). Though I have a K&R, I am ashemed to accept the fact that I have not managed to learn much from that.
May I request you people to suggest me some "good" book that incorporates a lot of example?


Top
 Profile  
 
 Post subject: Re: glib array help
PostPosted: Tue Sep 11, 2012 8:08 pm 
Offline
Never Seen the Sunlight

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

My favorite books on C are "Sams Teach Yourself C for Linux Programming in 21 Days" by Eric De Castro Lopo (great book for beginners - I started with that book) and "Expert C Programming: Deep C Secrets" by Peter van der Linden. After that, it's been all reading others' people code for me.

Cheers,
Tadej


Top
 Profile  
 
 Post subject: Re: glib array help
PostPosted: Tue Sep 11, 2012 11:46 pm 
Offline
GTK+ Guru

Joined: Fri Mar 25, 2011 5:16 pm
Posts: 177
Location: USA
This works. I'm using text.txt with the following on their own lines: one two three four five six seven eight nine ten
Code:
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
#include <glib/gprintf.h>

int main(int argc, char* argv[])
{
  int i;
  char *alst[10];
  FILE *finp;
  GArray *a = g_array_new (FALSE,  FALSE,  sizeof(char*));
  finp = fopen ("text.txt","r");

  for(i = 0; i < 10; i++)
  {
    alst[i] = malloc (sizeof(char)); 
    fscanf (finp, "%s", alst[i]);
    g_array_append_val (a, alst[i]);
    printf ("%s\n", g_array_index (a, char*, i));
  }

  fclose(finp);
  g_array_free(a, FALSE);
  return 0;
}


Top
 Profile  
 
 Post subject: Re: glib array help
PostPosted: Wed Sep 12, 2012 1:54 am 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
zerohour,
Thanks for your enormous support.


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

All times are UTC


Who is online

Users browsing this forum: Yahoo [Bot] and 4 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