Computing.Net > Forums > Programming > search for words in textfile

search for words in textfile

Reply to Message Icon

Original Message
Name: Ibana
Date: September 6, 2004 at 08:15:59 Pacific
Subject: search for words in textfile
OS: Win XP Pr0 Swe Edition
CPU/Ram: Intel P4 2.4GHz/512 ram
Comment:

Hi there!

I have a problem where I want to search a text file for certain words and then extract data that follows after that word.
For example in the following textfile I would like to extract the integers value by searching for the word QUAD. But the search doesnt work as I want to. Se below please:)

input.txt
-------
THIS IS A SEARCH TEST FOR
THE WORD QUAD 2 2 2 2

-------

I use the following code but it only works if QUAD it the first word on a new line, so it wouldnt find QUAD in the example above as I only search in the beginning of the buffer.

FILE * pFile;
char buffer [64];
char tempbuffer [64];
char string [5]="QUAD";
pFile = fopen ("indata.txt" , "r");

k =strlen(buffer);
n = strlen(string);
std::cout<<"size string="<<n<<" "<<"size buffer="<<k<<"\n";

while(fgets(buffer, 63, pFile)){
if(!_strnicmp(buffer,string, strlen(string))){
points.push_back(i); // add the intgers value into a vector
break;
}
//Need some recursive metohd to test the remaining of the buffer
//Any suggestions?
}


Would be grateful for any help from you guys!



Report Offensive Message For Removal


Response Number 1
Name: wizard-fred
Date: September 7, 2004 at 00:23:45 Pacific
Subject: search for words in textfile
Reply: (edit)

You have another problem, what if the desired string and values are split by the reads.

I don't do C but I had the same problem in BASIC.

What you need is to have a sliding window on the file. You also need to know the maximum length of your "QUAD n n n n" string, 12 unless the n be more than one digit.
Your read buffer should be longer than the length of the "quad" string.

pseudo code
START
open files
read buffer

TEST for "QUAD"
Yes and its position is greater than the max length of a quad string from the end then evaluate
else read another buffer append (except at eof) evaluate
No If EOF then FINISH else read next buffer, append and repeat TEST

EVALUATE Remove character up to and including "QUAD" extract the 4 values and save vector calculation in output file. Repeat TEST

FINISH
close, cleanup, etc.

I don't know the number and frequency of the "quad" values and what constitutes the rest of the file.
If you have control of the writing program, it would be a lot easier if the "quad" line was on a line by itself.


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: search for words in textfile

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge