No it won't. You will have to do something like this.
KurtCode:if (file.is_open()) { while (file.getline( array, sizeof(array) )) { char * ch = array; while((ch = strstr( ch, "hello james")) != 0) { count++; ch++; } } }
No it won't. You will have to do something like this.
KurtCode:if (file.is_open()) { while (file.getline( array, sizeof(array) )) { char * ch = array; while((ch = strstr( ch, "hello james")) != 0) { count++; ch++; } } }
Last edited by ZuK; 02-17-2006 at 02:30 PM.
Thanks how come the pointer doesn't have the new and delete key words? and what does the ch variable do?Originally Posted by ZuK
Is this how the code would be developed to work with other words?
Thanks.Code:if (file.is_open()) { while (file.getline( array, sizeof(array) )) { char * ch = array; while((ch = strstr( ch, "hello james")) != 0) count++; ch++; //uses same char pointer? while((ch = strstr( ch, "hello david")) != 0) count++; ch++;
Originally Posted by 182>> how come the pointer doesn't have the new and delete key words? and what does the ch variable do?Code:if (file.is_open()) { while (file.getline( array, sizeof(array) )) { char * ch = array; while((ch = strstr( ch, "hello james")) != 0) { count++; ch++; } //uses same char pointer? //yes the pointer can be recycled but it has to be reset to the beginning //of the array before the next string can be searched ch = array; while((ch = strstr( ch, "hello david")) != 0) { count++; ch++; } } }
ch is just a helper variable the points to where stsstr() should start searching.
No space was allocated for ch so it doesn't have to be deleted.
Kurt
Last edited by ZuK; 02-17-2006 at 02:29 PM.
Thanks for that and sorry if these newbie questions are annoying but how come the ch variable is incremented as well as the count variable?
Thanks again.
by incrementing the pointer ch it will point to the character after the position where the string was found. Otherwise it would not stop to find the same location over and over again.
Kurt
I see, thanks
So even with the pointer variable is it still ok to keep the old code and just close and open the file again with the same ifstream variable and add this new code or would I need a new ifstream variable?
It's no problem to recycle variables. Works fine with streams if you properly close them before opening them again. That has noting to do with what you do with the data that you read from the stream.
I really don't understand what you are worried about.
Kurt
Thanks again for all your help