Thread: why this while condition doesnt stop the loop??

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    why this while condition doesnt stop the loop??

    i told it that when it reaches EOF then stop but it keeps putting 005 over and over ...
    why ??
    it should get out of the loop after it wrote 005
    ???

    file1
    000
    001
    002
    003
    005

    file2
    003
    004
    005

    file3
    005
    006
    007


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define false 0
    #define true 1
    
    int equal(char*f1,char*f2,char*f3);
      int smallest(char*f1,char*f2,char*f3);
    int main()
    {
    
       FILE *file[3];
       FILE *common;
       
       char f[3][20];
        
       int small;
        int equall;    
       file[0]=fopen("g:\\1.txt","r");
       file[1]=fopen("g:\\2.txt","r");
       file[2]=fopen("g:\\3.txt","r");
       common=fopen("g:\\common.txt","w");
       fscanf(file[0],"%9s",f[0]);
       fscanf(file[1],"%9s",f[1]);
       fscanf(file[2],"%9s",f[2]);
       while(fgetc(file[0])!=EOF||fgetc(file[1])!=EOF||fgetc(file[2])!=EOF)
       {
           small=smallest(f[0],f[1],f[2]);
           equall=equal(f[0],f[1],f[2]);
          if(equall!=-1)
    	  {
            fputs(f[equall],common);
            fscanf(file[equall],"%20s",f[equall]);
    		fputc('\n',common);
    	  }
    
          
          fscanf(file[small],"%20s",f[small]);
       } 
    
       fclose(file[1]);
       fclose(file[2]);
       fclose(file[0]);
       return 0;
    }
    
    int smallest(char* f_a,char* f_b,char* f_c)
    {
         int i=0;
        char* pLowest = f_a;
         if(strcmp(pLowest, f_b) > 0)
        {   
           i=1;
          pLowest = f_b;
        }
          if(strcmp(pLowest, f_c) > 0)
         {
           pLowest = f_c;
         i=2;
         }
          return i;
    }
    
      int equal(char* f1,char*f2,char*f3)
      {
          
         if(strcmp(f1,f3)==0)
        {
           return  0;
        }
        if(strcmp(f1,f2)==0)
        {
           return  1;
        }
         if(strcmp(f2,f3)==0)
        {
           return  2;
        }
         return -1;
      }
    Last edited by transgalactic2; 03-30-2009 at 05:41 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  2. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  3. Replies: 26
    Last Post: 06-15-2005, 02:38 PM
  4. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM
  5. 2 largest elements; -1 to stop loop
    By Peachy in forum C Programming
    Replies: 4
    Last Post: 09-16-2001, 05:16 AM