Searching txt files

Started by
12 comments, last by rAm_y_ 9 years, 9 months ago

Just for clairfication I'm using c++ so C looks a bit different to me but I'm sure I can figure it out.


and under "cbl" is the cable numbers (none are going to be integers all are strings).

Clarify this statement. You say none are going to be integers, yet your example text contains only integers. When it comes to string parsing, the details are essential.

Everything in my txt right now are going to be used as strings. Even though it looks like a number, I am only worried about passing it as strings of txt for now. I'll soon be searching txt for numerical values and sorting them for other purposes but right now I'm going step by step trying to get string positions and minuplation.

Where I'm at now is a bit confusing. For example. I'm unsure how to get to the line under the "cbl" and to stop once it gets to another remote number.


if (mystring.find ("r=") != string::npos){
		rString_pos = mystring.npos + 2;
		rname = mystring.substr(rString_pos+1, 4);
		cout <<rname<<endl;		
		}

The above finds the string "r=" and moves the npos to the character afterwards then I put the remaining line into rname.

But I can't do the same for cbl. Even the above seems wrong or around-the-way (but it works). I'm thinking of using a while loop but i'm unsure how to write the correct loop with the right paramerters to look for. I tried getline() but that doesn't work because I don't know how to get the string from the line into my string and to continue until it reaches another keyword.

Advertisement

string::npos shouldn't be used as a position. It's a constant normally used as an indication (as mentioned above) that the search failed.

Try something like:


char buf[256];
file.getline(buf, 256, '\n');
mystring = buf;

int pos;
if( string::npos != (pos = mystring.find( "R=", 0 ) ) )
{
   rString_pos = pos + 2;
   rname = mystring.substring( pos+2, mystring.length() - rString_pos );
   cout << rname << endl;
}

Also, your original post has "R<space>=" with a capital "R" and a <space>. Be sure to test for all cases if you're not sure of the format of the text.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

psuedo-code:


string inLine
string prefix
while not eof
	getline inLine
	if inLine[0] = 'R'
		prefix = substring(inLine ... )
		continue
	endif
	if inLine[0] = 'c'
		 write out empty line
		continue
	endif
	if size of inLine > 0
		write out prefix " " inLine
	endif
wend

Basically, you have 3 scenarios:

  • whenever you come across an R line, parse out the value you want to retain and assign it to prefix (to late be used for output)
  • whenever you come across a cbl line, write out an empty line (your example output had space between 'sections').
  • whenever you have a non-empty line that isn't an R or cbl, output prefix + line

The continues skip any further processing.

Parsing/tokenizing all depends on consistency of the data your parsing. You look for a common delimiter and work from there. You could then use a Vector array to store you data and print it out.

This topic is closed to new replies.

Advertisement