elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Tutorial básico de Quickjs


  Mostrar Mensajes
Páginas: [1]
1  Programación / Programación C/C++ / Re: Existe algun comando para leer y guardar el valor de un fichero en C? en: 4 Febrero 2019, 00:38 am
Lo siento es que soy nuevo aquí.

Aquí lo tenéis.


Código
  1. bool guardat, espai;
  2.    char i;
  3.  
  4.    FILE *ficher;
  5.    guardat = false;
  6.    espai = true;
  7.  
  8.    ficher = fopen("records.txt" ,"r");
  9.    if (ficher != NULL)
  10.    {
  11.        fscanf (ficher, "%c", &i);
  12.        if (i == '\0')
  13.        {
  14.            espai = false;
  15.        }
  16.        fclose(ficher);
  17.    }
  18.  
  19.  
  20.    ficher = fopen("records.txt" ,"a");
  21.    if (ficher != NULL)
  22.    {
  23.        if (espai)
  24.        {
  25.            fprintf(ficher, "\n");
  26.        }
  27.        fprintf(ficher,"%d-%d-%d", jugador.fecha.dia, jugador.fecha.mes, jugador.fecha.anyo);
  28.        fprintf (ficher, "\t");
  29.  
  30.        fprintf(ficher,"%d", jugador.record);
  31.        fprintf (ficher, "\t");
  32.  
  33.        fprintf(ficher,"%s", jugador.nombre);
  34.        printf ("\n");
  35.  
  36.        fclose (ficher);
  37.        guardat = true;
  38.    }
  39.    else
  40.    {
  41.        return guardat;
  42.    }
  43.    return guardat;
  44.  
  45.  
  46. Por ahora he ido avanzando y tengo todo esto pero me sigue fallando en imprimir las cosas y esto me vuelve loco:
  47.  
  48.  
  49. int MaxAnterior = 0;
  50.    int puntuacionMax = 0;
  51.    int puntuacionMax2 = 0;
  52.    int puntuacion = 0;
  53.    char caracters;
  54.    int records = 0;
  55.    int cont = 0;
  56.    int i, c, aux;
  57.    char data[20];
  58.    int MAXIM = 0;
  59.  
  60.    FILE *ficher;
  61.  
  62.    system("cls");
  63.    c = 0;
  64.  
  65.    printf ("\nFECHA                   PUNTUACION      NOMBRE");
  66.    printf ("\n------------------------------------------------");
  67.  
  68.  
  69.  
  70.    ficher = fopen("records.txt" ,"r");                 /* Contador de records */
  71.    if (ficher != NULL)
  72.    {
  73.        while ((i = fgetc(ficher)) != EOF)
  74.        {
  75.            if (i == '\n')
  76.            {
  77.                records = records + 1;
  78.            }
  79.        }
  80.        records = records + 1;
  81.        fclose (ficher);
  82.    }
  83.    else
  84.    {
  85.        printf("Error");
  86.    }
  87.  
  88. do
  89. {
  90.    puntuacionMax2 = 0;
  91.    ficher = fopen("records.txt" ,"r");              /* Vizualitzar la puntuació màxima */
  92.    if(ficher!=NULL)
  93.    {
  94.        while(!feof(ficher))
  95.        {
  96.            fscanf(ficher,"%c",&caracters);
  97.            if(caracters == '\t')
  98.            {
  99.                fscanf(ficher,"%i",&puntuacionMax);
  100.                if (puntuacionMax == MaxAnterior)
  101.                if((puntuacionMax > puntuacionMax2) && (puntuacionMax != MaxAnterior))
  102.                {
  103.                    puntuacionMax2 = puntuacionMax;
  104.                }
  105.                fscanf(ficher, "\n");
  106.            }
  107.        }
  108.        fclose(ficher);
  109.    }
  110.  
  111.  
  112.    ficher = fopen("records.txt" ,"r");             /* Imprimir els records */
  113.    if (ficher != NULL)
  114.    {
  115.        while(!feof(ficher))
  116.        {
  117.        c = 0;
  118.        caracters = '\n';
  119.        while (caracters != '\t')
  120.        {
  121.            fscanf(ficher, "%c", &caracters);
  122.            data[c] = caracters;
  123.            c = c + 1;
  124.        }
  125.        fscanf (ficher, "\t");
  126.        fscanf(ficher, "%i", &puntuacion);
  127.        if(puntuacion == puntuacionMax2)
  128.        {
  129.            aux = c;
  130.            c = 0;
  131.            printf ("\n");
  132.            while (c < aux)
  133.            {
  134.                printf("%c",data[c]);
  135.                c++;
  136.            }
  137.            printf ("%i" ,puntuacion);
  138.            printf ("\t");
  139.            while (caracters != -1)
  140.            {
  141.                caracters = fgetc(ficher);
  142.                printf ("%c", caracters);
  143.            }
  144.            cont = cont + 1;
  145.            MaxAnterior = puntuacionMax2;
  146.            if (MaxAnterior > MAXIM)
  147.            {
  148.                MAXIM = MaxAnterior;
  149.            }
  150.        }
  151.        else
  152.        {
  153.            while (caracters != '\n')
  154.            {
  155.                fscanf(ficher,"%c", &caracters);
  156.            }
  157.        }
  158.        }
  159.        fclose (ficher);
  160.    }
  161.    else
  162.    {
  163.        printf("Error");
  164.    }
  165. }while (cont != records-1);
  166.  
  167.  
  168.    printf ("\n------------------------------------------------\n");
  169.    printf("Records: %i\n", records);
  170.    printf("Puntuacion max: %i\n\n", MAXIM);
