Thread: how do i use EOF in a char array?

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    6

    how do i use EOF in a char array?

    Hi,
    I was hoping someone may be able to help me out.
    I am trying to write a program which reads and prints an input of words.
    It needs to hold a max of 10 words but i need use a sentinel contolled loop using the EOF (<ctrlZ> twice) so i can end it early if needed.
    I have tried to use a while loop in scanf unsucessfully in the code below.

    Any help would be greatly appreciated.
    Cheers,
    Mike.

    Code:
    #include <stdio.h>
    #include <string.h>
    main()
    {
    char word[10][100];
    int a, x=0;
    
    
    	for(a=0; a<10; a++)
    		{
    		printf("enter a word\n");
    		scanf("%s", word[x]);
    		x=x+1;
    		}
    		x=0;
    		
    	printf("the word list is:\n");	
    	for(a=0; a<10; a++)
    		{
    		printf("%s\n", word[x]);
    		x=x+1;
    		}
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You really should be reading strings with fgets(), or alter your string format in scanf() to be more secure.

    FYI, control + Z doesn't always read as EOF when you're typing away at a Windows console. It might have to be at the beginning of a line for it to actually count.

    Anyway, the idea of checking for EOF, is to perform a read (by fgets() or scanf()), and then check the return value of the function that is doing the reading. In your case, scanf() should be returning 1 because it has only one variable to assign. If it returns less than 1, you know something went wrong. If the function failed, then check for EOF by means of the feof() function:

    Code:
    if(feof(stdin))
    {
    	/* EOF has occurred. */
    }

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    6
    Hi,
    Still unsure how this works, i do not understand what you mean when you say alter string format in scanf() to be more secure. I am not sure what this code is doing, it does end the program however prints characters to the screen.

    Thanks,
    Mike

    Code:
    #include <stdio.h>
    #include <string.h>
    main()
    {
    char word[10][100];
    int a, x=0;
    
    
    	for(a=0; a<10; a++)
    		{
    		printf("enter a word\n");
    		scanf("&#37;s", word[x]);
    		if(feof(stdin))
    		break;
    		x=x+1;
    		}
    		x=0;
    		
    	printf("the word list is:\n");	
    	for(a=0; a<10; a++)
    		{
    		printf("%s\n", word[x]);
    		x=x+1;
    		}
    }

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    if(feof(stdin))
    break;
    This code checks your standard input stream for EOF dile char. If it finds the EOF file. It breaks the loop and comes.

    Later it prints the string.

    It is basically checking the input stream, thats is your keyboard. You should alse be reading why feof() shoudn't be used for breaking a loop. Read up FAQ.,

    ssharish2005

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    6
    thanks all,
    i'll do some more reading.

    Mike

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It would be easier in your case to check the return value of scanf() to find errors, rather than calling other functions. It returns the number of format specifiers that it successfully read -- so for "&#37;d", it would return 1 for success or 0 or EOF for failure; and for "%d%d" it would return 2, 1, 0, or EOF. You'd use the value like so:
    Code:
    if(scanf("%s", word[x]) != 1) break;
    But it would probably be better to stop using scanf() and use fgets() instead. See this FAQ for more information: http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    6
    thanks guys,

    Huge help!!!

    Mike.

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You should also realize, that EOF doesn't fit into a char

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  2. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  3. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM
  4. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM