Hey
I have a text file (1.txt) with the lines:
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
and Im trying to read all the pair lines from that file and write them to another text file called 2.txt which would result in this:
This is line 2
This is line 4
My code works but the problem is at the end it runs into a infinite loop and the result is
This is line 2
This is line 4
This is line 5
This is line 5
This is line 5
This is line 5
This is line 5
This is line 5
This is......
until I manually stop the program. I imagine there is something wrong with my EOF loop and/or the length I set. Also maybe the program doesnt know it should jump to the next line when it reads a /n or something. The variables are in spanish but I THINK they could be understood. If not, Ill translate
#include <assert.h>
#include <complex.h>
#include <ctype.h>
#include <errno.h>
#include <fenv.h>
#include <float.h>
#include <inttypes.h>
#include <iso646.h>
#include <limits.h>
#include <locale.h>
#include <math.h>
#include <setjmp.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <wchar.h>
#include <wctype.h>
int main()
{
FILE *archivopar;
FILE *archivocopiado;
char texto[50];
int i=1;
archivopar=fopen("archivopar.txt","r");
archivocopiado=fopen("archivocopiado.txt","w");
if ((archivopar==NULL) || (archivocopiado==NULL))
{
printf ("No se puede abrir algun archivo!");
return 1;
}
else
{
do{
fgets(texto,50,archivopar);
if (i % 2 == 0)
{
fprintf(archivocopiado,"%s\n",texto);
}
if (archivopar!=EOF)
{
i=i+1;
}
}while(archivopar!=EOF);
fclose(archivopar);
fclose(archivocopiado);
return 0;
}
}
I know its problably something stupid but I cant figure it out. Thanks for the help.