I'm executing a program from command line, it takes string integer string string parameters, I call a function that reads the following file:
sólo te lo diré mañana al mediodía en la biblioteca
It reads the file and prints what it is supposed to print but when it goes back to the main function the value NumHijos takes a random value, why does it happen? I am not even using it in any function
Main program, let's say we have this command line ./program -d 4 File1 File2
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include "file.h"
int main(int argc, char *argv[]) {
/*command line args*/
int NumHijos;
int op = 0;
size_t len = strlen(argv[1]);
char * operacion = malloc(len+2);
strcpy(operacion, argv[1]);
NumHijos = atoi(argv[2]);
len = strlen(argv[3]);
char * file1 = malloc(len+5);
strcpy(file1, argv[3]);
strcat(file1,".txt");
len = strlen(argv[4]);
char * file2 = malloc(len+5);
strcpy(file2, argv[4]);
char vector [NumHijos];
printf("Arg 1: %s\n",operacion);
printf("Arg 2: %d\n",NumHijos);
printf("Arg 3: %s\n", file1);
printf("Arg 4: %s\n\n", file2);
abrirArchivoEntrada(file1, vector, NumHijos);
int i = 1;
if (operacion[1] == 'd'){
printf("decypher\n");
}
else if (operacion[1] == 'c'){
printf("crypt\n");
}
return 0;
printf("times %d", NumHijos);
}
file.h
void llenarVector(FILE *e, char texto [], int n) {
int l;
while (!feof(e)){
fgets(texto, 1000, e);
}
l = strlen(texto);
printf("%s\n", texto);
printf("%d\n", l);
fclose(e);
}
void abrirArchivoEntrada (char * nombre, char texto[], int n){
FILE *e;
e = fopen(nombre, "r");
if (e == NULL)
{
printf("error\n");
}
else {
printf("loaded successfully\n");
llenarVector(e,texto,n);
}
}