strcmp没有按照它应该的方式工作 [英] strcmp is not working the way it should

查看:141
本文介绍了strcmp没有按照它应该的方式工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c中的strcmp的返回值是什么。我在c中写了下面的代码。



What is the return value of strcmp in c.I wrote the following code in c.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/resource.h>
#include<stdarg.h>
#define MAX 128
char *path = "/bin/";
char *argument[MAX];
char buffer[MAX];
int numberOfArguments;
pid_t pid;
  int rv ;    
char prog[128];

char parse(char buffer[],int i)
{

             buffer[i] = ''\0'';
             argument[0] = strtok(buffer, " ");
             numberOfArguments = 1;
             argument[numberOfArguments] = strtok(NULL, " ");
             while (argument[numberOfArguments] != NULL) {
             numberOfArguments++;
             argument[numberOfArguments] = strtok(NULL, " ");
                           }  
                                
  return  numberOfArguments;                               
}
int changeDirectory( ){
   int result = 0;  
    if (numberOfArguments > 2) {
              printf("%s: Too many operands \nUsage: %s <pathname>\n", (char *) argument[0], (char *) argument[0]);
                 }
   if(numberOfArguments == 1) {
               
                const char* home = getenv("HOME");
                int i = chdir(home);
                if(i < 0)
                       printf("directory couldn''t be changed\n");
                else{
                        printf("directory changed\n");
                        printf("home = %s\n", home);
                    }
         }
   else {
             result = chdir(argument[1]);
              if(result == 0){
                      printf("directory changed\n");
                      exit(0);
                     }
              else{
                       printf("Couldn''t change directory to %4s", (char *)argument[1] ); 
          }
       }
return 0;
}
void forground(char buffer[], int i){ 
                         parse(buffer,i);         
                                 char prog[128];
                                  strcpy(prog, path);
                                  strcat(prog,argument[0]);
                                  printf("%s\n",prog);  
                                 rv = execv(prog, argument);
                                /* int l=0;
                                   while(l<numberOfArguments+1){
                                   printf("argument [%d] is %s\n",l,argument[l]);
                                     l++;
                                        } */
}
int main(){
char *s;
while(1)
       {
            printf("myshell>> ");  
            int i = 0; 
         
             while ((i < MAX) && ((buffer[i] = getchar()) != ''\n'') && buffer[i] != EOF)
                i++;
               if (i == MAX) {
                          buffer[MAX-1] = ''\0'';
                          while (getchar() != ''\n'');
                          printf("argument too long\n");
                       }
              else {
                        pid = fork();
                        if (pid != 0){
                                  wait(NULL);
                               }
                        if (pid != 0) {            
                                
                             forground(buffer,i);
                           }
             
          parse(buffer,i); 
     
          if (!strcmp(argument[0], "cd"))
          changeDirectory();
               else  if (!strcmp(argument[0], "exit")) {
      	                int ret;         
                        ret = execl ("/bin/bash", "bash",(char *)0);
                          }  
          
process();    
            }
 }
return 0;  
}



在argv [0]之后输出总是空的。这意味着它在cd之后失去了输入。这是什么意思strcmp将cd之后的其他valus转为null ???


the out put is always Null after argv[0].That means some how it is loosing the input after "cd".Why is that does strcmp turn the other valus after cd to null???

推荐答案

只需从你的代码片段中工作,很难弄清楚你的问题是什么:唯一的参考strcmp你根本不使用argv - 它使用你创建和填充的字符串数组。



但是你确定你正在使用它吗? />
Working just from your code fragment, it is difficult to work out what your problem is: the only reference to strcmp you have does not use argv at all - it uses an array of strings you create and fill.

But are you sure you are using it right?
strcmp(argument[0], "cd")


如果字符串相同,
将返回0,所以


will return 0 if the strings are the same, so

if (!strcmp(argument[0], "cd"))
  changeDirectory(buffer,i);

将改变如果争论是不cd



我认为这不是你想要的......



使用调试器 - 逐步执行代码并查看发生了什么。在这里询问会更容易,更快 - 特别是如果你没有给我们正确的信息! :笑:



C真是非零,不知道为什么我错了...- OriginalGriff [/ edit]

will change the directory if the arguement is not "cd"

I don''t think that is quite what you intended...

Use the debugger - step through you code and see what is going on. It''s a lot easier and quicker that asking here - particularly if you don''t give us the right info! :laugh:

[edit]C true is non-zero, dunno why I got that wrong...- OriginalGriff[/edit]




  1. 您应该始终查看文档 [ ^ ] 。如您所见, strcmp()函数返回一个整数值,而不是代码所假设的布尔值。
  2. 没有变量的定义(代码中的 buffer 参数);它们在哪里定义?
  3. 在调用解析() strcmp()函数$ c>功能。

  1. You should always check the documentation[^] first. As you can see, the strcmp() function returns an integer value not a boolean as your code assumes.
  2. There is no definition of the variables (buffer, argument) in your code; where are they defined?
  3. You are calling the strcmp() function before you call your parse() function.


这篇关于strcmp没有按照它应该的方式工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