As part of a problem, I have a struct which contains char isGood and char filename[32]
There is a function that checks if a file contains the string I'm a virus and return the value in isGood.
Then there is the following code:
strncpy(currentItem.filename, argv[i], sizeof(currentItem.filename));
if (strlen(argv[i]) > sizeof(currentItem.filename)) {
currentItem.filename[sizeof(currentItem.filename)] = '\0';
}
This code causes an error that a file with a name larger than 32 characters will always return "OK" even when the file contains the string I'm a virus.
Why does this happen? Why changing the currentItem.filename changes the value of currentItem.isGood?? It seems to be related to the strncpy because the next question I have to answer says "What is the proper way of ending a string copied with strncpy?"
sizeof(char)is 1, by definition.}>compare is wrong (off by one), thecurrentItem.filename[sizeof(currentItem.filename)] = '\0';is wrong too (off by one)