When using two WHILE loops in C, only the first one executes

1

If I mention one of the two, whatever it is, the other one works, but both at the same time it does not work.

#include <stdio.h>
#include <stdlib.h>

char fichero1;
char linea[1024];
int cuenta;
char letra;
char c;
main()
{
printf("Itroduce el nombre de un fichero en esta carpeta(ejemplo:origen.txt): \n");
scanf("%s", &fichero1);
printf("\n");

FILE *archivo, *archivo2;

archivo=fopen(&fichero1,"r");
archivo2=fopen("destino.txt","w");

if (archivo==NULL)

{printf("Imposible abrir fichero");}

else
{
    cuenta = 0;
    printf("Fichero abierto:\n");
    while ((letra = getc(archivo)) != EOF) {
        if(letra == '\n') {
        printf(" ");
        printf("(%d)\n", cuenta);
        cuenta = 0;

        } else {
        cuenta++;
        printf("%c", letra);
        }

    }
while (!feof(archivo))
    {
        fscanf(archivo,"%c",&c);
        if (c==' ' || c=='\t')
        {fprintf(archivo2,"");
         while (c==' ' || c=='\t')
         fscanf(archivo,"%c",&c);}
        fprintf(archivo2,"%c",c);
    }

fclose(archivo);
fclose(archivo2);
 printf("\n Texto copiado sin espacios en destino.txt \n");
}
return 0;
}
    
asked by Arturo Martin 13.06.2018 в 23:59
source

1 answer

0

The problem is that in the first while you read the whole file, and the pointer stays in the end, the solution is to use rewind () like this:

    while ((letra = getc(archivo)) != EOF) {
        if(letra == '\n') {
        printf(" ");
        printf("(%d)\n", cuenta);
        cuenta = 0;

        } else {
        cuenta++;
        printf("%c", letra);
        }

    }
rewind(archivo);
while (!feof(archivo))
    {
        fscanf(archivo,"%c",&c);
        if (c==' ' || c=='\t')
        {fprintf(archivo2,"");
         while (c==' ' || c=='\t')
         fscanf(archivo,"%c",&c);}
        fprintf(archivo2,"%c",c);
    }
    
answered by 14.06.2018 / 00:03
source