Please tell me whats wrong with the code below??
Code:char buff[100]; FILE* ptr; while(!feof(ptr)){ fgets(buff, 100, ptr); puts(buff); }
Please tell me whats wrong with the code below??
Code:char buff[100]; FILE* ptr; while(!feof(ptr)){ fgets(buff, 100, ptr); puts(buff); }
You're passing ptr to feof() without being initialized.
If you would have turned on all your compiler warnings and it would have told you the same thing.
The first mistake which you have here is using feof function and i cant really see anywhere that you are trying to read something from a file. If you are trying to read from a file you need to open a file first
Read this on why feof shouldn't be used FEOFCode:FILE *ptr; if( ( ptr = fopen( <filename>, <mode> ) ) == NULL ) { printf("Error: File cannot be opened\n"); return 1; } and then read the file
ssharish
It would of course help if you helped us define what you mean "wrong".
Well, first of all, the "ptr" variable is never assigned a value in this code-segment, so if this is a standalone piece of code, I would expect a segmentation fault or "application attempted to perform an illegal read" or whatever other symptoms the system shows when you try to access an uninitialized pointer.
If we assume this is done correctly elsewhere [in some not yet published part of the larger whole], then it's still broken because it uses feof() to check for end of file, THEN reads from the file, and prints the result without again checking if we could read anything or if we hit the "brick wall at the end of the file".
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I am so sorry for this putting this question so vaguely and in a dumb manner: Let me say this again:
I have the following code:
I get:Code:int main(void) { FILE *ptr; char buff[100]; ptr = fopen("file", "r"); while(!feof(ptr)){ fgets(buff, 100, ptr); puts(buff); } return 0; }
This is total garbage....Code:ELF ibc_start_main ÿÿÿ% =Y ÉEEøÿHì0ÇEÐ ÏH XÿHëÿTÝ [ÉÃHèýÿÿHÃ
Yes, you are printing a binary file [on a post-Linux 2.0 /Mac OS-X Apple system, as it appears]
I don't think there's that much wrong with your code as such, just open a file that isn't an executable, and you should be fine - e.g. bubblesort.c should work fine.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Thanks Mat.... now another question...
what would you say about a stupid mistake like the one i made above??!!!
And..... how do you know i am using bubblesort. i haven't mentioned the filename here... have i??
Well just few min before, i saw that you had bubblesort in your filename field in your fopen function. And now you have changed it to "file".And..... how do you know i am using bubblesort. i haven't mentioned the filename here... have i??
What to say, you made a mistake rectify it! A perhaps check the return value of fopen function. Look at my previous post.what would you say about a stupid mistake like the one i made above??!!!
ssharish
It's a "stupid mistake" allright, but one that I think many of us have done - I have certainly done the similar "cat name-of-some-binary-file" in Linux more than once - there is little in the way of "protecting yourself" from this mistake - the compiler certainly don't know what you are doing, and fopen() can't really know how you are going to use the file, so it just goes ahead and opens the file.
Just make sure you type in the right filename next time.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.