2  Programación / Programación C/C++ / Re: Existe algun comando para leer y guardar el valor de un fichero en C? en: 3 Febrero 2019, 23:22 pm
Perdón por no contestar antes. El problema que tengo es que las fechas, los puntuajes y los nombres los guardé a partir de unos registros.

A esto me refiero:

bool guardat, espai;
    char i;

    FILE *ficher;
    guardat = false;
    espai = true;

    ficher = fopen("records.txt" ,"r");
    if (ficher != NULL)
    {
        fscanf (ficher, "%c", &i);
        if (i == '\0')
        {
            espai = false;
        }
        fclose(ficher);
    }


    ficher = fopen("records.txt" ,"a");
    if (ficher != NULL)
    {
        if (espai)
        {
            fprintf(ficher, "\n");
        }
        fprintf(ficher,"%d-%d-%d", jugador.fecha.dia, jugador.fecha.mes, jugador.fecha.anyo);
        fprintf (ficher, "\t");

        fprintf(ficher,"%d", jugador.record);
        fprintf (ficher, "\t");

        fprintf(ficher,"%s", jugador.nombre);
        printf ("\n");

        fclose (ficher);
        guardat = true;
    }
    else
    {
        return guardat;
    }
    return guardat;


Por ahora he ido avanzando y tengo todo esto pero me sigue fallando en imprimir las cosas y esto me vuelve loco:


int MaxAnterior = 0;
    int puntuacionMax = 0;
    int puntuacionMax2 = 0;
    int puntuacion = 0;
    char caracters;
    int records = 0;
    int cont = 0;
    int i, c, aux;
    char data[20];
    int MAXIM = 0;

    FILE *ficher;

    system("cls");
    c = 0;

    printf ("\nFECHA                   PUNTUACION      NOMBRE");
    printf ("\n------------------------------------------------");



    ficher = fopen("records.txt" ,"r");                 /* Contador de records */
    if (ficher != NULL)
    {
        while ((i = fgetc(ficher)) != EOF)
        {
            if (i == '\n')
            {
                records = records + 1;
            }
        }
        records = records + 1;
        fclose (ficher);
    }
    else
    {
        printf("Error");
    }

do
{
    puntuacionMax2 = 0;
    ficher = fopen("records.txt" ,"r");              /* Vizualitzar la puntuació màxima */
    if(ficher!=NULL)
    {
        while(!feof(ficher))
        {
            fscanf(ficher,"%c",&caracters);
            if(caracters == '\t')
            {
                fscanf(ficher,"%i",&puntuacionMax);
                if (puntuacionMax == MaxAnterior)
                if((puntuacionMax > puntuacionMax2) && (puntuacionMax != MaxAnterior))
                {
                    puntuacionMax2 = puntuacionMax;
                }
                fscanf(ficher, "\n");
            }
        }
        fclose(ficher);
    }


    ficher = fopen("records.txt" ,"r");             /* Imprimir els records */
    if (ficher != NULL)
    {
        while(!feof(ficher))
        {
        c = 0;
        caracters = '\n';
        while (caracters != '\t')
        {
            fscanf(ficher, "%c", &caracters);
            data[c] = caracters;
            c = c + 1;
        }
        fscanf (ficher, "\t");
        fscanf(ficher, "%i", &puntuacion);
        if(puntuacion == puntuacionMax2)
        {
            aux = c;
            c = 0;
            printf ("\n");
            while (c < aux)
            {
                printf("%c",data[c]);
                c++;
            }
            printf ("%i" ,puntuacion);
            printf ("\t");
            while (caracters != -1)
            {
                caracters = fgetc(ficher);
                printf ("%c", caracters);
            }
            cont = cont + 1;
            MaxAnterior = puntuacionMax2;
            if (MaxAnterior > MAXIM)
            {
                MAXIM = MaxAnterior;
            }
        }
        else
        {
            while (caracters != '\n')
            {
                fscanf(ficher,"%c", &caracters);
            }
        }
        }
        fclose (ficher);
    }
    else
    {
        printf("Error");
    }
}while (cont != records-1);


    printf ("\n------------------------------------------------\n");
    printf("Records: %i\n", records);
    printf("Puntuacion max: %i\n\n", MAXIM);
3  Programación / Programación C/C++ / Existe algun comando para leer y guardar el valor de un fichero en C? en: 3 Febrero 2019, 14:48 pm
Estoy teniendo problemas con el fichero para leer un valor especifico y compararlo con otro. La idea es que tengo un archivo de récords donde están guardados en cada linea la fecha, la puntuación y el nombre. Tengo que imprimir este fichero en la pantalla, lo hago con el fgetc y putchar pero el problema es que tengo que imprimirlos en orden, es decir, el primero con la puntuación más alta, luego la segunda puntuación más alta y así hasta el final. Luego tengo que imprimir la cantidad de récords imprimidos (esto lo hice con un contador) y la puntuación máxima de los récords imprimidos.

Aqui os dejo lo que tengo por ahora. (lo intente con tabla de caracteres pero no supe ni como comenzar)

system("cls");
    int i, cont_ficher, cont, num;
    int aux[20];

    FILE *ficher;
   
    cont_ficher = 0;
    cont = 0;

    printf ("\nFECHA           PUNTUACION      NOMBRE");
    printf ("\n---------------------------------------\n");



    ficher = fopen("records.txt" ,"r");           
    if (ficher != NULL)
    {
        while ((i = fgetc(ficher)) != EOF)
        {
            if (i == '\n')
            {
                cont_ficher = cont_ficher + 1;
            }
        }
        cont_ficher = cont_ficher + 1;
        fclose (ficher);
    }
    else
    {
        printf("Error");
    }

    do
    {
    ficher = fopen("records.txt" ,"r");             
    if (ficher != NULL)
    {
        while ((i = fgetc(ficher)) != EOF)
        {
            if (i == 9)
            {
                while ((i = fgetc(ficher)) != 9)
                {

                }
            }
        }
         fclose (ficher);
    }
    else
    {
        printf("Error");
    }

    ficher = fopen("records.txt" ,"r");             
    if (ficher != NULL)
    {
        while ((i = fgetc(ficher)) != EOF)
        {
            putchar(i);
        }
        cont = cont + 1;
         fclose (ficher);
    }
    else
    {
        printf("Error");
    }
    }while (cont != cont_ficher);

    printf ("\n---------------------------------------\n");
    printf ("");



Tambien os dejo el fichero:

3-2-2019   39000      NOMBRE
3-2-2019   21000      NOMBRE               (Estan separados por tabulaciones)
3-2-2019   18000      NOMBRE               (FECHA \t  PUNTUACION \t\t NOMBRE)
Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines