?readBin will do what you want. If the file is really a set of lines, you could use readLines() followed by strsplit(). See also the R Data Import/Export manual. On Mon, 1 Sep 2003, Jiming Yu wrote:> Dear all, > I am trying to read characters byte by byte(in their ASCII codes) from a > file(already transferred from text file to a file of ASCII codes, by C > language). I am using scan() function. But it seems that this is impossible. > If I read data as 'character' type, data are read word by word, > separated by spaces(though in default setting, sep=""). e.g. if a file > contains "This is a book.", Then it reads "This", "is", "a", "book.", not > "T", "h", "i", "s", etc(as I wanted it to). > If I read data as 'integer' type from the file which contains all ASCII > codes of the original file, e.g. if a file contains "This is a book.", Then > it reads "T", " ", "i", " ", "a", " " , "b", which are initials of all words > and all spaces, not "T", "h", "i", "s", etc(as I wanted it to). > If anybody has a solution, please let me know. I'd really appreciate > your help. Thank you very much!-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
On Mon, 1 Sep 2003 14:26:43 -0700, "Jiming Yu" <jimingyu at princeton.edu> wrote:>Dear all, > I am trying to read characters byte by byte(in their ASCII codes) from a >fileI was going to suggest using readBin, but there seems to be a bug:> con <- file('c:/test.txt','rb') > readBin(con,'c',15,1)stack imbalance in internal readBin, 9 then 8stack imbalance in .Internal, 8 then 7 stack imbalance in {, 6 then 5 NULL Error: unprotect(): stack imbalance This was in today's r-devel build for Windows, but the bug also shows up in 1.7.1. I'll look into it. Duncan Murdoch
Dear all, I am trying to read characters byte by byte(in their ASCII codes) from a file(already transferred from text file to a file of ASCII codes, by C language). I am using scan() function. But it seems that this is impossible. If I read data as 'character' type, data are read word by word, separated by spaces(though in default setting, sep=""). e.g. if a file contains "This is a book.", Then it reads "This", "is", "a", "book.", not "T", "h", "i", "s", etc(as I wanted it to). If I read data as 'integer' type from the file which contains all ASCII codes of the original file, e.g. if a file contains "This is a book.", Then it reads "T", " ", "i", " ", "a", " " , "b", which are initials of all words and all spaces, not "T", "h", "i", "s", etc(as I wanted it to). If anybody has a solution, please let me know. I'd really appreciate your help. Thank you very much! Jiming Jiming Yu Information Sciences and Systems Group Department of Electrical Engineering Princeton University Email: jimingyu at princeton.edu Office: 609-258-4634 Cell: 609-933-6850