We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I have a buffer which contains bunch of data. I want to take out the index at some desired string stored on which line number in that buffer. for ex
buffer A = {'0','1',0x0D,0x0A,0, '2','3',0x0D,0x0A,0, '4','5',0x0D,0x0A,0, '6','7',0x0D,0x0A,0}
I want to point the line location at which string "45\r\n" stored in this buffer
so the answer is 2.
here is my code.
but every time I am getting answer 0. Please help
int16 stringIndex(uint8 * S,uint8 * T) { uint16 i,l;
l=strlen(T);
for(i=0; * (S+i); i++) { if(strncmp(S+i,T,l)) return i; }
return -1; }
void main(void {
uint8 A[25];
sprintf( A,"01\r\n%c23\r\n%c45\r\n%c67\r\n%c,0,0,0,0");
stringIndexValue = stringIndex(A,"45\r\n"); }
But you still use sprintf() when all you want is to perform a string copy.
The difference between sprintf() and strcpy() is that sprintf() looks at every character in the source string, trying to find formatting characters. So if there happens to be a '%' in that string, it'll come back and bite you.
Don't do any formatted print unless you really need any formatted print. Do use strcpy() if what you want is to copy a string - sometimes strncpy() instead.
dear Per Westermark,
Thanks for your suggetion. I got you. In original application I will never use any prints untill necessory. In my application data inserted in this buffer via SPI slave reading, where I have not used any printf. but thanks again for the point.