简体   繁体   中英

strcmp() in C not giving accurate value when run on linux server

I am trying to compare two strings in my c program with a.csv files input. Everything works fine when I run it on my localhost as a cgi-program. But upon uploading on to linux server it doesn't compare properly. The output file shows despite being the same value it's compared output is different. And I think the linux server is running C99 compiler for C. I have only pasted the code where it creates the problem. If you want to take a look at the full code follow the link please https://github.com/iaminhri/CGI-Programming/blob/main/checkpass2.c Following codes. I am stuck with this:')... Please let me know if you need any other information.

int k = 0;
for(; k < lineCounter; k++){
    char *trimmed = strtok(pwd[k], "\n ");
    doesUsrExists = strcmp(arr[0], usr[k]);
    doesPwdExists = strcmp(arr[1], pwd[k]);
    printf("<p> UserExists: %s -- PwdExists: %s", usr[k], pwd[k]);
    printf("<p> UserExists: %d -- PwdExists: %d", doesUsrExists, doesPwdExists);
    if(doesPwdExists == 0 && doesUsrExists == 0){
        bothExists = 0;
        break;
    }
    else
        bothExists = 1;

    doesUsrExists = 0;
    doesPwdExists = 0;
}



printf("<p> UserExists: %d", bothExists);

if(bothExists == 0){
    printf("<h1>Your Password Matches</h1>");
}
else{
    printf("<h1>Wrong username or password</h1>");
}
printf("</body></html>");

the strcmp(arr[1], pwd[k]); returns different values even though the strings matches.

The output is as follows: 在此处输入图像描述

strcmp will work only correct, if both character string are terminated with an null character. If they are missing, the comperation will be failed and furthemore you can corrupt the system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM