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?