get string from file

Discussion in 'C Programming' started by Rudra Banerjee, Sep 16, 2012.

  1. Dear friends,
    being a C novice, string is confusing me again.
    In my code, I have:
    FILE *fauth;
    char aauth[6][500],buffer[500];
    fauth=fopen("fauth.dat","r");

    if(!fauth){
    printf("fauth failed\n");
    }

    while(fgets(buffer,500,fauth)){
    g_print("i%d",i);
    strcpy(aauth,buffer);
    i++;
    }
    fclose(fauth);

    which is giving seg fault.
    the file I am reading is a 6 line of string
    b bq,
    b,
    a,
    abc,
    abcd,
    pqrs,

    what's wrong in this ?
    Rudra Banerjee, Sep 16, 2012
    #1
    1. Advertising

  2. "Rudra Banerjee" <> schrieb im Newsbeitrag
    news:...
    > Dear friends,
    > being a C novice, string is confusing me again.
    > In my code, I have:
    > FILE *fauth;
    > char aauth[6][500],buffer[500];
    > fauth=fopen("fauth.dat","r");
    >
    > if(!fauth){
    > printf("fauth failed\n");
    > }

    else
    > while(fgets(buffer,500,fauth)){
    > g_print("i%d",i);
    > strcpy(aauth,buffer);
    > i++;
    > }
    > fclose(fauth);
    >
    > which is giving seg fault.
    > the file I am reading is a 6 line of string
    > b bq,
    > b,
    > a,
    > abc,
    > abcd,
    > pqrs,
    >
    > what's wrong in this ?


    Does it print "fauth failed"?
    An else is missing where indicated.
    Maybe g_print gives the seg fault.
    May I see it's code?
    Heinrich Wolf, Sep 16, 2012
    #2
    1. Advertising

  3. On Sunday, September 16, 2012 11:04:06 AM UTC+1, Heinrich Wolf wrote:
    > May I see it's code?


    Thanks for your reply.
    No, its not saying fauth failed. Its just giving segmentation fault.
    The code is for opening and reading file.
    The complete function is:
    static void open_file(GtkWidget *widget, gpointer data) {
    GtkWidget *dialog; //, *entry;
    GtkFileFilter *filter;
    dialog = gtk_file_chooser_dialog_new("Open File", NULL,
    GTK_FILE_CHOOSER_ACTION_OPEN,
    GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
    GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
    NULL);

    filter = gtk_file_filter_new();
    gtk_file_filter_set_name(filter, "All files (*.*)");
    gtk_file_filter_add_pattern(filter, "*");
    gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);

    filter = gtk_file_filter_new();
    gtk_file_filter_set_name(filter, "Bibtex file (*.bib)");
    gtk_file_filter_add_pattern(filter, "*.bib");
    gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
    gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);

    if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
    filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
    char pcomm[1028];
    sprintf(pcomm, "bash parse.sh %s", filename);
    g_print("%s\n", pcomm);
    system((char *) pcomm);
    int i,j,k;
    FILE *fauth, *fyear, *ftitle;
    char aauth[6][500],ayear[6][4],atitle[6][500], buffer[500];
    fauth=fopen("fauth.dat","r");
    fyear=fopen("fyear.dat","r");
    ftitle=fopen("ftitle.dat","r");

    if(!fauth||!ftitle||!fyear){
    printf("fauth failed\n");
    }

    while(fgets(buffer,500,fauth)){
    g_print("i%s",i);
    strcpy(aauth,buffer);
    i++;
    }
    fclose(fauth);
    while(fgets(buffer,500,fyear)){
    strcpy(ayear[j],buffer);
    j++;
    }
    fclose(fyear);
    while(fgets(buffer,500,ftitle)){
    strcpy(atitle[k],buffer);
    k++;
    }
    fclose(ftitle);


    for (i = 0; i < 6; i++){
    gtk_tree_store_append(GTK_TREE_STORE(treestore), &toplevel,NULL);
    gtk_tree_store_set(treestore, &toplevel,
    COL_BIB_TYPE, NULL,
    COL_BIB_NAME, aauth,
    COL_BIB_YEAR, ayear,
    COL_BIB_PUB, atitle,
    -1);
    }


    }
    gtk_label_set_text(GTK_LABEL(flabel), filename);
    gtk_widget_destroy(dialog);
    }
    Rudra Banerjee, Sep 16, 2012
    #3
  4. Rudra Banerjee <> writes:

    > On Sunday, September 16, 2012 11:04:06 AM UTC+1, Heinrich Wolf wrote:
    >> May I see it's code?

    >
    > Thanks for your reply.
    > No, its not saying fauth failed. Its just giving segmentation fault.
    > The code is for opening and reading file.
    > The complete function is:
    > static void open_file(GtkWidget *widget, gpointer data) {
    > GtkWidget *dialog; //, *entry;
    > GtkFileFilter *filter;
    > dialog = gtk_file_chooser_dialog_new("Open File", NULL,
    > GTK_FILE_CHOOSER_ACTION_OPEN,
    > GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
    > GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
    > NULL);
    >
    > filter = gtk_file_filter_new();
    > gtk_file_filter_set_name(filter, "All files (*.*)");
    > gtk_file_filter_add_pattern(filter, "*");
    > gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
    >
    > filter = gtk_file_filter_new();
    > gtk_file_filter_set_name(filter, "Bibtex file (*.bib)");
    > gtk_file_filter_add_pattern(filter, "*.bib");
    > gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
    > gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
    >
    > if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
    > filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
    > char pcomm[1028];
    > sprintf(pcomm, "bash parse.sh %s", filename);
    > g_print("%s\n", pcomm);
    > system((char *) pcomm);
    > int i,j,k;


    You never set these.

    > FILE *fauth, *fyear, *ftitle;
    > char aauth[6][500],ayear[6][4],atitle[6][500], buffer[500];
    > fauth=fopen("fauth.dat","r");
    > fyear=fopen("fyear.dat","r");
    > ftitle=fopen("ftitle.dat","r");
    >
    > if(!fauth||!ftitle||!fyear){
    > printf("fauth failed\n");
    > }


    If any on these have failed you can't just go ahead and use them.

    >
    > while(fgets(buffer,500,fauth)){
    > g_print("i%s",i);
    > strcpy(aauth,buffer);
    > i++;
    > }


    I don't see any need to read into a buffer and then copy to where you
    want it. Also, you should check that there is somewhere for the string
    to go. I'd write:

    while (i < 6 && fgets(aauth, sizeof aauth, fauth))
    i++;

    > fclose(fauth);
    > while(fgets(buffer,500,fyear)){
    > strcpy(ayear[j],buffer);
    > j++;
    > }
    > fclose(fyear);
    > while(fgets(buffer,500,ftitle)){
    > strcpy(atitle[k],buffer);
    > k++;
    > }
    > fclose(ftitle);
    >
    >
    > for (i = 0; i < 6; i++){
    > gtk_tree_store_append(GTK_TREE_STORE(treestore), &toplevel,NULL);
    > gtk_tree_store_set(treestore, &toplevel,
    > COL_BIB_TYPE, NULL,
    > COL_BIB_NAME, aauth,
    > COL_BIB_YEAR, ayear,
    > COL_BIB_PUB, atitle,
    > -1);
    > }
    >
    >
    > }
    > gtk_label_set_text(GTK_LABEL(flabel), filename);
    > gtk_widget_destroy(dialog);
    > }
    >


    --
    Ben.
    Ben Bacarisse, Sep 16, 2012
    #4
  5. while (i < 6 && fgets(aauth, sizeof aauth, fauth))

    This one is nice and it did the trick.
    Rudra Banerjee, Sep 16, 2012
    #5
  6. ayear[6][4] seems a bit short and might overflow(4digits +terminating
    '\0' char).
    Johann Klammer, Sep 16, 2012
    #6
  7. Rudra Banerjee <> writes:

    > while (i < 6 && fgets(aauth, sizeof aauth, fauth))
    >
    > This one is nice and it did the trick.


    That's worrying. You had uninitialised variables (unless I missed
    something of course) and that must be fixed too. Simply getting the
    code to work is not enough.

    --
    Ben.
    Ben Bacarisse, Sep 16, 2012
    #7
  8. "Rudra Banerjee" <> schrieb im Newsbeitrag
    news:...
    ....
    > while(fgets(buffer,500,fauth)){
    > g_print("i%s",i);
    > strcpy(aauth,buffer);
    > i++;
    > }


    In your top posting you wrote
    > g_print("i%d",i);

    So at first glance I found no problem with it.
    Here you write
    > g_print("i%s",i);

    I guess g_print is a GTK library function like printf
    and %s must be followed by a string, but you supply an integer!

    Now I also see that i is not initialized.
    So also strcpy(aauth,buffer); may write to memory beyond the declared
    range of aauth.
    ....
    Heinrich Wolf, Sep 16, 2012
    #8
  9. On Sunday, September 16, 2012 3:24:23 PM UTC+1, Ben Bacarisse wrote:
    >
    > That's worrying. You had uninitialised variables (unless I missed
    >


    Ben,
    you are damn right and I am damn idiot. There is error, no error in running, but of I try to print the string, there is error in writing.
    Can you kindly help me with that?

    The complete code. after your correction is:
    #include <stdio.h>
    #include <string.h>
    char main(char argc, int argv[]){

    int i,j,k;
    FILE *fauth, *fyear, *ftitle;
    char aauth[6][500],ayear[6][4],atitle[6][500], buffer[500];
    fauth=fopen("fauth.dat","r");
    // fyear=fopen("fyear.dat","r");
    // ftitle=fopen("ftitle.dat","r");

    if(!fyear){
    printf("fauth failed\n");
    }
    else{
    while (i < 6 && fgets(aauth, sizeof aauth, fauth))
    // while (j < 6 && fgets(ayear[j], sizeof(ayear[j]), fyear))
    // while (k < 6 && fgets(atitle[k], sizeof atitle[k], ftitle))
    for (i=0; i<6;i++){
    printf("%s\n",ayear);
    }
    fclose(fauth);
    }
    return(0);
    }

    and fauth.dat looks like as before.
    Rudra Banerjee, Sep 16, 2012
    #9
  10. Rudra Banerjee <> writes:

    > On Sunday, September 16, 2012 3:24:23 PM UTC+1, Ben Bacarisse wrote:
    >>
    >> That's worrying. You had uninitialised variables (unless I missed
    >>

    >
    > Ben, you are damn right and I am damn idiot. There is error, no error
    > in running, but of I try to print the string, there is error in
    > writing.


    > Can you kindly help me with that?
    >
    > The complete code. after your correction is:


    You have not corrected the un-initialised variables and the code below
    has so many other errors, that it's not surprising it goes wrong.

    > #include <stdio.h>
    > #include <string.h>
    > char main(char argc, int argv[]){


    There are three types in this line and all three are wrong!

    > int i,j,k;


    int i = 0, j = 0, k = 0;

    > FILE *fauth, *fyear, *ftitle;
    > char aauth[6][500],ayear[6][4],atitle[6][500], buffer[500];
    > fauth=fopen("fauth.dat","r");
    > // fyear=fopen("fyear.dat","r");
    > // ftitle=fopen("ftitle.dat","r");
    >
    > if(!fyear){


    fyear is uninitialised. You probably meat to use fauth since that one
    is opened.

    > printf("fauth failed\n");
    > }
    > else{
    > while (i < 6 && fgets(aauth, sizeof aauth, fauth))
    > // while (j < 6 && fgets(ayear[j], sizeof(ayear[j]), fyear))
    > // while (k < 6 && fgets(atitle[k], sizeof atitle[k], ftitle))
    > for (i=0; i<6;i++){


    This loop is the body of the while loop. That's not what I wrote and
    it's going to make everything go wrong. Does your editor help you with
    indentation? If not get one that does!

    > printf("%s\n",ayear);


    ayear is uninitialised. You probably meant aauth.

    > }
    > fclose(fauth);
    > }
    > return(0);
    > }
    >
    > and fauth.dat looks like as before.


    You need to be a lot more careful about details!

    --
    Ben.
    Ben Bacarisse, Sep 16, 2012
    #10
  11. Ben,
    Extremely sorry for the mistake. it happned while I was trying not to post the comments.
    In this code, as you can see, the array is printing crap at the end of the code, probably means, datas are not stored in the array.

    #include <stdio.h>
    #include <string.h>
    char main(char argc, int argv[]){
    int i=0,j=0,k=0;
    FILE *fauth, *fyear, *ftitle;
    char aauth[6][500],ayear[6][4],atitle[6][500], buffer[500];
    fauth=fopen("fauth.dat","r");

    if(!fauth){
    printf("fauth failed\n");
    }
    else{
    while (i < 6 && fgets(aauth, sizeof aauth, fauth))
    printf("%d%s\n",i,aauth);
    i++;
    fclose(fauth);
    }
    for (i=0;i<6;i++){
    printf("%d%s\n",i,aauth);
    }
    return(0);
    }
    Rudra Banerjee, Sep 16, 2012
    #11
  12. Rudra Banerjee

    Angel Guest

    On 2012-09-16, Rudra Banerjee <> wrote:
    > Ben,
    > Extremely sorry for the mistake. it happned while I was trying not to
    > post the comments. In this code, as you can see, the array is printing
    > crap at the end of the code, probably means, datas are not stored in the
    > array.

    [...]
    > else{
    > while (i < 6 && fgets(aauth, sizeof aauth, fauth))
    > printf("%d%s\n",i,aauth);
    > i++;
    > fclose(fauth);
    > }


    You fclose() the stream fauth inside the loop where you are reading
    from it. Move the fclose() call outside the loop.


    --
    "C provides a programmer with more than enough rope to hang himself.
    C++ provides a firing squad, blindfold and last cigarette."
    - seen in comp.lang.c
    Angel, Sep 16, 2012
    #12
  13. Rudra Banerjee <> writes:

    > Extremely sorry for the mistake. it happned while I was trying not to
    > post the comments.
    > In this code, as you can see, the array is printing crap at the end of
    > the code, probably means, datas are not stored in the array.


    You need to take more care. Some of the problems are ones I've
    commented on before. In articular, you are confused about what is in
    and what is outside of your loop. Have you got yourself an editor that
    can help you indent you C code yet? The point is that it will show you
    when you've made one of the mistakes you've made below.

    > #include <stdio.h>
    > #include <string.h>
    > char main(char argc, int argv[]){


    All three types here are wrong.

    > int i=0,j=0,k=0;
    > FILE *fauth, *fyear, *ftitle;
    > char aauth[6][500],ayear[6][4],atitle[6][500], buffer[500];
    > fauth=fopen("fauth.dat","r");
    >
    > if(!fauth){
    > printf("fauth failed\n");
    > }
    > else{
    > while (i < 6 && fgets(aauth, sizeof aauth, fauth))
    > printf("%d%s\n",i,aauth);
    > i++;


    You've put a printf call in the loop so the i++ is now outside of it.
    Also, the printf call has the wrong argument: aauth is of the wrong type
    to be printed with a %s format.

    To be sure, you may prefer to switch to a style where you put {}s round
    every single controlled statement (if, while, for ext.) so that you
    don't make this mistake again. You've done that below, so you might
    want to do it always.

    > fclose(fauth);
    > }
    > for (i=0;i<6;i++){
    > printf("%d%s\n",i,aauth);
    > }
    > return(0);


    You don't need the ()s round 0: return <exp>; is a statement, not a
    function call. (It used to required in the dim and distant past so for
    some people it makes your code look old.)

    > }


    --
    Ben.
    Ben Bacarisse, Sep 16, 2012
    #13
  14. Angel <> writes:

    > On 2012-09-16, Rudra Banerjee <> wrote:
    >> Ben,
    >> Extremely sorry for the mistake. it happned while I was trying not to
    >> post the comments. In this code, as you can see, the array is printing
    >> crap at the end of the code, probably means, datas are not stored in the
    >> array.

    > [...]
    >> else{
    >> while (i < 6 && fgets(aauth, sizeof aauth, fauth))
    >> printf("%d%s\n",i,aauth);
    >> i++;
    >> fclose(fauth);
    >> }

    >
    > You fclose() the stream fauth inside the loop where you are reading
    > from it. Move the fclose() call outside the loop.


    No, it's outside the loop. The indentation is up the spout so it's not
    obvious but it's definitely outside.

    --
    Ben.
    Ben Bacarisse, Sep 16, 2012
    #14
  15. Rudra Banerjee

    Angel Guest

    On 2012-09-16, Ben Bacarisse <> wrote:
    > Angel <> writes:
    >
    >> On 2012-09-16, Rudra Banerjee <> wrote:
    >>> Ben,
    >>> Extremely sorry for the mistake. it happned while I was trying not to
    >>> post the comments. In this code, as you can see, the array is printing
    >>> crap at the end of the code, probably means, datas are not stored in the
    >>> array.

    >> [...]
    >>> else{
    >>> while (i < 6 && fgets(aauth, sizeof aauth, fauth))
    >>> printf("%d%s\n",i,aauth);
    >>> i++;
    >>> fclose(fauth);
    >>> }

    >>
    >> You fclose() the stream fauth inside the loop where you are reading
    >> from it. Move the fclose() call outside the loop.

    >
    > No, it's outside the loop. The indentation is up the spout so it's not
    > obvious but it's definitely outside.


    Yeah, you're right. But so is the i++ despite what the indentation
    suggests so that loop should never finish if it starts.


    --
    "C provides a programmer with more than enough rope to hang himself.
    C++ provides a firing squad, blindfold and last cigarette."
    - seen in comp.lang.c
    Angel, Sep 16, 2012
    #15
  16. Angel <> writes:

    > On 2012-09-16, Ben Bacarisse <> wrote:
    >> Angel <> writes:
    >>
    >>> On 2012-09-16, Rudra Banerjee <> wrote:
    >>>> Ben,
    >>>> Extremely sorry for the mistake. it happned while I was trying not to
    >>>> post the comments. In this code, as you can see, the array is printing
    >>>> crap at the end of the code, probably means, datas are not stored in the
    >>>> array.
    >>> [...]
    >>>> else{
    >>>> while (i < 6 && fgets(aauth, sizeof aauth, fauth))
    >>>> printf("%d%s\n",i,aauth);
    >>>> i++;
    >>>> fclose(fauth);
    >>>> }
    >>>
    >>> You fclose() the stream fauth inside the loop where you are reading
    >>> from it. Move the fclose() call outside the loop.

    >>
    >> No, it's outside the loop. The indentation is up the spout so it's not
    >> obvious but it's definitely outside.

    >
    > Yeah, you're right. But so is the i++ despite what the indentation
    > suggests so that loop should never finish if it starts.


    No, the condition has two parts -- one will almost certainly fail
    eventually.

    --
    Ben.
    Ben Bacarisse, Sep 17, 2012
    #16
  17. Rudra Banerjee

    Angel Guest

    On 2012-09-16, Ben Bacarisse <> wrote:
    > Angel <> writes:
    >
    >> On 2012-09-16, Ben Bacarisse <> wrote:
    >>> Angel <> writes:
    >>>
    >>>> On 2012-09-16, Rudra Banerjee <> wrote:
    >>>>> Ben,
    >>>>> Extremely sorry for the mistake. it happned while I was trying not to
    >>>>> post the comments. In this code, as you can see, the array is printing
    >>>>> crap at the end of the code, probably means, datas are not stored in the
    >>>>> array.
    >>>> [...]
    >>>>> else{
    >>>>> while (i < 6 && fgets(aauth, sizeof aauth, fauth))
    >>>>> printf("%d%s\n",i,aauth);
    >>>>> i++;
    >>>>> fclose(fauth);
    >>>>> }
    >>>>
    >>>> You fclose() the stream fauth inside the loop where you are reading
    >>>> from it. Move the fclose() call outside the loop.
    >>>
    >>> No, it's outside the loop. The indentation is up the spout so it's not
    >>> obvious but it's definitely outside.

    >>
    >> Yeah, you're right. But so is the i++ despite what the indentation
    >> suggests so that loop should never finish if it starts.

    >
    > No, the condition has two parts -- one will almost certainly fail
    > eventually.


    Right again. I really shouldn't be posting after midnight. :-/
    Off to bed with me...


    --
    "C provides a programmer with more than enough rope to hang himself.
    C++ provides a firing squad, blindfold and last cigarette."
    - seen in comp.lang.c
    Angel, Sep 17, 2012
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arne Schirmacher
    Replies:
    0
    Views:
    498
    Arne Schirmacher
    Feb 16, 2005
  2. Replies:
    5
    Views:
    503
  3. Replies:
    4
    Views:
    20,284
  4. Fei Liu
    Replies:
    9
    Views:
    434
  5. Mike Schilling
    Replies:
    2
    Views:
    557
    Arne Vajhøj
    Mar 5, 2010
Loading...

Share This Page