简体   繁体   English

C 中的 strcmp() 在 linux 服务器上运行时未给出准确值

[英]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.我正在尝试将我的 c 程序中的两个字符串与 a.csv 文件输入进行比较。 Everything works fine when I run it on my localhost as a cgi-program.当我在本地主机上将它作为 cgi 程序运行时,一切正常。 But upon uploading on to linux server it doesn't compare properly.但是在上传到 linux 服务器后,它无法正确比较。 The output file shows despite being the same value it's compared output is different. output 文件显示尽管它的值相同,但与 output 相比是不同的。 And I think the linux server is running C99 compiler for C. I have only pasted the code where it creates the problem.而且我认为 linux 服务器正在为 C 运行 C99 编译器。我只粘贴了产生问题的代码。 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.如果您想查看完整代码,请点击链接https://github.com/iaminhri/CGI-Programming/blob/main/checkpass2.c以下代码。 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]); strcmp(arr[1], pwd[k]); returns different values even though the strings matches.即使字符串匹配,也会返回不同的值。

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

strcmp will work only correct, if both character string are terminated with an null character.如果两个字符串都以 null 字符结尾,则 strcmp 将仅正确工作。 If they are missing, the comperation will be failed and furthemore you can corrupt the system.如果它们丢失,则比较将失败,而且您可能会破坏系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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