Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > get string from file

Reply
Thread Tools

get string from file

 
 
Rudra Banerjee
Guest
Posts: n/a
 
      09-16-2012
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 ?
 
Reply With Quote
 
 
 
 
Heinrich Wolf
Guest
Posts: n/a
 
      09-16-2012

"Rudra Banerjee" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> 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?

 
Reply With Quote
 
 
 
 
Rudra Banerjee
Guest
Posts: n/a
 
      09-16-2012
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);
}

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      09-16-2012
Rudra Banerjee <(E-Mail Removed)> 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.
 
Reply With Quote
 
Rudra Banerjee
Guest
Posts: n/a
 
      09-16-2012


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

This one is nice and it did the trick.
 
Reply With Quote
 
Johann Klammer
Guest
Posts: n/a
 
      09-16-2012
ayear[6][4] seems a bit short and might overflow(4digits +terminating
'\0' char).
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      09-16-2012
Rudra Banerjee <(E-Mail Removed)> 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.
 
Reply With Quote
 
Heinrich Wolf
Guest
Posts: n/a
 
      09-16-2012

"Rudra Banerjee" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
....
> 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.
....

 
Reply With Quote
 
Rudra Banerjee
Guest
Posts: n/a
 
      09-16-2012
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.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      09-16-2012
Rudra Banerjee <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How to get the file name from a String containing the path plus the file name? Mike Schilling Java 2 03-05-2010 03:53 AM
INPUT Type="FILE" - Just want to get the file name and path as string kevin@webpageconcepts.com ASP .Net 4 08-02-2006 05:47 PM
using std::string; string("hello") vs std::string("hello") in header file. Fei Liu C++ 9 04-01-2006 08:49 AM
Re: String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 0 12-04-2003 04:40 PM



Advertisments