Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Perl (http://www.programmingforums.org/forum21.html)
-   -   File I/O problem (http://www.programmingforums.org/showthread.php?t=10826)

kruptof Jul 24th, 2006 4:36 PM

File I/O problem
 
the following code is supposed to write to a file 60 characters per loop and then insert a new line character at the end of that line:
Quote:

#!/Perl/bin/perl
use CGI;
$results=new CGI;

$comment=$results->param("comments");

open(DAT,">>Comments.txt");

$i=0;
$szlen=length $comment;

while($i<=$szlen)
{
print DAT substr($comment,$i);
print DAT "\n";
$i=$i+60;
}

close(DAT);
but some how this code is not printing 60 characters per line but instead more than 60 characters...........can any body please tell me what i am doing wrong and also tell me how i can correct this.............i thank you in advance for your replies.

kruptof Jul 24th, 2006 5:53 PM

okay sorry to waste your time people.......it was a silly mistake and after staring at the computer screen numlessly the penny dropped.........i was using the wrong string function to start with and some of my algorithms were out the window..........here is the new code after i have solved the problem:

Quote:

#!/Perl/bin/perl
use CGI;
$results=new CGI;

$comment=$results->param("comments");

open(DAT,">>Comments.txt");

$i=60;
$j=0;
$szlen=length $comment;

while($i<$szlen+1)
{
print DAT substr($comment,$j,$i)."\n";
$j=$i;
$i+=60;
}

DaWei Jul 24th, 2006 6:37 PM

Off topic, I'd suggest you learn to indent if your code is going to be presented to those other than yourself.


All times are GMT -5. The time now is 12:40 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC