Thread: I need a help with this code source

  1. #1
    Registered User FISHING's Avatar
    Join Date
    Mar 2010
    Posts
    4

    I need a help with this code source

    I need a source code by passing a file full of strings (one per line) I order them alphabetically.

    I present the code I have made myself. orders 1 file but when I pass the second I get the error attached below:


    The poblem that I obtain with gdb is:

    Domingo

    Jueves

    Lunes

    Martes

    Miercoles

    Sabado

    Viernes


    Program received signal SIGSEGV, Segmentation fault.
    0x0000003334279140 in strcmp () from /lib64/libc.so.6



    Code:
    void Mostrar(FILE *fich);
    int Func(char **s1, char **s2);
    void PrintNames(char **ap, unsigned nobj);
    
    /*-----------------------------------------------------*/
    
    int main(int argc, char*argv[])
    {
    	argv0 = "sorttext";
    
    	if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) )
    	{
    		fprintf(stderr, "%s: Uso: %s [ fichero... ]\n", argv0, argv0);
    		fprintf(stderr, "%s: Invierte el orden de las líneas de los ficheros (o de la entrada).\n", argv0);
    		return 0;
    	}
    
    /*   Aqui se deben incluir las funciones necesarias para resolver los
         diferentes apartados de la practica. Se recomienda:
            - Sea ordenado y metodico en la codificación de las
              funcionalidades
            - Si el programa va a ser complejo, utilice varios ficheros 
    	  de implementacion .c y defina los ficheros de cabecera .h 
    	  con el prototipo de las funciones.
            - Antes de preguntar al profesor (en tutorias, correo
              electronico o via el foro) depure bien el problema e intente 
              acotarlo. No pregunte "Esto no me funciona...", diga donde,
              por que y en que casos falla.
    */
    
    /*	Ejemplo de uso de la función de Error. */
    //	...
    //		Error(EX_NOINPUT, "El fichero \"%s\" no puede ser leido.", argv[i]);
    /* fgets exmaple */
    
    /*--------------------Variables --------------------*/
    
    	FILE *archivo;
    	FILE *aux;
    	char porordenar [128], actual [128], auxiliar [128];
    	int cuenta = 0;
    	int turno ;
    	int i = 0;
    	int s = 0;
    	int l = 1;
    	int t = 0;
    	int puesto =0;
    	int idR = 0;
    
    /*-------------------NUEVOS-------------------------*/
    
    /*--------------------------------------------------*/
    	turno = argc;
    aux = fopen("aux.txt","w+");
    while (l<turno)
    {	
    	if (idR = access(argv[l], F_OK) != 0)
    	{
    		Error(EX_NOINPUT, "El fichero \"%s\" no puede ser leido.", argv[i]);	
    	}
    	archivo = fopen(argv [l],"r");
    
    /*archivo = fopen("segundo.txt","r");*/
    /*	aux = fopen("aux.txt","w+");*/
    
    	while (!feof(archivo))
    	{  
    	fgets (porordenar, 128, archivo);
    	cuenta = cuenta ++;
    	}
    	cuenta = cuenta --;
    
    	char *starr [cuenta];
    	char buffer[cuenta] [128];
    
    	rewind (archivo);
    
    	while (!feof(archivo))
            {
            fgets (porordenar, 128, archivo);
    	strcpy(buffer[i],porordenar);
    	starr [i]=buffer [i];
    	i = i++;
            }
    
    /*   PrintNames(starr, sizeof starr / sizeof starr[0]);*/
    /*
        qsort(
            starr,
            0,
            8,
            (int (*)(const void *,  const void *)) Func
        );
    */
    for (t=0, t<=cuenta, t++)
    {
    qsort(
            starr,
            sizeof starr / sizeof starr[t],
            sizeof starr[t],
            (int (*)(const void *,  const void *)) Func
        );
    }
    	while ( s < cuenta)
    	{
    	fputs (starr[s],aux);
    	s = s++;
    	}
    
    /*printf (" ORDENADO\n");	*/
    
    cuenta = 0;
    s = 0;
    l = l++;
    Mostrar (aux);
    /*fclose (archivo);*/
    }
    fclose (aux);
    return 0;
    }
    
    
    void Mostrar(FILE *fich)
    {
       char linea[128];
    
       rewind(fich);
       fgets(linea, 128, fich);
       while(!feof(fich)) {
          puts(linea);
          fgets(linea, 128, fich);
       }
    }
    
    /*Func:  compara dos strings apuntados por *s1 y  *s2 */
    int Func(char **s1, char **s2)
    {
        return strcmp(*s1, *s2);
    }
    
    
    /* PrintNames:  print names from ap for nobj elements */
    void PrintNames(char **ap, unsigned nobj)
    {
        for ( ; nobj > 0; ap++, nobj--)
            printf("%s\n", *ap);
    }
    THANKS for your time

  2. #2
    Registered User FISHING's Avatar
    Join Date
    Mar 2010
    Posts
    4
    the first file is
    Lunes
    Martes
    Miercoles
    Jueves
    Viernes
    Sabado
    Domingo

    Second file
    one
    two
    three

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    while (!feof(archivo))
    Read FAQ why using feof to control loop is bad idea.

    Code:
    for (t=0, t<=cuenta, t++)
    if I read your code correctly - you have only cuenta members initialized in your array... and accessing cuenta+1 element... right?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User FISHING's Avatar
    Join Date
    Mar 2010
    Posts
    4
    You have understood correctly... I dont know if de answer is to make free the pointer char *parr when I finish the operation with the pointer to pass to the other file. maybe ?

    I just switched While function of For..... all ok;
    Last edited by FISHING; 03-17-2010 at 04:30 AM.

  5. #5
    Registered User FISHING's Avatar
    Join Date
    Mar 2010
    Posts
    4
    How can I free a pointer that have a determinated size ?
    For example char *attr [5]

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by FISHING View Post
    How can I free a pointer that have a determinated size ?
    For example char *attr [5]
    That's actually an array of five pointers. As such, you'd free each one in turn:
    Code:
    for( x = 0; x < 5; x++ )
    {
        free( attr[ x ] );
    }

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

  7. #7
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    maybe free(attr) ? in stdlib.h



    sería mejor, cuando quieres que alguien te ayude, poner el programa, o por lo menos los 'comentos' en inglés tio.....
    Last edited by rogster001; 03-18-2010 at 04:21 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seven Kingdoms I: Ancient Adversaries for Linux
    By MIH1406 in forum Projects and Job Recruitment
    Replies: 13
    Last Post: 01-17-2010, 05:03 PM
  2. How do you call another source code file?
    By nifear4 in forum C Programming
    Replies: 2
    Last Post: 10-28-2008, 12:16 PM
  3. DxEngine source code
    By Sang-drax in forum Game Programming
    Replies: 5
    Last Post: 06-26-2003, 05:50 PM
  4. Lines from Unix's source code have been copied into the heart of Linux????
    By zahid in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 05-19-2003, 03:50 PM
  5. Source Code Beautifier
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-05-2002, 09:21 PM