Hi
any help it should be appreciable.
Hi
any help it should be appreciable.
Last edited by munna_dude; 06-16-2010 at 12:10 PM.
I would approach it like this I think:
Code:char *str = "Dream as if you will hello world forever, hello world as if you will die today"; char *hello = "ello world"; int i, j, found = 0; for(i = 0; i < strlen(str); i++){ if(str[i] == 'h'){ i++; for(j = 0; j < strlen(hello); j++, i++){ if(str[i] != hello[j]){ found--; break; } } found++; } }
Cprogramming.com - Tutorials - C File I/O would be a good place to start
Look at your code. It's reading all the lines of the file, into the very same char array, constantly over writing the previous line of text.
Until only the last line is left intact.
Preferred is just:
Note where the (( and )) are placed!Code:while((fgets(line, 256, database)) != NULL) { //rest of your code in here to handle the line array }
Last edited by Adak; 06-16-2010 at 01:33 AM.
Ok, reading this line "i have read the entire file and search the string." I though you had already read it from a file.
You need to open the file for reading and read into a buffer. The best is probably to allocate memory for it with malloc, if the file is reasonably sized. You can do that by seeking to the end and use ftell to get the size.
Skip the feof part, you should be able to manage with only testing for NULL from fgets().
That is very likely the problem, but it's not been posted in context with the searching. It's possible to do like that if the fgets call is inside the search loop.
What are you trying to do? What is r??
But you read one line of 256 chars, then you enter an infinite loop with your while(1), so it's not reading more than one time in your case.
If the text appears like it does in your first post it's likely the new line characters that mess this up. Hello world broken up on two lines will give you "h\nello world" for example. You actually only have two occurrences of the string 'r' in the file.
yes! the one.txt is as it is.
can you please run both the programs wich i posted. the first one is with out file concept. it giving the out put has "4".
but with file concept program is giving the output "2".
i have to use same text in file concept.( like messup, broken line and all)
Everything is as it is. You have never posted one.txt, we can only assume here.
So, you are going to have line breaks in there. Then you need to strip them out before doing any comparison. But there is still a problem if you supply a text that is larger than 256 and 'hello' is the last of one read and 'world' is the first of the next.