1

I have been trying to get strcmp to return true in the following program for many days now, I have read the man pages for strcmp, read, write... I have other's posts who have had the exact same problem. The following source code is just a test program that is frustrating the heck out of me, there are some commented out lines that are other attempts I've made at getting strcmp to work as expected. I have compiled with 'gdb -g' and stepped through one instruction at a time. The printf statements pretty much tell the whole story. I cannot get the value of buf, or bufptr to equal 't' ever. I have simplified the program, and had it just print one character at a time one after the other to the screen and they print as expected from whatever file is read-in, however, as soon as I start playing with strcmp, things get crazy. I cannot for the life of me figure out a way to get the value in buf to be the single char that I am expecting it to be. When simplified to just the write(1,...) call, it writes the expected single char to stdout, but strcmp to a single 't' never returns 0. !!!!! Thank you in advance. I originally didnt have bufptr in there and was doing a strcmp to buf itself and also tried using bufptr[0] = buf[0] and the still were not the same.

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

#define BUF_SIZE 1

void main(int argc, char *argv[])
{

    char buf[BUF_SIZE];
    int inputFd = open(argv[1], O_RDONLY);
    char tee[] = "t";
    int fff = 999;
    char bufptr[BUF_SIZE];
//  char *bufptr[BUF_SIZE];

    while (read(inputFd, buf, BUF_SIZE) > 0) {
            bufptr[0] = buf[0];
//          bufptr = buf;
            printf("********STRCMP RETURNED->%d\n", fff); // for debugging purposes
            printf("--------tee is -> %s\n", tee);        // for debugging purposes
            printf("++++++++buf is -> %s\n", buf);        // "  "   "
            printf("@@@@@@@@bufptr is -> %s", bufptr);    // "  "   "
            write (1, buf, BUF_SIZE);


            if ((fff = strcmp(tee, bufptr)) == 0)
                printf("THIS CHARACTER IS A T");
    }

    close(inputFd);

}
Ostküste
  • 23
  • 6
  • 1
    Try `bufptr[1] = '\0';`.. – Eugene Sh. Jun 06 '17 at 22:17
  • 1
    @EugeneSh. `#define BUF_SIZE 1`. There is no room for the null. – clcto Jun 06 '17 at 22:19
  • 2
    @clcto Oh.. then we are doomed. – Eugene Sh. Jun 06 '17 at 22:19
  • Read the documentation of strcmp . You will find that it operates on *strings*. You did not provide strings as arguments – M.M Jun 06 '17 at 22:24
  • I have read the man page...The value of buf works in write which is expecting a const void *buf and strcmp expects const char *sd. How is it working as const void *buf? It is declared as char, shouldn't it work where strcmp is expecting const char *something? If that is not what strcmp is expecting, in what way do I declare/initialize buf in order to store one char at a time from a file and compare to another char? – Ostküste Jun 06 '17 at 22:45
  • So what does it print? – user253751 Jun 06 '17 at 22:47
  • @immibis updated with the first dozen lines of printout. It is awaiting peer approval. – Ostküste Jun 07 '17 at 01:18

1 Answers1

2

The str-family of functions expects strings as inputs, which are arrays storing null-terminated character sequences. However, you do not provide space in the buffer for the null character. To make the buffers strings, you need to add space for the null-character and zero-out the value so that they end with the null character.

void main(int argc, char *argv[])
{
    char buf[ BUF_SIZE + 1 ] = {0};
    int inputFd = open(argv[1], O_RDONLY);
    char tee[] = "t";

    while (read(inputFd, buf, BUF_SIZE) > 0) {
        if ( strcmp( tee, buf ) == 0 )
            printf("THIS CHARACTER IS A T");
    }

    close(inputFd);
}
clcto
  • 9,530
  • 20
  • 42
  • @M.M Good point. Updated to add space for the null so that it could be used with varied BUF_SIZE. – clcto Jun 06 '17 at 22:36
  • I initialized buf and bufptr using buf[ BUF_SIZE + 1 ] = {0} and have gotten strcmp to return 0. Thank you for the help and suggestions. I will have to continue to debug from here to fully wrap my head around why exactly it works so I can naturally do it that way in the future. Thanks again all. – Ostküste Jun 06 '17 at 22:57
  • @Ostküste Or you could *stop guessing* and *start reading* (a decent book)... The reason I know you're not reading a recommended book is that the people who read recommended books don't make this kind of basic mistake. – autistic Jun 07 '17 at 05:46
  • 1
    @clcto I'm afraid you forgot to compare the result of `strcmp()` with 0. It should be `strcmp(tee, buf) == 0`, shouldn't it? (Otherwise, the sample will anything else except `"t"` report as "A T".) – Scheff's Cat Jun 07 '17 at 06:15
  • @Seb I am currently working through Learning C the Hard Way, Beej's Guide to C Programming, and Advanced Linux Programming. I am constantly reading but I may not be as smart as you... sorry. "The reason I know..." We've actually never met, so you don't know anything about me. Just because I haven't figured something out yet, doesn't mean I am not in the process of it. I was just looking for an explanation from a human's perspective...obviously not you. – Ostküste Jun 07 '17 at 17:41
  • @Ostküste If you want to learn C the hard way, don't bother reading... just *guess*. You'll write plenty of code with non-portable assumptions in mind, [just like "Learn C the hard way" would teach you](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list#comment14179652_8924274). None of the other books you listed appear to be anywhere in sight on that page, so I can recommend [starting from the top](https://stackoverflow.com/a/562377/1989425). – autistic Jun 08 '17 at 03:29
  • @Ostküste *"We've actually never met, so you don't know anything about me."* Under that line of reasoning, if you've never met Bill Gates you know nothing about him; you likely do know *some things* about Bill Gates, regardless of whether you've met him, based on what other people have told you and perhaps based on what he's put on the internet. Similarly, you're putting information on the internet, and I can make deductions based on that information. On the topic of deductions, [where did you learn that `void main` is acceptable?](http://port70.net/~nsz/c/c11/n1570.html#5.1.2.2.1) – autistic Jun 08 '17 at 03:36