سؤال

I can't figure out why my string compare isn't comparing correctly. This is for C.

It's reading from a file that is set up like this:

1 - ls              
2 - cd                   
3 - history               

If I type !c it's suppose to grab the last used string that started with 'c' and run the command. Yet it never goes into the if(strcmp(())=0) line.

Part of my code is here:

  char currLine[MAXINPUTLINE];

    else if (isalpha(input[1])){
        int count = 1;
        fileRead = fopen(".simpleshell_history", "r");
        while(fscanf(fileRead, "%s\n", currLine) != EOF){
            printf(input+1);
            printf(currLine);
            if(strcmp((input+1), currLine) == 0){
                printf("%s\n", currLine);
                parse(currLine);
            }
        }
    }

This is what the printf in the while loop prints, I can't figure out how to fix this and I've been stuck on it for a while. This is when I enter '!c'

 c
 1c
 -c
 lsc
 2c
 -c
 cdc
 3c
 -c
 historyc
 4c
 -c
 !c!c
هل كانت مفيدة؟

المحلول 2

If input is the string !c and you are looking to match that against the line 2 - cd, you will have to be careful. strcmp will definitely not work, as it will only return success if the two strings it is comparing are exact matches.

In order to test whether one string (cd) starts with another string (c), you want to use strncmp(), which will take a limit on the number of characters to compare.

Also: you will need to be careful to start comparing from the second character of input (skipping the !) and from the fifth character of currLine (skipping the 2 - characters).

This should get you there:

    while (fgets(currLine, sizeof currLine, fileRead) != NULL) {
        printf(input+1);
        printf(currLine);
        if (strncmp(input + 1, currLine + 4, strlen(input)-1) == 0) {
            printf("%s\n", currLine);
            parse(currLine);
        }
    }

نصائح أخرى

This loop:

while(fscanf(fileRead, "%s\n", currLine) != EOF) {

will read whitespace delimeted tokens into currLine rather than lines. So the first iteration will be 1, the second -, the 3rd ls, etc as you are seeing with your printfs. The name currLine suggests you want to read lines rather than tokens.

You then compare the read tokens with the rest of your input line which is apparently "c\n". Since you never get a token with a newline, it never matches. Even if you got rid of the newline, it would never match, as your file does not contain the token c

edit

You say you want to compare the rest of the input line against a prefix of the command on the line. To do that you want to first figure out how long the prefix is, then use strncmp. You also want to parse the line from the file to separate the command from the index. So you could do something like:

else if (isalpha(input[1])){
    int count = 1;
    int pfxlen = 1;
    while (!isspace(input[pfxlen+1])) pfxlen++;
    fileRead = fopen(".simpleshell_history", "r");
    while(fscanf(fileRead, "%d - %[^\n]", &index, currLine) == 2) {
        if(strncmp((input+1), currLine, pfxlen) == 0) {
            printf("%s\n", currLine);
            parse(currLine);
        }
    }
}

One possibility is that input may contain a trailing newline, while curline definitely will not because of the scanf specification.

Your problem is with the manner in which you get input. (notice when you print, it has a line feed). It has a trailing \n and your currLine does not. Hence the compare fails.

Suggest OP uses fgets() for both user and file input.

Something like

char buf[MAXINPUTLINE];
while(fgets(buf, sizeof buf, fileRead) != NULL) {
  int LineNo;
  if (sscanf(buf, "%d - %s", &LineNo, currLine) != 2) Handle_UnexpectedInput();
  ...
}

Note: "%s\n" does the same as "%s " does the same as "%s\t": %s skips optional leading whitespace, then scans non-whitespace. The whitespace after s in "%s\n" scans for optional whitespace.

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