Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting string/info from a file?

Status
Not open for further replies.

801119

Programmer
Apr 10, 2000
311
SE
If i got a file Called 'info.ini'<br>with the following contents (se below)<br>info.ini (file)<br>###Dummie text###<br>;Not intersting<br><br>@Input1=&quot;Get from this line&quot;<br>@input2=&quot;Get from this line to&quot;<br><br>File 'info.ini' above this line.<br>How do I extract information from a specific line in this file? It would be able to read all lines starting with a '@' and ignoring lines starting with '#' or ';'<br>Can some one write a quick code for this? Or just help me getting started with extracting specific information from a file? <br><br>Thanx! <br>&nbsp;&nbsp;&nbsp;The Philosopher [801119] <p>Martin G Broman<br><a href=mailto:mgb_svea@hotmail.com>mgb_svea@hotmail.com</a><br><a href= > </a><br>DWS - Alpha Whitin Dead Wolf Society
 
&nbsp;&nbsp;&nbsp;&nbsp;There are several ways to do this. I prefer using streams. You will need to include the correct header file:<br>#include &lt;fstream&gt;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Next you will need to set up the streams and open the file:<br>ifstream IFile; // stream set up<br>IFile.open(&quot;Info.ini&quot;);&nbsp;&nbsp;&nbsp;&nbsp;// open for read<br><br>&nbsp;&nbsp;&nbsp;&nbsp;You will still need to read each line in the file but you can ignore the lines w/o @:<br>if (!IFile)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;// Something went wrong<br>&nbsp;&nbsp;&nbsp;&nbsp;// Process error here<br>}<br>else<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;// Everything OK, get data<br>&nbsp;&nbsp;&nbsp;&nbsp;IFile.unsetf(ios::skipws);// skip white space<br><br>&nbsp;&nbsp;&nbsp;&nbsp;string ImpLine; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// import line<br>&nbsp;&nbsp;&nbsp;&nbsp;while (IFile &gt;&gt; ImpLine)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Do Something<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (ImpLine.empty())<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Nothing to import<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string CheckDigit = ImpLine.substr(1,1);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (CheckDigit == &quot;@&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Process here<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;I haven't tested this code so there are likely to be some errors but you should get the general idea.<br><br> <p>James P. Cottingham<br><a href=mailto: > </a><br><a href= Veneer Co., Inc.</a><br>All opinions are mine alone and do not necessarily reflect those of my employer.
 
Thanks for the Quick reply!<br><br>I think this could work, havn't tried it yet, but it didn't solove my problem!<br>You see the hard part, for me, is to get information from a sertain point ie. @Input1=&quot;Dummie text&quot; that is, the information is 'Dummie text' in 'Input1' or @Owner=&quot;Me&quot; and the information is 'Me' in 'Owner'<br>Somehow I must find 'Input1' or 'Owner' and finding things is not my best side! :)<br>Somthing like a code that finds a line starting with '@' then reads everything between '@' and '=' when the accurat index is found it will read the info. between '&quot;' and '&quot;'<br><br>That is:<br><br>@Owner=&quot;ME&quot;<br><br>Find line, contains @<br>Read text Owner<br>Read info ME<br>and for ie. stores it into an edit field!<br><br>Thanks again!<br><br>The Philosopher [801119] <br> <p>Martin G Broman<br><a href=mailto:mgb_svea@hotmail.com>mgb_svea@hotmail.com</a><br><a href= > </a><br>DWS - Alpha Whitin Dead Wolf Society
 
&nbsp;&nbsp;&nbsp;&nbsp;I created a simple BCB project that took a Info.ini file and broke out the lines in question. First, I created the following Info.ini file:<br><font color=blue>###Dummie Text###<br>;Not interesting<br><br>@Owner=&quot;Me&quot;<br>@Input=&quot;Get this line&quot;</font><br><br>&nbsp;&nbsp;&nbsp;&nbsp;Next, I created a simple form with one memo box and a button. The following code I put in the buttonclick method. You could put it anywhere you need to. You can also eliminate some/all of the Memo1 output lines. I put these in to follow the flow of the program. One final note, I did not put a lot of error processing in, I left that up to you. Also, you may need to rearrainge where the variables are created to change the scope if you need to. For that matter, you may need to rearrainge everything. :)<br><font color=blue><br>Memo1-&gt;Lines-&gt;Add(&quot;Opening file for reading...&quot;);<br><br>ifstream IFile; // stream set up<br>IFile.open(&quot;Info.ini&quot;);&nbsp;&nbsp;&nbsp;&nbsp;// open for read<br><br>if (!IFile)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;// Something went wrong<br>&nbsp;&nbsp;&nbsp;&nbsp;Memo1-&gt;Lines-&gt;Add(&quot;Could not open Info.ini&quot;);<br>}<br>else<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;// Everything OK, get data<br>&nbsp;&nbsp;&nbsp;&nbsp;IFile.unsetf(ios::skipws);// don't skip white space, probably not necessary with getline<br><br>&nbsp;&nbsp;&nbsp;&nbsp;string ImpLine;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// import line<br><br>&nbsp;&nbsp;&nbsp;&nbsp;// NOTE: If using BCB 5 or C++ 5.5, getline has a bug. See Borland Article ID #21145<br>&nbsp;&nbsp;&nbsp;&nbsp;while (getline(IFile, ImpLine, '\n'))<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Do Something<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (ImpLine.empty())<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Nothing to import<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Memo1-&gt;Lines-&gt;Add(&quot;Nothing to import from Info.ini&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Let's bite the bullet and translate the C++ string to Borland's Pascal string<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// This way we can use the Pascal string easier for Memo1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AnsiString WorkString = ImpLine.c_str();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Memo1-&gt;Lines-&gt;Add(WorkString);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char CheckDigit = WorkString[1];&nbsp;&nbsp;// We only want lines with @<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (CheckDigit == '@')<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Process here<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Memo1-&gt;Lines-&gt;Add(&quot;Got a line&quot;);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Good boy (girl). Now lets parse the line<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int StrLen = WorkString.Length();&nbsp;&nbsp;// We'll need the string length<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Let's get the part of the line from the @ to the =<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// First, let's find the = position<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int EqlSignPos = WorkString.Pos('=');<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (EqlSignPos &gt; 0)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Found equal sign;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Get the first part of the string, skipping the @ and = signs<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AnsiString FirstPart = WorkString.SubString(2, EqlSignPos-2);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Memo1-&gt;Lines-&gt;Add(FirstPart);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Now lets break about the string again, skipping the &quot; sign<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AnsiString SecondPart = WorkString.SubString(EqlSignPos+1, StrLen);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Memo1-&gt;Lines-&gt;Add(SecondPart);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AnsiString Who;&nbsp;&nbsp;// the who belongs to the first part<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// We could assume that the = sign is immediantly behind the =<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Parse string some more<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AnsiString ThirdPart = SecondPart.SubString(FirstQuotePos+1, SecondPart.Length());<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Memo1-&gt;Lines-&gt;Add(ThirdPart);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Now, we again could assume that the last &quot; mark is at the end<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// but what if we would like to add a comment after it<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int SecondQuotePos = ThirdPart.Pos('&quot;');<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><br>Memo1-&gt;Lines-&gt;Add(&quot;Closing file...&quot;);<br>IFile.close();<br><br>Memo1-&gt;Lines-&gt;Add(&quot;Done!&quot;);<br></font> <p>James P. Cottingham<br><a href=mailto: > </a><br><a href= Veneer Co., Inc.</a><br>All opinions are mine alone and do not necessarily reflect those of my employer.
 
This piece of code is a bit simpler than the exampleabove. But then it's not too sophisticated either :)<br>assuming you have a Button a Edit box and a Memo1 on a form<br>paste this code to test it, The file contains some rows<br>as in your example above, like this:<br><br>@Input1=&quot;Get from this line&quot;<br>@input2=&quot;Get from this line to&quot;<br>Comment <br><br>any row thats not starting with a @ is ignored.<br>The Code:<br><br>void __fastcall TForm1::Button1Click(TObject *Sender)<br>{<br>&nbsp;&nbsp;FILE *f;<br>&nbsp;&nbsp;char row[81];<br>&nbsp;&nbsp;AnsiString line,name,value,first;<br>&nbsp;&nbsp;int pos;<br><br>&nbsp;&nbsp;f=fopen(&quot;File1.txt&quot;,&quot;r&quot;); //open file for reading<br>&nbsp;&nbsp;while(fgets(row,80,f)){&nbsp;&nbsp;&nbsp;//read lines untilend of file<br>&nbsp;&nbsp;&nbsp;&nbsp;line=(AnsiString)row;&nbsp;&nbsp;&nbsp;//store whole line<br>&nbsp;&nbsp;&nbsp;&nbsp;first=line[1];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//store first char. of line<br>&nbsp;&nbsp;&nbsp;&nbsp;pos=line.Pos(&quot;=&quot;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//get position of = sign<br>&nbsp;&nbsp;&nbsp;&nbsp;name=line.SubString(2,pos-2);&nbsp;&nbsp;&nbsp;&nbsp;//get the name part eg. &quot;Input1&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;if(first==&quot;@&quot; && name.UpperCase()==Edit1-&gt;Text.UpperCase()){&nbsp;&nbsp;//If name matches ...<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//UpperCase for case insensitivity<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value=line.SubString(pos+1,line.Length()-(pos+1));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//store value part<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Memo1-&gt;Lines-&gt;Add(&quot;Name=&quot;+name+&quot;, Value=&quot;+value); //show result in Memo1 field<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;}<br>} <p>Anders Sjöberg<br><a href=mailto:ansj@perceptive.se>ansj@perceptive.se</a><br><a href= webpages</a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top