Thread: Linked List Part 2

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    18

    Linked List Part 2

    Hi,

    For the second part of my code (Part 1 is titled Linked List and Array of POinters) I need to fill in the Linked List with values for the line_used from a file. The file has the following format:

    Array size: 17 x 17 logic blocks.

    Routing:

    Net 0 (i_63_)

    SOURCE (18,1) Pad: 0
    OPIN (18,1) Pad: 0
    CHANY (17,1) Track: 2
    CHANX (17,1) Track: 3
    IPIN (17,1) Pin: 2
    SINK (17,1) Class: 0
    CHANX (17,1) Track: 2
    CHANY (16,2) Track: 4
    IPIN (16,2) Pin: 3
    SINK (16,2) Class: 0


    Hence I need to read the Channel X or Y and the coordinates and the corresponding track number. Depending on the Channel (X or Y ) and the coordinates I have to fill up the linked list.
    The logic is illlustrated in my code but when I run the program on VC++ I get an error message that the exe file has encountered an error. When I try to debug it says that it cannot evaluate the line_used expression and the temp=temp->nextptr expression.

    Also further I get the following errors on debugging

    Loaded 'C:\WINDOWS\system32\ntdll.dll', no matching symbolic information found.
    Loaded 'C:\WINDOWS\system32\kernel32.dll', no matching symbolic information found.
    Loaded 'C:\WINDOWS\system32\version.dll', no matching symbolic information found.
    Loaded 'C:\WINDOWS\system32\msvcrt.dll', no matching symbolic information found.
    Loaded 'C:\WINDOWS\system32\advapi32.dll', no matching symbolic information found.
    Loaded 'C:\WINDOWS\system32\rpcrt4.dll', no matching symbolic information found.
    Loaded 'C:\WINDOWS\system32\user32.dll', no matching symbolic information found.
    Loaded 'C:\WINDOWS\system32\gdi32.dll', no matching symbolic information found.
    Loaded 'C:\WINDOWS\system32\shlwapi.dll', no matching symbolic information found.
    Loaded 'C:\WINDOWS\system32\apphelp.dll', no matching symbolic information found.
    The thread 0x7E8 has exited with code 0 (0x0).
    The thread 0xF88 has exited with code -1073741819 (0xC0000005).


    On GCC I simply get a Segmentation Fault.

    I am again all confused where to try looking for tthe problem.
    I tried printing out the tracknumber and coordinates also. They do print if I comment the part from **HERE** to **HERE**

    I am attaching the code below:

    Code:
    #include<stdio.h>
    #include<stdlib.h.>
     
    struct switch_matrix
    {
     
      int pin_no;
      int line_used;
      struct switch_matrix *nextptr;
     
    };
     
    struct switch_block
    {
     
      struct switch_matrix *Array[40];
     
    }Layout[18][18];
     
     
    typedef struct switch_matrix SWITCH;
     
    void build_univ_switch (struct switch_block *X);
     
     
     
     
    int main()
    {
      int x1=0,x2=0,y1=0,y2=0,trackno1=0,trackno2=0,m,n;
      FILE *fp;
      SWITCH *temp;
      char ch;
      
     
    fp=fopen("e64.r","r");
      while(!feof(fp))
        { 
       **HERE**
          if (x1==x2)
            {
              if (y1==y2)
                { 
                  temp=Layout[17-y1][x1].Array[19+trackno2];
                  temp=temp->nextptr;
                  temp->line_used=1; 
                           
                  temp=Layout[17-y1][x1].Array[19-trackno1];
                  temp=temp->nextptr; 
                  temp=temp->nextptr; 
                  temp->line_used=1; 
                } 
                         
              else   
                {
                  if (y2==y1+1)
                    {
                      temp=Layout[17-y1][x1].Array[19-trackno1];
                      temp->line_used=1;
                 
                      temp=Layout[17-y1][x1].Array[9-trackno2];
                      temp=temp->nextptr;
                      temp->line_used=1; 
                    } 
                  else   
                    {
                      if (y2 > y1+1) 
                        { 
                          temp=Layout[17-(y2-1)][x1].Array[19+trackno2];
                          temp->line_used=1;
                   
                          temp=Layout[17-(y2-1)][x1].Array[9-trackno2];
                          temp=temp->nextptr;
                          temp=temp->nextptr;
                          temp->line_used=1;
                        }
                    }
                }
            } 
          else 
            { 
              if (y1==y2) 
                {
                  if (x1==x2+1) 
                    { 
                      temp=Layout[17-y2][x2].Array[19+trackno2];
                      temp=temp->nextptr;
                      temp=temp->nextptr;
                      temp=temp->nextptr;
                      temp->line_used=1;
                          
                      temp=Layout[17-y2][x2].Array[29+trackno1]; 
                      temp=temp->nextptr;   
                      temp=temp->nextptr;
                      temp->line_used=1; 
                    } 
                  else 
                    { 
                      if (x1 > x2+1)
                        {
                          temp=Layout[17-y1][x1-1].Array[19-trackno1];
                          temp=temp->nextptr;
                          temp=temp->nextptr;
                          temp=temp->nextptr;
                          temp->line_used=1;
                 
                          temp=Layout[17-y1][x1-1].Array[29+trackno1];
                          temp=temp->nextptr;
                          temp->line_used=1; 
                        } 
                    } 
                } 
              else 
                {         
                  if (y2==y1+1) 
                    { 
                      temp=Layout[17-y1][x2].Array[9-trackno2];
                      temp=temp->nextptr;
                      temp=temp->nextptr;
                      temp=temp->nextptr;
                      temp->line_used=1;
                       
                      temp=Layout[17-y1][x2].Array[29+trackno1];
                      temp->line_used=1; 
                    } 
                }
    			  
            } **HERE**
    	  
    
    		
                           
                 
          ch=fgetc(fp); 
          if (ch=='S') 
            { 
              x1 = 0; 
              y1 = 0; 
              x2 = 0;
              y2 = 0;
                          
              ch=fgetc(fp); 
              if(ch=='C')
                { 
                  ch=fgetc(fp); 
                  if(ch=='H') 
                    { 
                      ch=fgetc(fp); 
                      if(ch=='A')
                        { 
                          ch=fgetc(fp);  
                          if(ch=='N')
                            {
                              ch=fgetc(fp);
                              if(ch=='X')
                                {
                                  ch=fgetc(fp);//the blank
                                  //ch=fgetc(fp);//the colon
                                  ch=fgetc(fp);//the (
                                  fscanf(fp,"%d",&x1);
                                  printf("%d\n",x1); 
                                  ch=fgetc(fp);//the ,
                                  fscanf(fp,"%d",&y1); 
                                  printf("%d\n",y1); 
                                  for(m=0;m<10;m++)//no of charac till u reach track no
                                    ch=fgetc(fp);
                                  fscanf(fp,"%d",&trackno1); 
                                  printf("go\n");
                                  printf("%d\n",trackno1);
                         
                       
                                  //store these in the linked list
                                }
                              else if (ch=='Y')
                                {
                                  ch=fgetc(fp);//the blank
                                  //ch=fgetc(fp);//the colon
                                  ch=fgetc(fp);//the (
                                  fscanf(fp,"%d",&x2);
                                  printf("%d\n",x2);
                                  ch=fgetc(fp);//the ,
                                  fscanf(fp,"%d",&y2);
                                  printf("%d\n",y2);
                                  for(n=0;n<10;n++)//no of charac till u reach track no
                                    ch=fgetc(fp); 
                                  fscanf(fp,"%d",&trackno2);
                                  printf("%d\n",trackno2);
                                   
                                     
                                  //store these in the linked list
                                } 
                            } 
                        }
                    }  
                } 
            } 
                               
        } 
      fclose(fp); 
                                   
      return 0; 
    } 
                                   
    void build_univ_switch(struct switch_block *X) 
    { 
      SWITCH *head; 
      SWITCH *currentptr; 
      int i, j; 
                                   
                                   
                                   
                                     
      for (i = 0; i<40; i++) 
        { 
                              
          /*BUILDS LINKED LIST*/
          head = NULL; 
          for (j = 1; j<=4; j++)
            { 
              currentptr = (SWITCH *) malloc (sizeof (SWITCH));
              currentptr->line_used = 0;
              currentptr->nextptr = head;
              head = currentptr;   
            }   
          //currentptr = head;
          X->Array[i] = head;      
        } 
      
    }

    I am open to any help as I can get regarding this.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Use more functions - that main() is so horribly complicated that it's too hard to figure out what is going on.
    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
    Mar 2005
    Posts
    18

    Linked List Part 2

    Hi Salem,
    I suppose my function would contain the same code as my main. I was thinkning of writing a function once this part works but as it is not working I did not go on to writing a function.

    Nish.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well then you'd be doing it wrong. You shouldn't make functions that "do all". You should make a function be just that a function. That means you make it do one thing. A single function. For example, maybe you make a function that reads one chunk of data from a file, and returns it. Or maybe you make a function that parses that chunk of data into something usable. Or a function to delete your list when you're done with it. Things like that.

    Break any given task down into simpler and simpler chunks.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > I was thinkning of writing a function once this part works
    It's generally easier to make a function to make it work, not make a function once it works.

    Your code is too complicated as it stands, which is it's singular problem at the moment. Because it is too complicated, you can't see what needs to be done.

    Create some functions and simplify!
    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.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    18

    Linked List Part 2

    Ok if I could get a little help.
    Is it possible that I use one function that fills my list and one that reads the file and within the function that reads my file I call the function to fill my list.
    Does this make sense.
    Do let me know.
    Thanks.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sounds like a good way to do it to me.
    Code:
    somefunction( )
    {
        while not done
            readsomdatafromfile( )
            putsomedatainlist( )
    
    }
    Sure. sounds fine. You just need to think of how you would break it up into managable pieces. It's much much easier to debug one function at a time than it is to try and get one massive piece of code working.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    18

    Question

    Hi,
    I came up with this.
    I need to fill the list depending on the values of x1,x2,y1 etc and need to do the comparison everytime I get a new values for them.
    Hence I out them at the start of the function so that the comparison and list filling occurs before reading the next values of x1 etc.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    struct switch_matrix  
    {
      
      int pin_no;             
      int line_used;           
      struct switch_matrix *nextptr;
      
    };
    
    struct switch_block
    {
      
      struct switch_matrix *Array[40];
      
    }Layout[18][18];
    
    
    typedef struct switch_matrix SWITCH;
    
    void build_univ_switch (struct switch_block *X);
    
    void fill_list (int a1, int a2, int b1, int b2, int t1, int t2);
    
    void file_read (FILE *file_pointer); 
    
    
    
    
    
    
    int main ()
    {
    	FILE *fp;
    
    	fp = fopen("e64.r","r");
    	file_read (fp);
    	fclose(fp);
    	return (0);
    }
    
    
    
    void file_read (FILE *file_pointer)
    {
      int x1=0,x2=0,y1=0,y2=0,trackno1=0,trackno2=0,m,n;
      char ch;
      while(!feof(file_pointer))
    	  {
        
          fill_list (x1, x2, y1, y2, trackno1, trackno2);
          
          ch=fgetc(file_pointer);
          if (ch=='S')
    		  {
    			x1 = 0;
    			y1 = 0;
    			x2 = 0;
    			y2 = 0;
    	  
    			ch=fgetc(file_pointer);
    			if(ch=='C')
    				{
    					ch=fgetc(file_pointer); 
    					if(ch=='H')
    						{
    							ch=fgetc(file_pointer);
    							if(ch=='A')
    								{
    									ch=fgetc(file_pointer);
    									if(ch=='N')
    										{
    											ch=fgetc(file_pointer);
    											if(ch=='X')
    												{
    													ch=fgetc(file_pointer);//the blank
    			      
    													ch=fgetc(file_pointer);//the (
    													fscanf(file_pointer,"%d",&x1);
    													printf("%d\n",x1);
    													ch=fgetc(file_pointer);//the ,
    													fscanf(file_pointer,"%d",&y1);
    													printf("%d\n",y1);
    													for(m=0;m<10;m++)//no of charac till u reach track no
    														ch=fgetc(file_pointer);
    													fscanf(file_pointer,"%d",&trackno1);
    													printf("go\n");
    													printf("%d\n",trackno1);
    
    
    			      
    												}
    											else if (ch=='Y')
    												{
    													ch=fgetc(file_pointer);//the blank
    			      
    													ch=fgetc(file_pointer);//the (
    													fscanf(file_pointer,"%d",&x2);
    													printf("%d\n",x2);
    													ch=fgetc(file_pointer);//the ,
    													fscanf(file_pointer,"%d",&y2);
    													printf("%d\n",y2);
    													for(n=0;n<10;n++)//no of charac till u reach track no
    														ch=fgetc(file_pointer);
    													fscanf(file_pointer,"%d",&trackno2);
    													printf("%d\n",trackno2);
    			   
    
    			      			      
    												}
    										}
    								}
    						}
    				}
    		  }
    	  }
    	}
    											
    		    
    
    void build_univ_switch(struct switch_block *X)
    { 
      SWITCH *head;
      SWITCH *currentptr;
      int i, j;
      
      
      
    
      for (i = 0; i<40; i++)
        {
          
          /*BUILDS LINKED LIST*/
          head = NULL;
          for (j = 1; j<=4; j++)
    		{
    			currentptr = (SWITCH *) malloc (sizeof (SWITCH));
    			currentptr->line_used = 0;
    			currentptr->nextptr = head;
    			head = currentptr;
    		}
          
          X->Array[i] = head;
        }
      
    }
    
    
    void fill_list (int a1, int a2, int b1, int b2, int t1, int t2)
    {
    
    SWITCH *temp;
    
    if (a1==a2 && a1!=0)
    	{
    	  if (b1==b2 && b1!=0)
    	    {
    	      temp=Layout[17-b1][a1].Array[19+t2];
    	      temp=temp->nextptr;
    	      temp->line_used=1;
    	      
    	      temp=Layout[17-b1][a1].Array[19-t1];
    	      temp=temp->nextptr;
    	      temp=temp->nextptr;
    	      temp->line_used=1;
    	    }
    	  
    	  else
    	    {
    	      if (b2==b1+1)
    		{
    		  temp=Layout[17-b1][a1].Array[19-t1];
    		  temp->line_used=1;
    		  
    		  temp=Layout[17-b1][a1].Array[9-t2];
    		  temp=temp->nextptr;
    		  temp->line_used=1;
    		}
    	      else 
    		{
    		  if (b2 > b1+1)
    		    {
    		      temp=Layout[17-(b2-1)][a1].Array[19+t2];
    		      temp->line_used=1;
    		      
    		      temp=Layout[17-(b2-1)][a1].Array[9-t2];
    		      temp=temp->nextptr;
    		      temp=temp->nextptr;
    		      temp->line_used=1;
    		    }
    		}
    	    }
    	}
          else
    	{
    	  if (b1==b2) 
    	    {
    	      if (a1==a2+1)  
    		{
    		  temp=Layout[17-b2][a2].Array[19+t2];
    		  temp=temp->nextptr;
    		  temp=temp->nextptr;
    		  temp=temp->nextptr;
    		  temp->line_used=1;
    		  
    		  temp=Layout[17-b2][a2].Array[29+t1];
    		  temp=temp->nextptr;
    		  temp=temp->nextptr;
    		  temp->line_used=1;
    		}
    	      else
    		{
    		  if (a1 > a2+1)
    		    {
    		      temp=Layout[17-a1][a1-1].Array[19-t1];
    		      temp=temp->nextptr;
    		      temp=temp->nextptr;
    		      temp=temp->nextptr;
    		      temp->line_used=1;
    		      
    		      temp=Layout[17-b1][a1-1].Array[29+t1];
    		      temp=temp->nextptr;
    		      temp->line_used=1;
    		    }
    		}
    	    }
    	  else
    	    {
    	      if (b2==b1+1)
    		{
    		  temp=Layout[17-b1][a2].Array[9-t2];
    		  temp=temp->nextptr;
    		  temp=temp->nextptr;
    		  temp=temp->nextptr;
    		  temp->line_used=1;
    		  
    		  temp=Layout[17-b1][a2].Array[29+t1];
    		  temp->line_used=1;
    		}
    	    }
    	}
    	
    }
    I am making my best effort. As I have said I am not very good at programming and hence still could be wrong. My program compiles and runs but I do not get any output i.e. my printfs do not work. If I comment my fill_list function then I get the output for my printfs howevr I still get a message when I debug, the same message below:


    Loaded 'ntdll.dll', no matching symbolic information found.
    Loaded 'C:\WINDOWS\system32\kernel32.dll', no matching symbolic information found.
    The thread 0x9EC has exited with code 0 (0x0).
    The program '\\winusers\homes\WinFiles\Desktop\list\Debug\test 4.exe' has exited with code 0 (0x0).

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Dude, review your posts before pressing submit!
    Your code looks worse and worse every time you post it.
    Turn OFF tabs in your editor, and make it use 2 or 4 spaces

    Example code indented with tabs. Observe how the board blows tabs up out of all proportion.
    Code:
    void foo(int a[][10][20], int max)
    {
    	int x, y, z;
    	for (x = 0; x < max; x++) {
    		for (y = 0; y < 10; y++) {
    			for (z = 0; z < 20; x++) {
    				a[x][y][z] = 0;
    			}
    		}
    	}
    }
    Same example code indented with spaces
    Code:
    void foo ( int a[][10][20], int max ) {
        int x, y, z;
        for ( x = 0 ; x < max ; x++ ) {
            for ( y = 0 ; y < 10 ; y++ ) {
                for ( z = 0 ; z < 20 ; x++ ) {
                    a[x][y][z] = 0;
                }
            }
        }
    }
    Now, here's your code
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    struct switch_matrix {
        int pin_no;
        int line_used;
        struct switch_matrix *nextptr;
    };
    
    struct switch_block {
        struct switch_matrix *Array[40];
    } Layout[18][18];
    
    typedef struct switch_matrix SWITCH;
    
    void build_univ_switch(struct switch_block *X);
    void fill_list(int a1, int a2, int b1, int b2, int t1, int t2);
    void file_read(FILE * file_pointer);
    
    int main()
    {
        FILE *fp;
    
        fp = fopen("e64.r", "r");
        file_read(fp);
        fclose(fp);
        return (0);
    }
    
    void file_read(FILE * file_pointer)
    {
        int x1 = 0, x2 = 0, y1 = 0, y2 = 0, trackno1 = 0, trackno2 = 0, m, n;
        char ch;
        while (!feof(file_pointer)) {
    
            fill_list(x1, x2, y1, y2, trackno1, trackno2);
    
            ch = fgetc(file_pointer);
            if (ch == 'S') {
                x1 = 0;
                y1 = 0;
                x2 = 0;
                y2 = 0;
    
                ch = fgetc(file_pointer);
                if (ch == 'C') {
                    ch = fgetc(file_pointer);
                    if (ch == 'H') {
                        ch = fgetc(file_pointer);
                        if (ch == 'A') {
                            ch = fgetc(file_pointer);
                            if (ch == 'N') {
                                ch = fgetc(file_pointer);
                                if (ch == 'X') {
                                    ch = fgetc(file_pointer);       //the blank
                                    ch = fgetc(file_pointer);       //the (
                                    fscanf(file_pointer, "%d", &x1);
                                    printf("%d\n", x1);
                                    ch = fgetc(file_pointer);       //the ,
                                    fscanf(file_pointer, "%d", &y1);
                                    printf("%d\n", y1);
                                    for (m = 0; m < 10; m++)        //no of charac till u reach track no
                                        ch = fgetc(file_pointer);
                                    fscanf(file_pointer, "%d", &trackno1);
                                    printf("go\n");
                                    printf("%d\n", trackno1);
                                } else if (ch == 'Y') {
                                    ch = fgetc(file_pointer);       //the blank
                                    ch = fgetc(file_pointer);       //the (
                                    fscanf(file_pointer, "%d", &x2);
                                    printf("%d\n", x2);
                                    ch = fgetc(file_pointer);       //the ,
                                    fscanf(file_pointer, "%d", &y2);
                                    printf("%d\n", y2);
                                    for (n = 0; n < 10; n++)        //no of charac till u reach track no
                                        ch = fgetc(file_pointer);
                                    fscanf(file_pointer, "%d", &trackno2);
                                    printf("%d\n", trackno2);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    
    void build_univ_switch(struct switch_block *X)
    {
        SWITCH *head;
        SWITCH *currentptr;
        int i, j;
        
        for (i = 0; i < 40; i++) {
            /*BUILDS LINKED LIST */
            head = NULL;
            for (j = 1; j <= 4; j++) {
                currentptr = (SWITCH *) malloc(sizeof(SWITCH));
                currentptr->line_used = 0;
                currentptr->nextptr = head;
                head = currentptr;
            }
            X->Array[i] = head;
        }
    }
    
    void fill_list(int a1, int a2, int b1, int b2, int t1, int t2)
    {
        SWITCH *temp;
    
        if (a1 == a2 && a1 != 0) {
            if (b1 == b2 && b1 != 0) {
                temp = Layout[17 - b1][a1].Array[19 + t2];
                temp = temp->nextptr;
                temp->line_used = 1;
                temp = Layout[17 - b1][a1].Array[19 - t1];
                temp = temp->nextptr;
                temp = temp->nextptr;
                temp->line_used = 1;
            }
            else {
                if (b2 == b1 + 1) {
                    temp = Layout[17 - b1][a1].Array[19 - t1];
                    temp->line_used = 1;
                    temp = Layout[17 - b1][a1].Array[9 - t2];
                    temp = temp->nextptr;
                    temp->line_used = 1;
                } else {
                    if (b2 > b1 + 1) {
                        temp = Layout[17 - (b2 - 1)][a1].Array[19 + t2];
                        temp->line_used = 1;
                        temp = Layout[17 - (b2 - 1)][a1].Array[9 - t2];
                        temp = temp->nextptr;
                        temp = temp->nextptr;
                        temp->line_used = 1;
                    }
                }
            }
        } else {
            if (b1 == b2) {
                if (a1 == a2 + 1) {
                    temp = Layout[17 - b2][a2].Array[19 + t2];
                    temp = temp->nextptr;
                    temp = temp->nextptr;
                    temp = temp->nextptr;
                    temp->line_used = 1;
                    temp = Layout[17 - b2][a2].Array[29 + t1];
                    temp = temp->nextptr;
                    temp = temp->nextptr;
                    temp->line_used = 1;
                } else {
                    if (a1 > a2 + 1) {
                        temp = Layout[17 - a1][a1 - 1].Array[19 - t1];
                        temp = temp->nextptr;
                        temp = temp->nextptr;
                        temp = temp->nextptr;
                        temp->line_used = 1;
                        temp = Layout[17 - b1][a1 - 1].Array[29 + t1];
                        temp = temp->nextptr;
                        temp->line_used = 1;
                    }
                }
            } else {
                if (b2 == b1 + 1) {
                    temp = Layout[17 - b1][a2].Array[9 - t2];
                    temp = temp->nextptr;
                    temp = temp->nextptr;
                    temp = temp->nextptr;
                    temp->line_used = 1;
                    temp = Layout[17 - b1][a2].Array[29 + t1];
                    temp->line_used = 1;
                }
            }
        }
    }
    Now, lets deal with file_read first, and make sure it is extracting all the data you want, and rejecting all the data you don't want. Then we can work on calling your other functions in another post.

    > while (!feof(file_pointer))
    Read the FAQ.

    We can simplify this function and avoid a lot of different ways of reading the file fgetc, fscanf etc.
    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, file_pointer ) != NULL ) {
        if ( strncmp( buff, "SCHAN", 5 ) == 0 ) {
            /* now compare buff[6] to see if it's 'X' or 'Y' */
        }
    }
    Then you can do something like this to extract data from the interior of the buffer
    Code:
    sscanf( &buff[7], "%d %d", &x, &y );
    Posting a couple of examples of SCHANX and SCHANY lines would really help.
    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.

  10. #10
    Registered User
    Join Date
    Mar 2005
    Posts
    18
    Hi Salem,
    I read the feof in the FAQ. I was trying to use the strncmp. The first condition is checked fine. But when I have to check the next element to see if its an X or Y, I cannot use the buff[5]. I tried reading if it is CHANX first then as you specified sscanf but in the sscanf I am unable to read the y as I simply get a 0.
    I tired one sscanf for the x and one for the y but now if my x or y change then I will again be reading a zero as the buff[] element specified in my sscanf will change.
    Code:
    while ( fgets( buff, 50, file_pointer ) != NULL ) 
      {
        
           if ( strncmp( buff, "CHANX", 5 ) == 0 ) 
              {
    
    	//if (strncmp( &buff[5], "X", 1 ) == 0 )
    	//{
    			  
    	    sscanf( &buff[7], "%d ", &x1);
                sscanf( &buff[9], "%d", &y1);
    	    printf("%d %d\n", x1, y1);
    	    sscanf(&buff[20], "%d", &trackno1);
    	    printf("%d\n", trackno1);
                    //}
               }
       }

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you're just checking one element, don't bother with strncmp. Just check that spot in the array:
    Code:
    while ( fgets( buff, 50, file_pointer ) != NULL ) 
      {
        
           if ( strncmp( buff, "CHANX", 5 ) == 0 ) 
              {
    
    	if ( buff[5] == 'X' )
    	{
                ...do whatever...
    Hope is the first step on the road to disappointment.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Post your sscanf calls, and examples of lines in your file
    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.

  13. #13
    Registered User
    Join Date
    Mar 2005
    Posts
    18

    File Read

    My File starts like this. Also the file is really big: around 4 K so what effect will that have on my buffer as I dont think I will be able to use such a big buffer to read the entire contents of the file.
    The spaces are as posted in the file. No space before Source, 2 before Opin/Ipin, 1 before CHANX/Y, 1 before the coordinates and 2 after the coordinates. Hence if I use the sscanf I will have to specify the element of the array and will not be the same everytime.
    I need to read if it is CHANX or CHANY the coordinates and the track number. Using these I will compare and fill my list in the other function.


    Code:
    Array size: 17 x 17 logic blocks.
    
    Routing:
    
    Net 0 (i_63_)
    
    SOURCE (18,1)  Pad: 0  
      OPIN (18,1)  Pad: 0  
     CHANY (17,1)  Track: 2  
     CHANX (17,1)  Track: 2  
      IPIN (17,1)  Pin: 2  
      SINK (17,1)  Class: 0  
     CHANX (17,1)  Track: 2  
     CHANY (16,2)  Track: 2  
      IPIN (16,2)  Pin: 3  
      SINK (16,2)  Class: 0  
    
    
    Net 1 (i_50_)
    
    SOURCE (4,0)  Pad: 0  
      OPIN (4,0)  Pad: 0  
     CHANX (4,0)  Track: 5  
     CHANY (4,1)  Track: 4  
     CHANY (4,2)  Track: 4  
     CHANY (4,3)  Track: 4  
     CHANY (4,4)  Track: 4  
     CHANY (4,5)  Track: 4  
     CHANY (4,6)  Track: 4  
     CHANY (4,7)  Track: 4  
     CHANX (4,7)  Track: 4  
     CHANY (4,8)  Track: 5  
     CHANY (4,9)  Track: 5  
     CHANY (4,10)  Track: 5  
     CHANY (4,11)  Track: 5  
      IPIN (4,11)  Pin: 3  
      SINK (4,11)  Class: 0
    I have used only 3 sscanf calls for x1, y1, and for track no.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void decode ( char *line ) {
        char type;
        int x, y, track;
        int n;
        if ( strncmp( line, " CHAN", 5) == 0 ) {
            n = sscanf( &line[5], "%c (%d,%d) Track: %d", &type, &x, &y, &track );
            if ( n == 4 ) {
                printf( "Type=%c, pos=%d,%d, track=%d\n", type, x, y, track );
            } else {
                printf( "Oops %d\n", n );
            }
        }
    }
    
    int main ( ) {
        char *lines[] = {
    " CHANY (17,1)  Track: 2",
    " CHANX (17,1)  Track: 2",
    "  IPIN (17,1)  Pin: 2",
    "  SINK (17,1)  Class: 0",
    " CHANX (17,1)  Track: 2",
    " CHANY (16,2)  Track: 2",
    "  IPIN (16,2)  Pin: 3",
    "  SINK (16,2)  Class: 0",
    "SOURCE (4,0)  Pad: 0",
    "  OPIN (4,0)  Pad: 0",
    " CHANX (4,0)  Track: 5",
    " CHANY (4,1)  Track: 4",
    " CHANY (4,2)  Track: 4",
    " CHANY (4,3)  Track: 4",
        };
        size_t i;
        for ( i = 0 ; i < sizeof(lines)/sizeof(*lines); i++ ) {
            decode(lines[i]);
        }
        return 0;
    }
    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.

  15. #15
    Registered User
    Join Date
    Mar 2005
    Posts
    18
    Hi Salem,
    I was just confused after reading this post, I need to read the file and I cannot copy the contents as you have shown. As I said the file is around 4KB and hence has a lot of data similar to the type I have posted earlier. I cannot modify the file, I need to read the file and from that extract the cordinates and track no.
    I should have mentioned this earlier sorry.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list question
    By mikeman in forum C Programming
    Replies: 1
    Last Post: 11-30-2008, 01:56 PM
  2. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  3. Duplicating value of pointer to linked list
    By zephyrcat in forum C Programming
    Replies: 14
    Last Post: 01-22-2008, 03:19 PM
  4. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  5. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM