Ordering names

This is a discussion on Ordering names within the C Programming forums, part of the General Programming Boards category; I need to order the names in a file (alphabetically), I managed to put the names in an array and ...

  1. #1
    Registered User unixOZ's Avatar
    Join Date
    Feb 2002
    Posts
    91

    Ordering names

    I need to order the names in a file (alphabetically), I managed to put the names in an array and find the first name. However, Im having trouble in printing the rest of the names. Heres part of the code:

    /*matriz1 and matriz2 are [150][45], alumnos is FILE *alumnos */
    Code:
      memset(matriz1,'\0',sizeof(matriz1));
      memset(matriz2,'\0',sizeof(matriz2));
      while(fgets(buffer,255,alumnos)) {
                    int len;
                    len = strlen(buffer);
                    if(buffer[len-1] == '\n') {
                            buffer[--len] = 0;
                            if(buffer[len-1] == '\r')
                                    buffer[--len] = 0;
                    }
    
                    if(!buffer[0] || buffer[0] == '#')
                            continue;
    		
    				strncpy(matriz1[i], buffer, 45);
    				matriz1[i][44]='\0';
    //				printf("\nI: %i, valor: %s", i, matriz1[i]);
    				fflush(stdout);
    				i++;
    	}
    	
    
    
    	m=i;
    /*
    	limit=str_search(matriz1[0],"#",1);
    	memset(row,'\0',sizeof(row));
    	strncpy(row, matriz1[0], limit);
    	row[limit-1]='\0';
    */		
     for(j=0; j<=m; j++) {
    	if(strlen(matriz1[j])<=1)
    		continue;
    		
    	limit=str_search(matriz1[j],"#",1);
    	memset(row,'\0',sizeof(row));
    	strncpy(row, matriz1[j], limit);
    	row[limit-1]='\0';
    	
    	for(i=0; i<=m; i++){
    		if(strlen(matriz1[i])<=1)
    			continue;
    		/* Matriz1[i] es menor que row */
    		if(strncmp(row,matriz1[i],limit-1)>0){
    			limit=str_search(matriz1[i],"#",1);
    			memset(row,'\0',sizeof(row));
    			strncpy(row, matriz1[i], limit);				
    		}
    	}
    	
    	printf("\n\nEl menor es : %s",row);
    	strncpy(matriz2[index_matriz2], row, 45);
    	matriz2[index_matriz2][44]='\0';
    	matriz1[i][0]='\0';
    	
    	index_matriz2++;	
    
    	memset(matriz1[i],'\0', sizeof(matriz1[i]));
    
    	for(aux=0; aux<150; aux++){
    		if(strlen(matriz1[aux])>1)
    			limit=str_search(matriz1[aux],"#",1);
    			memset(row,'\0',sizeof(row));
    			strncpy(row, matriz1[0], limit);
    			row[limit-1]='\0';
    			break;
    	}
     }	
    
    	printf("\n\nImprimiendo matriz 2\n");
    	for(i=0; i<=m; i++){
    		printf("\nI: %i; %s",i, matriz2[i]);
    		fflush(stdout);	
    	}
    Thanks


    &#91;code]&#91;/code]tagged by Salem

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: Ordering names

    Originally posted by unixOZ
    I need to order the names in a file (alphabetically), I managed to put the names in an array and find the first name.
    ... then you could use the qsort() function.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 02:10 AM
  2. runtime function names
    By lruc in forum Tech Board
    Replies: 2
    Last Post: 10-11-2008, 10:51 AM
  3. reading folder names..how is it done ?
    By roalme00 in forum C++ Programming
    Replies: 8
    Last Post: 01-11-2008, 09:34 AM
  4. Tab Ordering of GUI Controls
    By cloudy in forum Windows Programming
    Replies: 2
    Last Post: 04-22-2006, 09:13 AM
  5. Opening files with korean file names
    By tek in forum C++ Programming
    Replies: 4
    Last Post: 08-28-2003, 10:22 PM

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