an infinite loop with fgetc

This is a discussion on an infinite loop with fgetc within the C Programming forums, part of the General Programming Boards category; Hi, My i/p file is : #A1 aslamas-adlkmaslkmd--adkn- #A2 asdknadnlaksnd-adkmalmd- #A3 askdjn--adlm---- I want to read the file such that ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    141

    an infinite loop with fgetc

    Hi,

    My i/p file is :

    #A1
    aslamas-adlkmaslkmd--adkn-
    #A2
    asdknadnlaksnd-adkmalmd-
    #A3
    askdjn--adlm----

    I want to read the file such that data[0] contains "aslamas-adlkmaslkmd--adkn-" , data[1] contains "asdknadnlaksnd-adkmalmd-" and so on... using the following code:

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    
    int main(){
    
    	  FILE *f;
    	  int i=0;
    	  int j=0;
    	  int k=0;
    	  int n=0;
    	  char ch;
    	  char *buff;
    	  char data[6][2000];
    	  clrscr();
    	  if((f=fopen("test.mul","r"))==NULL)
    	  {
    	  printf("Error\n");
    	  }
    	       while (!(feof(f))){
    		fgets(buff,100,f);
    
    			 while(((ch=fgetc(f))!='#') && ((ch=fgetc(f))!=EOF))
    			 {
    			 ch = fgetc(f);
    			  // printf("%c",ch);
    			   data[i][j++] = ch;
    			 }
    			 data[i][j]='\0';
    			 j=0;
    			 i++;
    		   }
    	    // printf("%d\n",i);
    	     printf("%s",data[1]);
    	    /*
    	    for(k=0;k<i;k++)
    	     printf("%s",data[k]);
    	    */
    	  fclose(f);
    	  return 0;
    
       }
    But my inner while loop is becoming infinite! I wonder why!
    Has it got anything to do with the fgetc? How to overcome it? These phases in the learning stage seem dark...

    and my logic is:

    loop till eof:
    read a line
    read the next line into data till before '#'

    Where is my implementation going wrong?
    Please help.

    AK
    Last edited by AngKar; 01-08-2006 at 11:45 PM.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 03:45 AM
  3. infinite loop with signal chaining
    By zxcv in forum C Programming
    Replies: 1
    Last Post: 04-18-2008, 10:14 AM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM

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