![]() |
get string from file
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[i],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 ? |
Re: get string from file
"Rudra Banerjee" <bnrj.rudra@gmail.com> schrieb im Newsbeitrag news:a5ea95cf-4cc2-4aaa-9cbc-8f73cc2c482d@googlegroups.com... > 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[i],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? |
Re: get string from file
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(dialo g), 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(dialo g), filter); gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialo g), filter); if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dia log)); 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[i],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[i], COL_BIB_YEAR, ayear[i], COL_BIB_PUB, atitle[i], -1); } } gtk_label_set_text(GTK_LABEL(flabel), filename); gtk_widget_destroy(dialog); } |
Re: get string from file
Rudra Banerjee <bnrj.rudra@gmail.com> 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(dialo g), 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(dialo g), filter); > gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialo g), filter); > > if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { > filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dia log)); > 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[i],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[i], sizeof aauth[i], 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[i], > COL_BIB_YEAR, ayear[i], > COL_BIB_PUB, atitle[i], > -1); > } > > > } > gtk_label_set_text(GTK_LABEL(flabel), filename); > gtk_widget_destroy(dialog); > } > -- Ben. |
Re: get string from file
while (i < 6 && fgets(aauth[i], sizeof aauth[i], fauth)) This one is nice and it did the trick. |
Re: get string from file
ayear[6][4] seems a bit short and might overflow(4digits +terminating
'\0' char). |
Re: get string from file
Rudra Banerjee <bnrj.rudra@gmail.com> writes:
> while (i < 6 && fgets(aauth[i], sizeof aauth[i], 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. |
Re: get string from file
"Rudra Banerjee" <bnrj.rudra@gmail.com> schrieb im Newsbeitrag news:ec632cdc-bacf-4774-8816-85587e871259@googlegroups.com... .... > while(fgets(buffer,500,fauth)){ > g_print("i%s",i); > strcpy(aauth[i],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[i],buffer); may write to memory beyond the declared range of aauth. .... |
Re: get string from file
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[i], sizeof aauth[i], 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[i]); } fclose(fauth); } return(0); } and fauth.dat looks like as before. |
Re: get string from file
Rudra Banerjee <bnrj.rudra@gmail.com> 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[i], sizeof aauth[i], 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[i]); 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. |
| All times are GMT. The time now is 06:32 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.