Thread: wrong code to print a string?

  1. #1
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54

    wrong code to print a string?

    Please tell me whats wrong with the code below??

    Code:
    char buff[100];
    FILE* ptr;
    
    while(!feof(ptr)){
    
     fgets(buff, 100, ptr);
     puts(buff);
    }

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    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.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    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

    Code:
    FILE *ptr;
    
    if( ( ptr = fopen( <filename>, <mode> ) ) == NULL )
    {
         printf("Error: File cannot be opened\n");
         return 1;
    }
    
    and then read the file
    Read this on why feof shouldn't be used FEOF

    ssharish

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  5. #5
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54
    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:
    Code:
    int main(void) {
    FILE *ptr;
    char buff[100];
    ptr = fopen("file", "r");
    
    while(!feof(ptr)){
     fgets(buff, 100, ptr);
     puts(buff);
    }
    
    return 0;
    }
    I get:
    Code:
    ELF
    
    
    ibc_start_main
    
    &#255;&#255;&#255;&#37;
    =Y
    &#201;EE&#248;&#255;H&#236;0&#199;E&#208;
    &#207;H     X&#255;H&#235;&#255;T&#221;
    [&#201;&#195;H&#232;&#253;&#255;&#255;H&#195;
    This is total garbage....

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by jas_atwal View Post
    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:
    Code:
    int main(void) {
    FILE *ptr;
    char buff[100];
    ptr = fopen("file", "r");
    
    while(!feof(ptr)){
     fgets(buff, 100, ptr);
     puts(buff);
    }
    
    return 0;
    }
    I get:
    Code:
    ELF
    
    
    ibc_start_main
    
    ÿÿÿ%
    =Y
    ÉEEøÿHì0ÇEÐ
    ÏH     XÿHëÿTÝ
    [ÉÃHèýÿÿHÃ
    This is total garbage....
    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.

  7. #7
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54

    Talking

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

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    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".

    what would you say about a stupid mistake like the one i made above??!!!
    What to say, you made a mistake rectify it! A perhaps check the return value of fopen function. Look at my previous post.

    ssharish

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. checking for a code in a string
    By shin_ono in forum C Programming
    Replies: 13
    Last Post: 10-01-2008, 02:04 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  4. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  5. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM