|
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. bool guardat, espai; char i; FILE *ficher; guardat = false; espai = true; ficher = fopen("records.txt" ,"r"); if (ficher != NULL) { if (i == '\0') { espai = false; } } ficher = fopen("records.txt" ,"a"); if (ficher != NULL) { if (espai) { } fprintf(ficher ,"%d-%d-%d", jugador. fecha. dia, jugador. fecha. mes, jugador. fecha. anyo); fprintf(ficher ,"%d", jugador. record); fprintf(ficher ,"%s", jugador. nombre); 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; 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; } else { } do { puntuacionMax2 = 0; ficher = fopen("records.txt" ,"r"); /* Vizualitzar la puntuació màxima */ if(ficher!=NULL) { { fscanf(ficher ,"%c",&caracters ); if(caracters == '\t') { fscanf(ficher ,"%i",&puntuacionMax ); if (puntuacionMax == MaxAnterior) if((puntuacionMax > puntuacionMax2) && (puntuacionMax != MaxAnterior)) { puntuacionMax2 = puntuacionMax; } } } } ficher = fopen("records.txt" ,"r"); /* Imprimir els records */ if (ficher != NULL) { { c = 0; caracters = '\n'; while (caracters != '\t') { fscanf(ficher , "%c", &caracters ); data[c] = caracters; c = c + 1; } fscanf(ficher , "%i", &puntuacion ); if(puntuacion == puntuacionMax2) { aux = c; c = 0; while (c < aux) { c++; } while (caracters != -1) { caracters = fgetc(ficher ); } cont = cont + 1; MaxAnterior = puntuacionMax2; if (MaxAnterior > MAXIM) { MAXIM = MaxAnterior; } } else { while (caracters != '\n') { fscanf(ficher ,"%c", &caracters ); } } } } else { } }while (cont != records-1); printf ("\n------------------------------------------------\n"); printf("Records: %i\n", records ); 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)
|
|
|
|
|
|
|