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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading one and one char in a file

Status
Not open for further replies.

Oddgeir

Programmer
Jul 10, 2000
6
NO
Hi<br><br>I wonder if anybody know how to read a file character by character? And then check the character each time. <br><br>And if anybody knows how to delete everything in a file after or before a special charcter (like . or /)<br><br>I guess these are stupid questions but I'm new to this. So please excuse me.<br><br>Sincerly <br>Oddgeir
 
one approach......<br><br><br><FONT FACE=monospace><br>#!perl<br>$inFile = 'temp.txt';<br>open(IPF,&quot;&lt;$inFile&quot;) ¦¦ die &quot;Failed to open IPF, $!\n&quot;;<br>while (&lt;IPF&gt;) { $buffer .= $_; } <font color=red># catch the entire file in $buffer </font><br>close IPF;<br><br><font color=red># a period or dot is a regex wild card<br># the 'g' on the end says match globally, not just once<br># the 's' on the end says treat new line chars like any other</font><br>while ($buffer =~ /./gs) <br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;$beforeMatch = $`; <font color=red> # catch everything before the current char.</font><br>&nbsp;&nbsp;&nbsp;&nbsp;$afterMatch&nbsp;&nbsp;= $'; <font color=red>&nbsp;&nbsp;&nbsp;# catch everthing after the current char.</font><br>&nbsp;&nbsp;&nbsp;&nbsp;$thisChar = $&; <font color=red>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# catch the current character.<br>&nbsp;&nbsp;&nbsp;&nbsp;# put whatever condition-behavior you want here.</font><br>&nbsp;&nbsp;&nbsp;&nbsp;last if ($thisChar eq 5); <br>&nbsp;&nbsp;&nbsp;&nbsp;print &quot;$thisChar\n&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br></font><br><br>'hope this helps.... <p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo
 
Thanks. It worked fine with a little bit of modifying. <br><br>Oddgeir
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top