Skip to content
Advertisement

Integer taking a random value after a function call that doesn’t even edit it

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: %sn",operacion); 
    printf("Arg 2: %dn",NumHijos); 
    printf("Arg 3: %sn", file1);
    printf("Arg 4: %snn", file2); 

    abrirArchivoEntrada(file1, vector, NumHijos);

    int i = 1;

    if (operacion[1] == 'd'){
        printf("decyphern");
    }

    else if (operacion[1] == 'c'){
        printf("cryptn");
    }
    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("%sn", texto);
    printf("%dn", l);

 fclose(e);
}

void abrirArchivoEntrada (char * nombre, char texto[], int n){
    FILE *e;
    e = fopen(nombre, "r");
            if (e == NULL) 

            {
                printf("errorn");
            }
            else { 
                printf("loaded successfullyn");    
                llenarVector(e,texto,n);
            }
}

Advertisement

Answer

len = strlen(argv[3]);
char * file1 = malloc(len+3);
strcpy(file1, argv[3]);
strcat(file1,".txt");

Is undefined behavior. ".txt" needs 4 more characters, so you need to allocate strlen(argv[3]) + 5 bytes for file1 pointer.

while (!feof(e)) is always wrong.

fgets(texto, 1000, e); (most probably) is way into undefined behavior. The texto pointer points to char vector[NumHijos];. The variable is initialized with NumHijos = atoi(argv[2]);, where argv[2] is passed 2. So vector is a variable length array with only 2 bytes. Yet you try to write 1000 bytes into the vector with fgets call – this most probably accesses the pointer out of bounds and undefined behavior happens.

Once undefined behavior happens, you can’t have any expectations as to what the program will produce. Probably the fgets function overwrites the memory behind NumHijos variable. Or strcat overwrites it. Debug the program to find out. How to debug small programs.

i don’t know how many characters the var “texto” could have, is there any way I can reprogram this so I don’t have to put that 1000?

Yes. Use getline if you have it, if not rewrite it on your own. Writing a function that reads a file char by char, reallocates some destination pointer and stores read character into that pointer looks like a good C training. Anyway, here’s the libcs50 library with it’s get_string() function which may serve as a good reference how to implement such function.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement