(defun BinFile-Write (FileName lst / fso stream file result)
(vl-catch-all-apply
'(lambda (/ size)
(setq fso (vlax-create-object "Scripting.FileSystemObject")
stream (vlax-invoke fso 'OpenTextFile FileName 2 :vlax-true 0))
(vlax-invoke stream 'Write (vl-list->string lst))
(vlax-invoke stream 'Close)
(setq file (vlax-invoke fso 'GetFile FileName)
result (vlax-get file 'Size))))
(if stream
(vlax-release-object stream))
(if file
(vlax-release-object file))
(if fso
(vlax-release-object fso))
result)
Why not use the same principle as MP's over here: http://www.theswamp.org/index.php?topic=17465.0 (http://www.theswamp.org/index.php?topic=17465.0)
So I am using MP's (_WriteStream). But somehow the conversion to text that his write function requires causes trouble. In my test only 290 bytes instead of 440 - 60 = 380 bytes were written.
To write a null byte in lisp use char code 256
(subst 256 0 lst)
I again end up with a file of only 4 bytes, meaning the writing process stopped before the first null byte.(vl-list->string '(0 1 2 3)) => "\000\001\002\003"
(vl-list->string '(256 1 2 3)) => "\000\001\002\003"
(defun c:writefile()
(setq fn (open "C:\\Users\\Keith\\Desktop\\testing.dat" "w")
i 1
x 10)
(repeat 100 (write-char 256 fn))
(while (< 0 x)
(write-char i fn)
(setq i (1+ i))
(if (= i 257)
(setq x (1- x)
i 1)
)
)
(close fn)
)
(defun c:Test-write-char ( / file)
(setq file (open (strcat (getvar 'dwgprefix) "test.bin") "w"))
(repeat 8 (write-char 1 file))
(repeat 8 (write-char 256 file)) ; Is not written to the file.
(repeat 8 (write-char 2 file))
(repeat 8 (write-char 0 file)) ; Is not written to the file.
(repeat 8 (write-char 3 file))
(close file)
)
_$ (setq lst nil n 256)
256
_$ (repeat n (setq lst (cons (setq n (1- n)) lst)))
(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255)
_$ (length lst)
256
_$ (BinFile-Write "c:/temp/test1.bin" lst)
256
_$ (length (BinFile-Read "c:/temp/test1.bin"))
256
_$ (BinFile-Read "c:/temp/test1.bin")
(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255)
Note if you use MP's writestream directly it's going to write the file in the default text format. On my system that's bigendian Unicode - i.e. the above file would have been 512 bytes. That's because he's using CreateTextFile which simply uses the default format. I've changed that to OpenTextFile (http://www.devguru.com/technologies/vbscript/quickref/filesystemobject_opentextfile.html) with the option 0 to force using ASCII instead of whatever the default is._$ lst
(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255)
_$ (BinFile-WriteChars "c:/temp/test2.bin" lst)
nil
_$ (BinFile-Read "c:/temp/test2.bin")
nil
Perhaps it's something to do with my Win7-64-pro + ACad Vanilla 2012.
Keith's code doesn't work for me:...Sorry I'm an idiot :ugly: ... why did I think I should check if the file exists before I write to it? :lmao:
_$ (BinFile-WriteChars "c:/temp/test2.bin" lst)
nil
_$ (BinFile-Read "c:/temp/test2.bin")
(0 1 2 3 4 5 6 7 8 9 13 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255)
_$ (length (BinFile-Read "c:/temp/test2.bin"))
257
It's now 257 long since it writes a "\r\n" for each newline character ("\r" = 13, "\n" = 10).
Are you sure you've copied my code correctly?Yes, I am sure. :angel:
OT:Strange, when I do so the numbers are omitted. Perhaps it's a browser thing. I'm using FireFox 12.0 and then pasting directly into Command Line / VLIDE / NotePad / NotePad+ / SciTE - nowhere do I get those line numbers pasted, even if I selected some text before and/or after the code block.
What is the correct procedure for copying Auto/Visual Lisp formatted code from the forum? If I do a simple copy/paste I get the line numbers in the result.
The fact that your code fails for me may well be a Bricscad thing.Then I'm guessing its BC's implementation of the vl-string->list and vl-list->string functions. AC's seem to take 0 values perfectly, and it also doesn't screw with the 10 value ("\n") like the write-char / read-char does.
It is not possible to write a null character to file with lisp
Using 256 to write a binary null is not supported in autolisp ... in the help file, it states in partThat's what I tried to overcome in my previous code. Try the following in BC:QuoteIt is not possible to write a null character to file with lisp
Obviously using 256 does work in AutoCAD, so as has been suggested previously, I suspect it is a Briscad implementation issue.
(strcat "\400" "abc")
In ACad it results in a string"\000abc"
If you get the same, then sending that string to an outside writer (like the TextStream object) might just do the trick.
Thanks for that. Upgrading from FF 3.6.10 to FF 12.0 has solved this. Lines that have been indented do loose their first space character, but that is not a problem of course.OT:Strange, when I do so the numbers are omitted. Perhaps it's a browser thing. I'm using FireFox 12.0 and then pasting directly into Command Line / VLIDE / NotePad / NotePad+ / SciTE - nowhere do I get those line numbers pasted, even if I selected some text before and/or after the code block.
What is the correct procedure for copying Auto/Visual Lisp formatted code from the forum? If I do a simple copy/paste I get the line numbers in the result.
SR30365, SR30397 - LISP: (vl-list->string) function did not convert integer values according to system codepage.Both functions behave correctly in the current version of Bricscad. My guess at this point is that the problem occurs when the string is passed to the COM object.
...
SR25892 - LISP: (vl-string->list) did not convert the characters to system ANSI codepage, so the results were different from (ascii).
Here is another method using adodbThank you for that link.
http://www.theswamp.org/index.php?topic=36656.0
Try the following in BC:Code: [Select](strcat "\400" "abc")
In ACad it results in a stringCode: [Select]"\000abc"
If you get the same, then sending that string to an outside writer (like the TextStream object) might just do the trick.
(strcat "\400" "abc") => "Āabc"
This is perhaps a codepage issue?(strcat "\000" "abc") => "\000abc"