Re: [algogeeks] Re: file handing
@divya u r correct that in a=(2,3,4) 4 gets in a but if u remove parenthesis then u get 2 in a here although parenthesis is there bt that is for function call i.e f(1,2,3) but effectively its written 1,2,3 so a.c get closed i hope i m clear...plzzz correct me if i m wrong sumwhere -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: file handing
why a.c gets closed. well comma operator has left to right associativity. for eg a=(2,3,4) ; here is a=4 after the statement is executed so similarly why not here. y c.c is not closed here? On 13 June 2010 22:16, souravsain wrote: > For Problem 3 see section "4.2 Using feof() incorrectly" in > http://www.drpaulcarter.com/cs/common-c-errors.php > > On Jun 13, 8:36 pm, divya jain wrote: > > sorry i pasted wrong questn unser 2.. > > > > the real question is > > which file will get closed through fclose() > > #include > > int main() > > { > > FILE *fp,*fs,*ft; > > fp=fopen("a.c","r"); > > fs=fopen("b.c","r"); > > ft=fopen("c.c","r"); > > fclose(fp,fs,ft); > > return 0; > > > > } > > > > 3. yes it is feof..srry typed it wrong... nd fgets(str,80,fp) is > perfectly > > fine.. now the ans to this questn is that last line of the file will be > > printed twice...( which i m unable to get why)...plzz explain... > > > > @ souravsain plzz ignore this mail..srry for the inconvenience.. > > > > On 13 June 2010 17:37, jalaj jaiswal wrote: > > > > > > > > > in question 1... ch gets the value of EOF... so first kicit 44-a > > > gokulpeth\0 nagpur will get printed and then the value of EOF.. > > > > > question number 2 .. seems to me as nrml ...i think myfile.c only gets > > > closed > > > > > in question number 3..it shld be fgets(str,79,fp) > > > > > On Sun, Jun 13, 2010 at 2:49 PM, divya > wrote: > > > > >> 1. wat ll be the o/p. plz explain y? > > >> // abc.c contains "kicit 44-a gokulpeth\0 nagpur" > > >> #include > > >> #include > > >> int main() > > >> { > > >> unsigned char ch; > > >> FILE *fp; > > >> fp=fopen("abc.c","r"); > > >> if(fp==NULL) > > >> { > > >> printf("unable to open the file \n"); > > >> exit(1); > > >> } > > >> while((ch=getc(fp))!=EOF) > > >> printf("%c",ch); > > >> fclose(fp); > > >> printf("\n",ch); > > >> return 0; > > >> } > > > > >> 2.which file will get closed through fclose() in the following > > >> program and why? > > >> #include > > >> int main() > > >> {FILE *fp; > > >> char ch; > > >> int i=1; > > >> fp=fopen(myfile.c","r"); > > >> while((ch=getc(fp)!=EOF)) > > >> { > > >> if(ch=='\n') > > >> i++; > > >> } > > > > >> fclose(fp); > > >> return 0; > > >> } > > > > >> 3.point out the error if any in following > > > > >> #include > > >> int main() > > >> { > > >> FILE *fp; > > >> char str[80]; > > >> fp=fopen("trial","r"); > > >> while(!eof(fp)) > > >> { > > >> fgets(str,80,fp); > > >> puts(str); > > >> } > > >> fclose(fp); > > >> return 0; > > >> } > > > > >> -- > > >> You received this message because you are subscribed to the Google > Groups > > >> "Algorithm Geeks" group. > > >> To post to this group, send email to algoge...@googlegroups.com. > > >> To unsubscribe from this group, send email to > > >> algogeeks+unsubscr...@googlegroups.com > > > >> . > > >> For more options, visit this group at > > >>http://groups.google.com/group/algogeeks?hl=en. > > > > > -- > > > With Regards, > > > Jalaj Jaiswal > > > +919026283397 > > > B.TECH IT > > > IIIT ALLAHABAD > > > > > -- > > > You received this message because you are subscribed to the Google > Groups > > > "Algorithm Geeks" group. > > > To post to this group, send email to algoge...@googlegroups.com. > > > To unsubscribe from this group, send email to > > > algogeeks+unsubscr...@googlegroups.com > > > > . > > > For more options, visit this group at > > >http://groups.google.com/group/algogeeks?hl=en.- Hide quoted text - > > > > - Show quoted text - > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algoge...@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com > . > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: file handing
@Divya For (3) : You are getting last line twice because of wrong usage of feof(fp). Remember , feof() returns true after EOF file is reached and not when EOF is read. feof tells you end of file is reached and it can tell that only after reading EOF and not while reading it !! So after last line is printed , feof(fp) is still false, and then program tries to read from file where it fails ( but no one checks there if fgets(...) fails ) and program prints the old value (i.e. last line ) . You can cross check this by making str null after every print and you would see its not last line but the last value stored by 'str' which is printed. You should ideally do !feof(fp) after fgets() OR use fgets() to check file end by " feof() != NULL OR similar way. Hope it helps. On Jun 13, 8:57 pm, jalaj jaiswal wrote: > declaration of fclose in stdio.h is as fclose (FILE*); > so it has too many arguments... > On Sun, Jun 13, 2010 at 9:06 PM, divya jain wrote: > > > > > sorry i pasted wrong questn unser 2.. > > > the real question is > > > which file will get closed through fclose() > > #include > > int main() > > { > > FILE *fp,*fs,*ft; > > fp=fopen("a.c","r"); > > fs=fopen("b.c","r"); > > ft=fopen("c.c","r"); > > fclose(fp,fs,ft); > > return 0; > > } > > > 3. yes it is feof..srry typed it wrong... nd fgets(str,80,fp) is perfectly > > fine.. now the ans to this questn is that last line of the file will be > > printed twice...( which i m unable to get why)...plzz explain... > > > @ souravsain plzz ignore this mail..srry for the inconvenience.. > > > On 13 June 2010 17:37, jalaj jaiswal wrote: > > >> in question 1... ch gets the value of EOF... so first kicit 44-a > >> gokulpeth\0 nagpur will get printed and then the value of EOF.. > > >> question number 2 .. seems to me as nrml ...i think myfile.c only gets > >> closed > > >> in question number 3..it shld be fgets(str,79,fp) > > >> On Sun, Jun 13, 2010 at 2:49 PM, divya wrote: > > >>> 1. wat ll be the o/p. plz explain y? > >>> // abc.c contains "kicit 44-a gokulpeth\0 nagpur" > >>> #include > >>> #include > >>> int main() > >>> { > >>> unsigned char ch; > >>> FILE *fp; > >>> fp=fopen("abc.c","r"); > >>> if(fp==NULL) > >>> { > >>> printf("unable to open the file \n"); > >>> exit(1); > >>> } > >>> while((ch=getc(fp))!=EOF) > >>> printf("%c",ch); > >>> fclose(fp); > >>> printf("\n",ch); > >>> return 0; > >>> } > > >>> 2.which file will get closed through fclose() in the following > >>> program and why? > >>> #include > >>> int main() > >>> {FILE *fp; > >>> char ch; > >>> int i=1; > >>> fp=fopen(myfile.c","r"); > >>> while((ch=getc(fp)!=EOF)) > >>> { > >>> if(ch=='\n') > >>> i++; > >>> } > > >>> fclose(fp); > >>> return 0; > >>> } > > >>> 3.point out the error if any in following > > >>> #include > >>> int main() > >>> { > >>> FILE *fp; > >>> char str[80]; > >>> fp=fopen("trial","r"); > >>> while(!eof(fp)) > >>> { > >>> fgets(str,80,fp); > >>> puts(str); > >>> } > >>> fclose(fp); > >>> return 0; > >>> } > > >>> -- > >>> You received this message because you are subscribed to the Google Groups > >>> "Algorithm Geeks" group. > >>> To post to this group, send email to algoge...@googlegroups.com. > >>> To unsubscribe from this group, send email to > >>> algogeeks+unsubscr...@googlegroups.com > >>> . > >>> For more options, visit this group at > >>>http://groups.google.com/group/algogeeks?hl=en. > > >> -- > >> With Regards, > >> Jalaj Jaiswal > >> +919026283397 > >> B.TECH IT > >> IIIT ALLAHABAD > > >> -- > >> You received this message because you are subscribed to the Google Groups > >> "Algorithm Geeks" group. > >> To post to this group, send email to algoge...@googlegroups.com. > >> To unsubscribe from this group, send email to > >> algogeeks+unsubscr...@googlegroups.com > >> . > >> For more options, visit this group at > >>http://groups.google.com/group/algogeeks?hl=en. > > > -- > > You received this message because you are subscribed to the Google Groups > > "Algorithm Geeks" group. > > To post to this group, send email to algoge...@googlegroups.com. > > To unsubscribe from this group, send email to > > algogeeks+unsubscr...@googlegroups.com > > . > > For more options, visit this group at > >http://groups.google.com/group/algogeeks?hl=en. > > -- > With Regards, > Jalaj Jaiswal > +919026283397 > B.TECH IT > IIIT ALLAHABAD -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: file handing
For Problem 3 see section "4.2 Using feof() incorrectly" in http://www.drpaulcarter.com/cs/common-c-errors.php On Jun 13, 8:36 pm, divya jain wrote: > sorry i pasted wrong questn unser 2.. > > the real question is > which file will get closed through fclose() > #include > int main() > { > FILE *fp,*fs,*ft; > fp=fopen("a.c","r"); > fs=fopen("b.c","r"); > ft=fopen("c.c","r"); > fclose(fp,fs,ft); > return 0; > > } > > 3. yes it is feof..srry typed it wrong... nd fgets(str,80,fp) is perfectly > fine.. now the ans to this questn is that last line of the file will be > printed twice...( which i m unable to get why)...plzz explain... > > @ souravsain plzz ignore this mail..srry for the inconvenience.. > > On 13 June 2010 17:37, jalaj jaiswal wrote: > > > > > in question 1... ch gets the value of EOF... so first kicit 44-a > > gokulpeth\0 nagpur will get printed and then the value of EOF.. > > > question number 2 .. seems to me as nrml ...i think myfile.c only gets > > closed > > > in question number 3..it shld be fgets(str,79,fp) > > > On Sun, Jun 13, 2010 at 2:49 PM, divya wrote: > > >> 1. wat ll be the o/p. plz explain y? > >> // abc.c contains "kicit 44-a gokulpeth\0 nagpur" > >> #include > >> #include > >> int main() > >> { > >> unsigned char ch; > >> FILE *fp; > >> fp=fopen("abc.c","r"); > >> if(fp==NULL) > >> { > >> printf("unable to open the file \n"); > >> exit(1); > >> } > >> while((ch=getc(fp))!=EOF) > >> printf("%c",ch); > >> fclose(fp); > >> printf("\n",ch); > >> return 0; > >> } > > >> 2.which file will get closed through fclose() in the following > >> program and why? > >> #include > >> int main() > >> {FILE *fp; > >> char ch; > >> int i=1; > >> fp=fopen(myfile.c","r"); > >> while((ch=getc(fp)!=EOF)) > >> { > >> if(ch=='\n') > >> i++; > >> } > > >> fclose(fp); > >> return 0; > >> } > > >> 3.point out the error if any in following > > >> #include > >> int main() > >> { > >> FILE *fp; > >> char str[80]; > >> fp=fopen("trial","r"); > >> while(!eof(fp)) > >> { > >> fgets(str,80,fp); > >> puts(str); > >> } > >> fclose(fp); > >> return 0; > >> } > > >> -- > >> You received this message because you are subscribed to the Google Groups > >> "Algorithm Geeks" group. > >> To post to this group, send email to algoge...@googlegroups.com. > >> To unsubscribe from this group, send email to > >> algogeeks+unsubscr...@googlegroups.com > >> . > >> For more options, visit this group at > >>http://groups.google.com/group/algogeeks?hl=en. > > > -- > > With Regards, > > Jalaj Jaiswal > > +919026283397 > > B.TECH IT > > IIIT ALLAHABAD > > > -- > > You received this message because you are subscribed to the Google Groups > > "Algorithm Geeks" group. > > To post to this group, send email to algoge...@googlegroups.com. > > To unsubscribe from this group, send email to > > algogeeks+unsubscr...@googlegroups.com > > . > > For more options, visit this group at > >http://groups.google.com/group/algogeeks?hl=en.- Hide quoted text - > > - Show quoted text - -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: file handing
Guys Lets keep discussions in t his group limited to Algos and problems neutral to any language. @divya: Request you to post these C++ language specific questions to those language specific groups. This will not only help this group remain confined to its core purpose but will help you get better insights to ur problem by language specific geeks there. For C++ I would recommend you to try the group http://groups.google.co.in/group/comp.lang.c++.moderated/topics?hl=en. Regards, Sourav On Jun 13, 5:32 pm, BALARUKESH SIVARAMAN wrote: > For the 1st qn.. > the o/p will print the code in the file and then print an infinite sequence > of empty spaces. the reason is... > In C EOF is defined to hold a value -1 which when assigned to unsigned > becomes 255. So it goes in an unending loop even after encountering the end > of file. > > In second qn.. > the file myfile.c will be closed. > > In third qn... > It is not eof(fp) it is feof(fp) . There is no function eof defined in > stdio.h. Use the correct function for files -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.