Hello, I am a novice in C language and have been trying to find a method of reading/scanning input from GSM, then comparing it against a text (e.g OK or ERROR). Then if true set a break. I have tried scanf with no avail. I have also tried reading string which works but am unsure how to compare with my text.Has anyone any advice with what function would be useful? Any replies greatly appreciated. Andy
Look up strcmp and associated functions in the Standard 'C' library.
I can get string by using gets() but can only compare two strings that are entered but not read. Any ideas to how to get round this? Do I have to place read string into memory etc?
"I can get string by using gets() but can only compare two strings that are entered but not read." I'm afraid I don't really understand your question. At a guess the GPS outputs a variety of (NMEA0183?) strings and you are trying to identify the one of interest before sucking the data out of it. "Do I have to place read string into memory etc?" Yes. It is probably easier to write your own routine to read in the string. That way you can detect the start and end characters to ensure that you capture one complete string with everything in the right place. You should then be able to use sscanf() to extract the data. Stefan
An example of the code I was trying to use is belo. It does not work. I will try sscanf now. Thanks. #include <stdio.h> /* standard I/O .h-file */ #include <reg167.h> /* special function register 80C167 */ #include <string.h> { char arr[50]; gets(arr); printf("\nInput read into array was:\n"); //* just for confirmation*// puts(arr); char *string[1] = {"OK"} ; //* string 1 = OK (text) char *string[2] = gets(arr); int strcmp(string[1], char* arr); } {} { } }
please remember to use the < pre > and < /pre > tags for code - as it says in the instructions when you make a post! You are confusing your arrays and pointers - you need to re-read the section on strings in your 'C' textbook. The code you've shown won't compile, not least because you have defined the symbol 'string' twice:
char *string[1] = {"OK"} ; //* string 1 = OK (text)
char *string[2] = gets(arr);
View all questions in Keil forum