Like Tree1Likes

Seem unable to use EOF?

This is a discussion on Seem unable to use EOF? within the C Programming forums, part of the General Programming Boards category; Okay, I tried rewriting this piece of code with getchar in the while loop to manually end the loop with ...

  1. #1
    Registered User black_stallion's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    22

    Question Seem unable to use EOF?

    Okay, I tried rewriting this piece of code with getchar in the while loop to manually end the loop with EOF(Ctrl+Z)(I'm using Windows) It worked.

    However, any reasons as to why this doesn't work?

    Code:
    #include<stdio.h>
    
    int main (void)
    {
        int data = 10;
        while (data!= EOF)
        {
           scanf ("%d", &data);
           printf("%d",data);
        }
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,659
    scanf() will never set data to be EOF. You need read the documentation about scanf() carefully (scanf() does not necessarily return EOF if an error occurs).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    77
    hi,
    you want to read a file but you didnt opened a file, you didnt closed it and you didnt read this file with codes such as fgets,fgetc,fscanf
    please look at file reading topics.
    EOF means End of FILE

    if you need the search the ending of an array or string, you should use
    Code:
    for(i=0;my_array[i]!='\0';i++) length++;
    you can use it for finding how many letters is there in my_array.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    black_stallion and rac1: Read the documentation on scanf:

    In the case of an input failure before any data could be successfully read, EOF is returned.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by rac1 View Post
    hi,
    you want to read a file but you didnt opened a file, you didnt closed it and you didnt read this file with codes such as fgets,fgetc,fscanf
    please look at file reading topics.
    EOF means End of FILE

    if you need the search the ending of an array or string, you should use
    Code:
    for(i=0;my_array[i]!='\0';i++) length++;
    you can use it for finding how many letters is there in my_array.
    *sigh* another poor code example...

    1) You don't need two separate counter variables there. You could just do:
    Code:
    for(length=0; my_array[length]!='\0'; length++) {}
    2) Stop reinventing the wheel, the above is exactly the same as strlen.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Take out the scanf statement, and assign data to getchar() inside the while conditional.

    Code:
    #include <stdio.h>
    
    int main(void) {
        int data;
        while ((data = getchar()) != EOF)
            putchar(data);
        return 0;
    }
    Why did you assign data to 10 before the while loop? That seems kinda unnecessary.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,659
    That changes the meaning of the code, completely, Babkockdood.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Location
    Douala, Cameroon
    Posts
    195

    code is okay

    Quote Originally Posted by Babkockdood View Post
    Take out the scanf statement, and assign data to getchar() inside the while conditional.
    Code:
    #include   int main(void) {     int data;     while ((data = getchar()) != EOF)         putchar(data);     return 0; }
    Why did you assign data to 10 before the while loop? That seems kinda unnecessary.
    i think the code is okay like this and easier to understand probably faster.

  9. #9
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Quote Originally Posted by Nyah Check View Post
    i think the code is okay like this and easier to understand probably faster.
    It probably is faster but as grumpy said the two pieces of code do not do the same thing.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    Quote Originally Posted by Babkockdood View Post
    Why did you assign data to 10 before the while loop? That seems kinda unnecessary.
    you need to assign something to 'data' or it will contain random data and it might be EOF by chance.

  11. #11
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by dmh2000 View Post
    you need to assign something to 'data' or it will contain random data and it might be EOF by chance.
    a. The odds of it being EOF are incredibly slim, and b. Why doesn't he just assign it to 0?
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  12. #12
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Quote Originally Posted by Babkockdood View Post
    a. The odds of it being EOF are incredibly slim
    No they are not. When a variable is declared it contains whatever value was in that memory space beforehand. -1 is a pretty common number and previously run applications could easily have set that memory location to -1. Still slim but not as slim as you think.

    Also, why not use this logic with commercial applications? Take the worst possible odds and say that the odds of the memory space being -1 are 1 in however large an integer is. Your commercial application is distributed to millions of people, all who run this application many times. What are the chances that 1 of them encounters this error?

    Bad practises are bad practises.

    Quote Originally Posted by Babkockdood View Post
    b. Why doesn't he just assign it to 0?
    He liked 10 better? To be honest EOF can be any number so the best solution would be to set it to EOF + 1 or something of the sort. (actually the best solution would be to use a do...while() loop, but barring that this would do).
    iMalc likes this.

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    Quote Originally Posted by Babkockdood View Post
    a. The odds of it being EOF are incredibly slim, and b. Why doesn't he just assign it to 0?
    I agree on b. but a. is asking for a Heisenbug

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Babkockdood View Post
    a. The odds of it being EOF are incredibly slim,
    One in 256. Or 50/50 if he runs the program twice in a row...

  15. #15
    Registered User
    Join Date
    Nov 2011
    Posts
    63
    Quote Originally Posted by CommonTater View Post
    One in 256. Or 50/50 if he runs the program twice in a row...
    Um, EOF is an int. So assuming an int is 32 bits, then wouldn't the percent chance of EOF showing up by default be (2^-32)(100)? This, of course, isn't taking into account how often programs store or use the value EOF.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unable to print
    By nehal in forum C Programming
    Replies: 2
    Last Post: 03-09-2011, 08:01 AM
  2. unable to remember
    By Pulock2009 in forum C Programming
    Replies: 6
    Last Post: 05-08-2010, 12:57 PM
  3. unable to write to a file
    By roaan in forum C Programming
    Replies: 2
    Last Post: 08-07-2009, 09:18 AM
  4. Unable to use string
    By Suchy in forum C++ Programming
    Replies: 9
    Last Post: 09-26-2006, 02:24 PM
  5. unable to set classpath
    By rachel in forum C++ Programming
    Replies: 2
    Last Post: 08-28-2001, 12:51 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21