get string from file

R

Rudra Banerjee

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 ?
 
H

Heinrich Wolf

Rudra Banerjee said:
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?
 
R

Rudra Banerjee

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);
}
 
B

Ben Bacarisse

Rudra Banerjee said:
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);
}
 
R

Rudra Banerjee

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

This one is nice and it did the trick.
 
J

Johann Klammer

ayear[6][4] seems a bit short and might overflow(4digits +terminating
'\0' char).
 
B

Ben Bacarisse

Rudra Banerjee said:
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.
 
H

Heinrich Wolf

....
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.
....
 
R

Rudra Banerjee

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.
 
B

Ben Bacarisse

Rudra Banerjee said:
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!
 
R

Rudra Banerjee

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);
}
 
A

Angel

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.
 
B

Ben Bacarisse

Rudra Banerjee said:
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.)
 
B

Ben Bacarisse

Angel said:
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.
 
A

Angel

Angel said:
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.
 
B

Ben Bacarisse

Angel said:
Angel said:
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.
 
A

Angel

Angel said:
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...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top