0

A function in my program, choice_move_to_position, enables to move the virtual cursor in an open file to a specific position. For example if I type 56, the cursor will be put at the position 56 of the file (which means the 57th character). I tested it and found no problem, it seemed to work perfectly. However, I used it to debug another function, but choice_move_to_position failed. After it failed once, I tried to use it again and it worked.

Here is my function, and two other functions that may cause the problem, yet I can't get what's wrong.

int     get_nb(char *str) // This function converts a string into an integer
                          // In this program, get_nb will always receive a string
                          // corresponding to a real number: the input is formatted.
{

    int     i = 0;
    int     nb = 0;

    if (str[0] == '-')
        i++;
    while (str[i] != '\n')
    {
        nb *= 10;
        nb += str[i] - 48;
        i++;
    }
    if (str[0] == '-')
        return -nb
    return nb;

}

void    get_new_buffer(char *str, int max_size) // Resets the buffer and the string
                                                // in which the buffer will be stored,
                                                // then get an input
{
    char    c;
    int     i = 0;

    // If we have a buffer overflow, the function resets the buffer
    if (str[max_size - 2] != '\n' && str[max_size - 2] != '\0')
        while ((c = getchar()) != '\n' && c != EOF);
    // The following resets the string in which will be stored the buffer
    while (str[i])
    {
        str[i] = '\0';
        i++;
    }
    // The buffer and the string are clean, we can call fgets
    fgets(str, max_size, stdin);
}

void    choice_move_to_position(FILE *file)
{
    char    choice[12] = {'\0'};

    printf("Select the position to get.\n");
    get_new_buffer(choice, 12);
    // The following loop checks 3 things:
    //     the input must be an integer (positive or negative)
    // AND the input must not be larger than 11 characters or smaller than 2 characters (including the ending newline)
    // AND the input must correspond to a valid position
    while (check_int_buffer(choice, 1) != 0 ||
           check_size_buffer(choice, 12, 2, "", "") != 0 ||
           check_position(file, ftell(file), choice) != 0)
    {
        printf("Please select a valid integer.\n");
        get_new_buffer(choice, 12);
    }
    fseek(file, get_nb(choice), SEEK_SET); // Go to the selected position
    printf("New position is: %ld.\n", ftell(file));
}

I wanted to go to the position 10 (11th character) in a file that contains 11 characters. The output was the following:

 - What do you want to do ? (help) // this is a printf
move to position                   // this is a user input
 - Select the position to get      // this is a printf
10                                 // this is a user input
 - New position is: 0              // this is a printf using the function ftell

Please note that there is no error message, which means that the input was correctly formatted.

I checked everything, tested it alot and I still can't get any answer. I would like to know if the bug I had have something to do with my program, or if it could be material. I once had a program that had like 95% chance of working and eventually, randomly, it didn't. It was due to a bad management of the memory I used (no protection). In this program it seems to me that I have protected every bit of code that needed to be protected. Please share your thoughts about this. Is it possible that the problem is material, or does my function have some flaws?

nounoursnoir
  • 671
  • 2
  • 10
  • 25
  • `nb += str[i] != '\n';` - what, dare I ask, is *that* supposed to be doing ? Since `str[i] != '\n'` is the condition of your while-loop (which you *inside of*), it must be true, so was your intent to simply be `nb += 1;` ? A *debugger* certainly seems appropriate for further investigation. – WhozCraig May 17 '17 at 11:58
  • eeww that's a mistake, this is not what I wrote in my code I don't know how I managed to write this here. I edit. – nounoursnoir May 17 '17 at 12:02
  • Your example is missing all of your check functions. I'd start by checking the return value of fseek, and verifying that the output of get_nb is what you are expecting. – Retired Ninja May 17 '17 at 12:09
  • Since the checking functions does only that: checking, and always return 0, I thought that it was of no use to show them, and worst, it would be some floody code. I had put a few more lines in my code to check the return value of fseek, but I did it after the fail happened. Of course, when it doesn't fail, the return value is 0. Yet I can't make the bug happen again... But it happened once! I'm really confused about this – nounoursnoir May 17 '17 at 12:16
  • 1
    `return` is not a function. [Don't use it with parentheses like that](http://stackoverflow.com/q/4762662/995714) – phuclv May 17 '17 at 12:41
  • it should be the same with other statements then? like `if`, `while`? – nounoursnoir May 17 '17 at 14:28
  • `if` and `while` are control blocks which have different syntax. `return` is just a single statement – phuclv May 17 '17 at 15:31

0 Answers0