Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Escriba un programa ISO C que procese el archivo de números datos.txt sobre
- sí mismo. El procesamiento consite en convertir los números encontrados
- (de 1 o más cifras decimales) a octal.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <math.h>
- int decimal_a_octal(int decimal) {
- int octal = 0;
- int contador = 1;
- while (decimal >= 1) {
- int resto = (decimal % 8);
- octal += resto * contador;
- decimal = decimal / 8;
- contador *= 10;
- }
- return octal;
- }
- int obtener_cifras_de_un_numero(int n) {
- int cifras = 0;
- while (n >= 1) {
- cifras++;
- n = n / 10;
- }
- return cifras;
- }
- int leer_reversa(FILE* f, int* seek_lectura) {
- fseek(f, *seek_lectura, SEEK_SET);
- char c = fgetc(f);
- if (ftell(f) == 1) {
- *seek_lectura = -1;
- } else {
- fseek(f, -2, SEEK_CUR);
- *seek_lectura = ftell(f);
- }
- if (c == ' ') return -1;
- if (c >= 48 && c <= 57)
- return (int)(c - 48);
- return -2;
- }
- void escribir_valor_reversa(FILE* f, int* seek_escritura, int valor) {
- int cifras = obtener_cifras_de_un_numero(valor);
- fseek(f, *seek_escritura - cifras, SEEK_SET);
- fprintf(f, "%d", valor);
- fseek(f, *seek_escritura - cifras - 1, SEEK_SET);
- *seek_escritura = ftell(f);
- }
- void escribir_espacio_reversa(FILE* f, int* seek_escritura) {
- fseek(f, *seek_escritura, SEEK_SET);
- fputc(' ', f);
- fseek(f, -2, SEEK_CUR);
- *seek_escritura = ftell(f);
- }
- int main() {
- FILE* f = fopen("datos.txt", "r");
- if (!f) return -1;
- int new_size = 0;
- int decimal_actual = 0;
- char c;
- int seek_lectura = 0;
- int seek_escritura = 0;
- while ( (c = fgetc(f)) != EOF ) {
- if (c == ' ') {
- int octal = decimal_a_octal(decimal_actual);
- new_size += obtener_cifras_de_un_numero(octal) + 1;
- decimal_actual = 0;
- } else if (c >= 48 && c <= 57){
- decimal_actual *= 10;
- decimal_actual += (int)(c - 48);
- }
- seek_lectura++;
- }
- seek_lectura -= 2;
- int octal = decimal_a_octal(decimal_actual);
- new_size += obtener_cifras_de_un_numero(octal);
- seek_escritura = new_size;
- fclose(f);
- truncate("datos.txt", new_size);
- f = fopen("datos.txt", "r+");
- if (!f) return -1;
- decimal_actual = 0;
- printf("DEBUG: antes del while seek_lectura : %d\n", seek_lectura);
- while ( seek_lectura != -1 ) {
- c = leer_reversa(f, &seek_lectura);
- printf("DEBUG: seek_lectura : %d - c: %d\n", seek_lectura, (int)c);
- if (c == -1) {
- // espacio
- printf("DEBUG: Decimal: %d, octal: %d\n", decimal_actual, decimal_a_octal(decimal_actual));
- escribir_valor_reversa(f, &seek_escritura, decimal_a_octal(decimal_actual));
- escribir_espacio_reversa(f, &seek_escritura);
- decimal_actual = 0;
- } else if (c >= 0 && c <= 9) {
- decimal_actual += c * (pow(10, obtener_cifras_de_un_numero(decimal_actual)));
- }
- }
- printf("DEBUG: Decimal: %d, octal: %d\n", decimal_actual, decimal_a_octal(decimal_actual));
- escribir_valor_reversa(f, &seek_escritura, decimal_a_octal(decimal_actual));
- fclose(f);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement