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
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[i], sizeof aauth[i], fauth))
printf("%d%s\n",i,aauth);
i++;
fclose(fauth);
}
for (i=0;i<6;i++){
printf("%d%s\n",i,aauth[i]);
}
return(0);
}
 
Reply With Quote
 
 
 
 
Angel
Guest
Posts: n/a
 
      09-16-2012
On 2012-09-16, Rudra Banerjee <(E-Mail Removed)> 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[i], sizeof aauth[i], 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
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      09-16-2012
Rudra Banerjee <(E-Mail Removed)> 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[i], sizeof aauth[i], 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[i]);
> }
> 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.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      09-16-2012
Angel <(E-Mail Removed)> writes:

> On 2012-09-16, Rudra Banerjee <(E-Mail Removed)> 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[i], sizeof aauth[i], 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.
 
Reply With Quote
 
Angel
Guest
Posts: n/a
 
      09-16-2012
On 2012-09-16, Ben Bacarisse <(E-Mail Removed)> wrote:
> Angel <(E-Mail Removed)> writes:
>
>> On 2012-09-16, Rudra Banerjee <(E-Mail Removed)> 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[i], sizeof aauth[i], 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
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      09-16-2012
Angel <(E-Mail Removed)> writes:

> On 2012-09-16, Ben Bacarisse <(E-Mail Removed)> wrote:
>> Angel <(E-Mail Removed)> writes:
>>
>>> On 2012-09-16, Rudra Banerjee <(E-Mail Removed)> 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[i], sizeof aauth[i], 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.
 
Reply With Quote
 
Angel
Guest
Posts: n/a
 
      09-17-2012
On 2012-09-16, Ben Bacarisse <(E-Mail Removed)> wrote:
> Angel <(E-Mail Removed)> writes:
>
>> On 2012-09-16, Ben Bacarisse <(E-Mail Removed)> wrote:
>>> Angel <(E-Mail Removed)> writes:
>>>
>>>> On 2012-09-16, Rudra Banerjee <(E-Mail Removed)> 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[i], sizeof aauth[i], 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
 
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