Odd quick writing data to a file

Discussion in 'Perl Misc' started by Robert TV, Jun 27, 2004.

  1. Robert TV

    Robert TV Guest

    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
     
    Robert TV, Jun 27, 2004
    #1
    1. Advertising

  2. Robert TV

    Bob Walton Guest

    Robert TV wrote:

    > 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.

    ....

    > Robert


    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
     
    Bob Walton, Jun 27, 2004
    #2
    1. Advertising

  3. Robert TV

    Robert TV Guest

    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

    "Bob Walton" <> wrote in message
    news:...
    > Robert TV wrote:
    >
    > > 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.
    >
    > ...
    >
    > > Robert

    >
    > --
    > Bob Walton
    > Email: http://bwalton.com/cgi-bin/emailbob.pl
    >
     
    Robert TV, Jun 27, 2004
    #3
  4. Robert TV

    Ben Morrow Guest

    [don't top-post]

    Quoth "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.


    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

    --
    All persons, living or dead, are entirely coincidental.
    Kurt Vonnegut
     
    Ben Morrow, Jun 27, 2004
    #4
  5. Robert TV

    Matt Garrish Guest

    "Robert TV" <> wrote in message
    news:ifqDc.865811$Pk3.398962@pd7tw1no...
    > 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
     
    Matt Garrish, Jun 28, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. HNguyen
    Replies:
    4
    Views:
    2,430
    HNguyen
    Dec 21, 2004
  2. John Curley
    Replies:
    3
    Views:
    352
    David Dorward
    Oct 4, 2003
  3. JKop
    Replies:
    11
    Views:
    915
  4. Michael Speer

    Odd behavior with odd code

    Michael Speer, Feb 16, 2007, in forum: C Programming
    Replies:
    33
    Views:
    1,140
    Richard Heathfield
    Feb 18, 2007
  5. Mike Cain
    Replies:
    3
    Views:
    394
    Jack Klein
    Oct 10, 2006
Loading...

Share This Page