Odd quick writing data to a file

R

Robert TV

I seem to be experiencing something odd when writing data to a file ... I
say because I have not had a problem like this before. I have 3 values to
write to the file ... I want each value on its own line. I use this code
(not working correctly)

#create the file
open (FILE,">>datafile.inc") or die "Can't open file: $!";
close(FILE);

#write data to file
open (FILE,">>datafile.inc") or die "Can't open file: $!";
print FILE "$value1\n";
print FILE "$value2\n";
print FILE "$value3\n";
close(FILE);

After this operation is complete, I can find the file on the server. When I
open it, all data is on a single line. For some reason, it's ignoring my
"\n"s. I also tried this:

#create the file
open (FILE,">>datafile.inc") or die "Can't open file: $!";
close(FILE);

#write value1 to file
open (FILE,">>datafile.inc") or die "Can't open file: $!";
print FILE "$value1\n";
close(FILE);

#write value2 to file
open (FILE,">>datafile.inc") or die "Can't open file: $!";
print FILE "$value2\n";
close(FILE);

#write value3 to file
open (FILE,">>datafile.inc") or die "Can't open file: $!";
print FILE "$value3\n";
close(FILE);

Same thing, all data is still on a single line. Funny thing is i've working
with simple flatfile datafiles before without this problem. Maybe my brain
isn't firing correctly right now. I will add this though ... if I replace my
"\n"s with "\r\n"s I get the data on multipul lines ... BUT ... "\r"s screw
up other parts of my code (not shown) Any ideas?

Robert
 
B

Bob Walton

Robert said:
I seem to be experiencing something odd when writing data to a file ... I
say because I have not had a problem like this before. I have 3 values to
write to the file ... I want each value on its own line. I use this code
(not working correctly)

#create the file
open (FILE,">>datafile.inc") or die "Can't open file: $!";
close(FILE);

#write data to file
open (FILE,">>datafile.inc") or die "Can't open file: $!";
print FILE "$value1\n";
print FILE "$value2\n";
print FILE "$value3\n";
close(FILE);

After this operation is complete, I can find the file on the server. When I
open it, all data is on a single line. For some reason, it's ignoring my
"\n"s. I also tried this:


I'll bet your server is running some flavor of *nix, and you're running
Windoze -- is that right? When you transfer your file back to your PC,
be sure and use the ASCII mode of FTP (assuming, of course, that you are
using FTP to do your file transfers). Or use a good Windoze editor that
is capabile of handling files with *nix line endings (like VIM, for
example).

Or are you perhaps looking at your file in a web browser as an HTML
file? Remember that all whitespace is the same in HTML -- a newline is
treated as if it were a space character.

....
 
R

Robert TV

I have found my problem. it has to do with the file extension oddly enough.
To view the newly created file, I log into the server with my ftp program. I
then double click it to execute the programs "view file" command. It opens
in Windows Notepad. When viewing the files as an .inc extension, all data is
on the same line. If I rename to .txt and reopen, the data is on separate
lines ... so it appears that Notepad was the culprit.

Robert
 
B

Ben Morrow

[don't top-post]

Quoth "Robert TV said:
I have found my problem. it has to do with the file extension oddly enough.
To view the newly created file, I log into the server with my ftp program. I
then double click it to execute the programs "view file" command. It opens
in Windows Notepad. When viewing the files as an .inc extension, all data is
on the same line. If I rename to .txt and reopen, the data is on separate
lines ... so it appears that Notepad was the culprit.

No, your FTP client. It will be configured to transfer files with
certain extensions as 'text' and others as 'binary'. Text transfers
convert unix newlines (ascii LF) to windows (ascii CR LF), binary don't.
You can probably change the list of extensions to include any others
which you know will be text files, such as your .inc.

Ben
 
M

Matt Garrish

Robert TV said:
I seem to be experiencing something odd when writing data to a file ... I
say because I have not had a problem like this before. I have 3 values to
write to the file ... I want each value on its own line. I use this code
(not working correctly)

#create the file
open (FILE,">>datafile.inc") or die "Can't open file: $!";
close(FILE);

I see your problem was a file transfer issue, but I have to ask why you are
opening and closing the file before writing to it? If the file doesn't
exist, it will be created the first time you append to it (i.e., in your
next open). If it does exist, opening and closing it in append mode without
doing anything to it is just a waste of time.

Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top