View Full Version : write to text file
cgibie
03-01-2006, 06:48 PM
I have problem with my text files, it worked really fine until last nite I just found out that the format is not what I want.
I want the output to be like this:
Date: Wed Mar 1 12:40:35 2006
Entry: 1
Category: Blog
Month: March
Day: 1
Year: 2006
Title: Test
Content: Test the blog
File1:
URL:
Date: Wed Mar 1 12:42:43 2006
Entry: 1
Category: Blog
Month: March
Day: 1
Year: 2006
Title: Test
Content: Test 2 blog
but it gives me like this:
Date: Wed Mar 1 12:40:35 2006Entry: 1Category: BlogMonth: MarchDay: Year: 2006Title: TestContent: Test the blogFile1: URL:
and so on....
# write the entry to the file.txt without image
open(OUT, ">>$newfile") or &dienice ("Could not open the $outfile: $!");
# lock the file exclusivly
flock(OUT, LOCK_EX);
#
seek(OUT, 0, SEEK_SET);
my %form;
# print local time
print OUT "\n";
print OUT "Date: $time";
print OUT "\n";
# loop through the field
foreach my $field(param()) {
$form{$field} = param($field);
$form{$field} =~ s/\n/ /g; # replace newlines with spaces
$form{$field} =~ s/\r//g; # remove hard returns
print OUT "$field: $form{$field}\n";
}
print OUT "\n";
close(OUT);
Could anyone helps me where did I do wrong? I thought I put all the "\n"
FishMonger
03-01-2006, 07:31 PM
Is the output you're refering to in the text file or what's being dispayed in the browser? Your file should have each item on seperate lines, but the browser uses <br> to seperate the lines not \n.
If you show us how you are outputing the data so we can see what's wrong.
cgibie
03-01-2006, 07:36 PM
Fishmonger, I meant the text file not in the browser... after my that cgi file executed, the fields on my forms wrote to text files in a wrapped format, which I don't want. I don't want it to be wrapped but instead seperate each field by "\n" makes sense?
FishMonger
03-01-2006, 07:46 PM
Based on your code the fields are on sperate lines, but you may be seeing the difference between how Windows and Unix "see" the termination of the lines.
What system is the cgi file executing on? Are you viewing the text file on that same system or did you ftp it to another system? What program are you using to view the file?
cgibie
03-01-2006, 07:51 PM
I host my script on netfirms and I am running ONLY MS windows. I used notepad to read that text file. Should I use other program to view it or what?
cgibie
03-01-2006, 08:05 PM
Fishmonger, I just noticed this there are weird [][][] sign after each line in that text file, I cannot copy here because when I copied the text here, the text will display one field for each line. GRrrrrrrrrrrrrrrrrr..... what's wrong? It's just like when I used word pad to read that text file instead, it will be one field per each line. But then when I start to modify that text file and upload it my server, the problem occurs....
FishMonger
03-01-2006, 08:05 PM
How did you transfer the file from netfirms to you Windows system? I'm assuming you used an ftp client. Did you transfer it in ascii or binary mode? Try viewing the file in wordpad or MS Word.
cgibie
03-01-2006, 08:17 PM
Yes I used FTP "auto detect" then I also used http download. Both gives me the same result. I tried to use wordpad and yes the contents in that text file read just like what I wanted but the thing is whenever I modify that text file in wordpad and upload it back to the server, my read script reads it in the way I don't want too. In other words, the text file screws up.
cgibie
03-01-2006, 08:21 PM
Yes I used FTP "auto detect" then I also used http download. Both gives me the same result. I tried to use wordpad and yes the contents in that text file read just like what I wanted but the thing is whenever I modify that text file in wordpad and upload it back to the server, my read script reads it in the way I don't want too. In other words, the text file screws up.
# $/ holds each record after \n in the text file
# double quote not single quote
local $/ = "\n\n";
@lines = reverse<IN>;
# printed within the while loop.
foreach (@lines) {
while (/entry: (.*)\n/ig ) {
$myword1 = "$1";
# if Entry is NOT equal to "DELETE" print the entry out
if ($myword1 ne "DELETED") {
print "<table id=entry><td valign=middle width=500 bgcolor=0066CC>";
print "<b>Entry#: " . $myword1 . " </b>";
print "</td></table>";
print "<br>";
}
}
FishMonger
03-01-2006, 08:25 PM
Your ftp client set the mode to binary which means the line endings didn't get translated. Instead of "auto detect", set it to ascii mode. In ascii mode, the line endings will be translated from unix to windows durning the transfer.
cgibie
03-02-2006, 02:45 AM
Problem is solved due to the ASCII, thanks Fishmonger
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.