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"); }
You are making the assumption that he did actually bother to debug it...
No, I am making the assumption that he did consider debugging to be too boring and decided to distribute the task to "the net". This is not the type of bug someone can fail to notice if actually starting their debugger. This is not the type of bug someone can fail to notice if even trying to follow the logic with pen and paper. Or just carefully reading the code.
Dear Per Westermark & Andrew Neil,
First of all thanks for your valuable replies. My problem got solved. It not that I am not aware of debuggers and I am lazy.. but I have made series of mistakes. But again u people have helped much.
Per Westermark you have given first clue to me in your comment.
I have used “sprint(....)” just to show u guys my buffer. Originally I check my buffer array in my application (yes in debugger) and found that my buffer lines have not been separated with null character.
So now I don’t need to find the line number. So as u suggested “Next thing - your goal is to return the value 2, enumerating the text lines 0, 1, 2, 3, ...But your loop is iterating characters. So if it did reach "45\r\n", it would not return 2. It would return 10.”. I have to manage with the character index from where my substring ‘T’ is started. So now as u see below in my code now I will have answer 10(now it is 8 as we remove null characters. See below).
Problem of null characters: And again it solved from your query “Can you tell me how you expect to reach your "45\r\n" when you have two string terminations before that substring?”. Thanks again for that.
again in line
for(i=0; S[i]; i++) { if(strncmp(&S[i],T,l)) return i; }
i have to add "== 0"
So My new code:
int16 stringIndex(uint8 * S,uint8 * T) { uint16 i,l; l=strlen(T); for(i=0; S[i]; i++) { if(strncmp(&S[i],T,l) == 0) return i; } return -1; } void main(void { uint8 A[25]; uint16 stringIndexValue=0; sprintf( A,"01\r\n23\r\n45\r\n67\r\n"); stringIndexValue = stringIndex(A,"45\r\n"); }
I got my new ans 8
But didn't you catch this when you debugged your code? Exactly what did you find your program doing when you debugged it???. Yes dear I got it from debbuger. Thanks again.
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.