readLines                package:base                R Documentation

_R_e_a_d _T_e_x_t _L_i_n_e_s _f_r_o_m _a _C_o_n_n_e_c_t_i_o_n

_D_e_s_c_r_i_p_t_i_o_n:

     Read some or all text lines from a connection.

_U_s_a_g_e:

     readLines(con = stdin(), n = -1, ok = TRUE, warn = TRUE,
               encoding = "unknown")

_A_r_g_u_m_e_n_t_s:

     con: a connection object or a character string.

       n: integer.  The (maximal) number of lines to read.  Negative
          values indicate that one should read up to the end of the
          connection.

      ok: logical.  Is it OK to reach the end of the connection before
          'n > 0' lines are read?  If not, an error will be generated.

    warn: logical.  Warn if a text file is missing a final EOL.

encoding: encoding to be assumed for input strings.  It is used to mark
          character strings as known to be in Latin-1 or UTF-8: it is
          not used to re-encode the input.  To do the latter, specify
          the encoding as part of the connection 'con' or via
          'options(encoding=)': see the example under 'file'. 

_D_e_t_a_i_l_s:

     If the 'con' is a character string, the function calls 'file' to
     obtain a file connection which is opened for the duration of the
     function call.

     If the connection is open it is read from its current position. 
     If it is not open, it is opened in '"rt"' mode for the duration of
     the call and then closed again.

     If the final line is incomplete (no final EOL marker) the
     behaviour depends on whether the connection is blocking or not. 
     For a non-blocking text-mode connection the incomplete line is
     pushed back, silently.  For all other connections the line will be
     accepted, with a warning. 

     Whatever mode the connection is opened in, any of LF, CRLF or CR
     will be accepted as the EOL marker for a line.

_V_a_l_u_e:

     A character vector of length the number of lines read.

     The elements of the result have a declared encoding if 'encoding'
     is '"latin1"' or '"UTF-8"',

_N_o_t_e:

     The default connection, 'stdin', may be different from 'con =
     "stdin"': see 'file'.

_S_e_e _A_l_s_o:

     'connections', 'writeLines', 'readBin', 'scan'

_E_x_a_m_p_l_e_s:

     cat("TITLE extra line", "2 3 5 7", "", "11 13 17", file="ex.data",
         sep="\n")
     readLines("ex.data", n=-1)
     unlink("ex.data") # tidy up

     ## difference in blocking
     cat("123\nabc", file = "test1")
     readLines("test1") # line with a warning

     con <- file("test1", "r", blocking = FALSE)
     readLines(con) # empty
     cat(" def\n", file = "test1", append = TRUE)
     readLines(con) # gets both
     close(con)

     unlink("test1") # tidy up

