saiz66 Posted October 4, 2004 Share Posted October 4, 2004 How do I search a file for a specified keyword? Thanks a lot!!! P.s. I am new to files. Link to comment Share on other sites More sharing options...
0 Mouton Posted October 4, 2004 Share Posted October 4, 2004 fopen is a good start. fgets might help too. Link to comment Share on other sites More sharing options...
0 saiz66 Posted October 5, 2004 Author Share Posted October 5, 2004 thanks.. but I am not sure how to traverse a file with spaces, tabs and new lines to find the specific word... Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted October 5, 2004 Share Posted October 5, 2004 Read it in as a string, and use char *strstr(const char *s1, const char *s2) to locate s2 in s1. Although I guess it depends on what you what to do with the result, this returns s1 + x, where x is the offset of the location of substring s2. Link to comment Share on other sites More sharing options...
0 Mouton Posted October 5, 2004 Share Posted October 5, 2004 thanks.. but I am not sure how to traverse a file with spaces, tabs and new lines to find the specific word... strtok allows u to tokenize but strstr is useful too to find a substring; it's case-sensitive btw. Link to comment Share on other sites More sharing options...
0 saiz66 Posted October 5, 2004 Author Share Posted October 5, 2004 thanks.. but what if i wanted to find a word to replace with a different word in a file? would i still use strstr? Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted October 5, 2004 Share Posted October 5, 2004 thanks.. but what if i wanted to find a word to replace with a different word in a file? would i still use strstr? That's getting a little tricker, you'd have to make a temp string that starts with the string up until that place and then concatenate the replacement word onto it and then concatenate what's after the word you're replacing. I'd use strstr to find the place in the string where that word is. Link to comment Share on other sites More sharing options...
0 saiz66 Posted October 5, 2004 Author Share Posted October 5, 2004 also i could use the strstr to find the searched word more than once right?? Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted October 5, 2004 Share Posted October 5, 2004 also i could use the strstr to find the searched word more than once right?? Sure, you can run strstr on the newly created string when you replace the first occurence of it. Link to comment Share on other sites More sharing options...
0 saiz66 Posted October 5, 2004 Author Share Posted October 5, 2004 i am having some problem reading a flie.. here is my code so far: #include <stdio.h> int main() { char c[10]; FILE *file; file = fopen("c:\\file.txt", "r"); if(file==NULL) { printf("Error: can't open file.\n"); return 1; } else { printf("File opened successfully. Contents:\n\n"); while(fgets(c, 10, file)!=NULL) { printf("String: %s", c); } printf("\n\nNow closing file...\n"); fclose(file); return 0; } } for some reason it keeps on saying "Error: can't open file." Even though I have a c:\file.txt and some text in it. Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted October 5, 2004 Share Posted October 5, 2004 (edited) Odd, works for me. You may also want to try reading the entire file at once. Here's an example of using fread: http://www.harpercollege.edu/bus-ss/cis/16...lect18/l18i.htm Edited October 5, 2004 by kjordan2001 Link to comment Share on other sites More sharing options...
0 saiz66 Posted October 5, 2004 Author Share Posted October 5, 2004 ok i figured out what was wrong with it... i had a file.txt.txt.. damn u notepad!!! now i just got to figure out the strstr thing. Link to comment Share on other sites More sharing options...
Question
saiz66
How do I search a file for a specified keyword? Thanks a lot!!!
P.s. I am new to files.
Link to comment
Share on other sites
11 answers to this question
Recommended Posts