Thread: beginner question about stopping a for-loop.

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    2

    beginner question about stopping a for-loop.

    I am currenty writing a program that needs to read in an unknown number of integers from a file and sort them using selection sort (with a max 100,000 integers). Here is my code so far:

    Code:
    #include <stdio.h>
    
    void selectionsort ( int x[], int size ) ;
    
    int main ( int argc, char *argv[] ) {
    
      int x[100000], i ;
    
      FILE *fin = fopen ( argv [1], "r" ) ;
    
      for ( i = 0 ; i < 100000 ; i++ ) {
    
        fscanf ( fin, "%d", &x[i] ) ;
    
      }
    
      selectionsort ( x, i ) ;
    
      printf ( "\nThe first five sorted numbers are: %d %d %d %d %d\n", x[0], x[1], x[2], x[3], x[4] ) ;
    
      printf ( "The last five sorted numbers are: %d %d %d %d %d\n\n", x[i-5], x[i-4], x[i-3], x[i-2], x[i-1] ) ;
    
      fclose ( fin ) ;
    
      return 0 ;
    }
    
    void selectionsort ( int x[], int size ) {
    
      int i, j, min_index, temp ;
    
      for ( i = 0 ; i < size - 1 ; i++ ) {
    
        min_index = i ;
    
        for ( j = i + 1 ; j < size ; j++ ) {
    
    	if ( x[j] < x[min_index] ) min_index = j ;
    
        }
    
        temp = x[i];
    
        x[i] = x[min_index] ;
    
        x[min_index] = temp ;
    
      }
    
    }
    Now my question is, how would I go about stopping the for-loop in main when fscanf starts scanning just blanks. I tried adding " if ( x[ i ] == ' ' ) break ; " after the fscanf but it doesn't work. What would I need to add so it stops scanning the file when it runs out of numbers?

    Thank you very much

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Study the return result(s) of fscanf

    Then break; out of the loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    2
    Never mind, I figured it out. I just initialized x[i] to be \0, then when fscanf scanned a \0 it broke out of the loop.
    Last edited by Techboy10; 12-11-2008 at 04:31 PM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Techboy10 View Post
    Never mind, I figured it out. I just initialized x[i] to be \0, then when fscanf scanned a \0 it broke out of the loop.
    That means that the value 0 is not allowed in the numbers - is that how it will always be?

    Yes, the scanf() family of functions do not modify the data when an error happens while reading the data, but like Salem suggests, check if the return value from fscanf() is what you expect, as it returns the number of items successfully read, so you expect 1 in this case - if not, its a failure to read the data. That could be "rubbish" (such as "abc" for a number) or the end of the file (in which case you get EOF back - but it's better to check for "not equal to the number of items I expect")

    --
    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. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 06:51 AM
  2. A very beginner question!
    By sifeet in forum C Programming
    Replies: 8
    Last Post: 11-06-2007, 07:13 AM
  3. General question from a beginner to Windows C++ programming
    By Kontan in forum Windows Programming
    Replies: 1
    Last Post: 09-29-2006, 08:03 PM
  4. Question About External Files (Beginner)
    By jamez05 in forum C Programming
    Replies: 0
    Last Post: 08-11-2005, 07:05 AM
  5. Beginner on Win32 apps, lame question.
    By Templario in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 08:39 PM