Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
weird problem with strcmp()
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="rabbits77, post: 4052400"] x-no-archive: yes Seebs wrote: [snip] Thanks for the advice. The problem I was seeing came exclusively from the fact that I was trying to read in a single character at a time(remember this is just toy code...I know this is completely inefficient). I was restricting myself to using read() which takes a char* as an arg, not a single char. So, in order to remain faithful to my restriction I used a small char array to read in a single character at a time. I did not correctly add a null termination character '\0' to this array which caused garbage characters to come into play when I then proceeded to use strcat. Anyway, thanks for the help! --------------------- #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <strings.h> #define MAXSIZE 4096 #define ONEBYTE 1 #define NUMBYTES(x) x*2 int execute_command(); int main(){ int n; int num_chars=-1; char char_buf[2]; char* command_line; command_line=(char*)malloc(MAXSIZE); bzero(command_line, MAXSIZE); while((n=read(STDIN_FILENO, char_buf, ONEBYTE))==ONEBYTE){//read one byte at a time char_buf[1]='\0'; num_chars++; if(char_buf[0]=='\n'){ execute_command(command_line); bzero(command_line, MAXSIZE); num_chars=-1; } else{ strcat(command_line,char_buf); } } } int execute_command(char* command_line){ char* command = strtok(command_line, " "); char* argument = strtok(NULL,""); if (!strcmp(command, "run")) { printf("run: \"%s\"\n",command); } if (!strcmp(command, "show")) { printf("show: \"%s\"\n",command); } if (!strcmp(command, "kill")) { printf("kill: \"%s\"\n",command); } } --------------------- $ ./a.out run puke run: "run" show puke show: "show" kill puke kill: "kill" ^C [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
weird problem with strcmp()
Top