سؤال

as the problem i was having was assignment related and has been solved i've decided it remove the code. Thanks for the help.

هل كانت مفيدة؟

المحلول

You have char choice[1]; but then you call fgetsTrim(choice, 2). That then calls fgets(choice, 2, stdin) causing a buffer overflow (and thus undefined behaviour).

To fix this, make choice bigger. However your code has a lot of magic numbers in it. Use sizeof where possible, and use #define constants in other places.

BTW you probably want to make it at least 3. If it is size 2, then fgets will only extract the digit the person types, and not the newline. Then your next call to fgets, wherever it may be, will pick up that newline. To be user-friendly you should probably use a fairly large buffer, there's no reason not to.

For example:

char choice[80];
// ...
fgetsTrim(choice, sizeof choice);

For the struct names:

#define STRUCT1_SIZE 20
char struct1[STRUCT1_SIZE] = { 0 };   // don't need to set it to zero later
// ... in function
fgetsTrim(struct1, STRUCT1_SIZE);

Another problem is char password[6] = "Fedora";. This is NOT a string because it is not null-terminated. But you call login(password) which then calls strncmp, a function that expects a null-terminated string. So you have a read buffer overflow.

To fix this, write char password[] = "Fedora"; instead, then the compiler will pick the right size for you.

Edit: The createStruct function has severe issues. It seems as if you think you can use the user's input as a typename... you can't. The line typedef struct fileProperties struct1; means that the token struct1 in your code aliases that struct. Then your printf("%s", struct1... will not compile because struct1 is now a type name.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top