Jump to content

Recommended Posts

Posted

Hi,

 

i have a txt file that contains data for my drawing, each line of the text file contains a different number of characters ..... i need to amend the text file (using spaces at the end of the data) so that every line is 256 characters long so that when i import my data everything lines up correctly. what would be the best/easiest way of doing this?

 

for example one line may contain 200 characters, i would need to add 56 spaces onto the end of the line.

 

Thanks

 

Dan

Posted

Guaranteed no lines more that 256 on the text file?

Posted (edited)

Dan,

 

if I understood correctly, perhaps something like this will do the trick...

(defun c:demo (/ a b c f l n)
 (if (and (setq f (getfiled "Select file" "" "txt" 0))
          (setq n (getint "\n Enter new line length: "))
     )
   (progn
     (setq a (open f "r"))
     (while (setq b (read-line a))
       (if (< (setq l (strlen b)) n)
         (progn
           (repeat (- n l)
             (setq b (strcat b " "))
           )
           (setq c (append c (list b)))
         )
         (setq c (append c (list b)))
       )
     )
     (close a)
     (setq a (open f "w"))
     (foreach b c
       (write-line b a)
     )
     (close a)
   )
 )
 (princ)
)

Hope that helps

Henrique

Edited by hmsilva
typo
Posted
Dan,

(defun c:demo (/ a b c f l n)......
[/quote]

Oh man, you guys got too much on your hands  I was thinking of create the space pad THEN one [i]strcat[/i]

EDIT: like this
[code]
(defun pad (str)
      (Strcat
 str
 (substr
   "                                                                                                                                                                                                                                                               "
   (strlen str)
 )
      )
 )

 

EDIT2:

 

Granting there are lines more than the desired number of string

 

We did not get a response from the OP if the lines are more than 256, should the string beyond 256 be a new line? or leave it as it is? (like what Henrique posted).

Posted
Oh man, you guys got too much on your hands :lol: I was thinking of create the space pad THEN one strcat

 

Hi pBe!

Nice to see you back! :)

 

Granting there are lines more than the desired number of string

We did not get a response from the OP if the lines are more than 256, should the string beyond 256 be a new line? or leave it as it is? (like what Henrique posted).

 

Good point!

Let's wait and see the OP reply...

 

Cheers

Henrique

Posted
Hi pBe!

Nice to see you back! :)

 

Thank you Henrique, Nice to be seen, I'm always around [lurking and observing], I just don't have the downtime to play as i use to :)

Posted

Thank shmsilva, that looks perfect ... however i am getting an "; error: bad argument type: stringp nil" on the second "(setq c (append c (list b)))"

 

what have i done wrong??

 

thank you!

 

PS it will never go above 256 characters.

Posted
Thank shmsilva, that looks perfect ... however i am getting an "; error: bad argument type: stringp nil" on the second "(setq c (append c (list b)))"

what have i done wrong??

thank you!

PS it will never go above 256 characters.

 

You're welcome, Dan!

 

You you should not get any error on the second 'append', it's only to append the list, if the string have more characters then the value setted in variable 'n'...

I can not reproduce that error.

 

Henrique

Posted
When the string manipulation is concerned, this thread may be of interest:

 

Nice stuff!

Thank you Lee.

 

 

Henrique

Posted
Nice stuff!

Thank you Lee.

 

Thanks Henrique! :)

 

Here's another way to write this program (using a similar method to that proposed by pBe above):

(defun c:padfile ( / ds1 ds2 fn1 fn2 spc str )
   (if (and (setq fn1 (getfiled "Select file" "" "txt" 0))
            (setq ds1 (open fn1 "r"))
            (setq fn2 (vl-filename-mktemp (vl-filename-base fn1) (vl-filename-directory fn1) ".txt"))
            (setq ds2 (open fn2 "w"))
       )
       (progn
           (setq spc " ")
           (repeat 8 (setq spc (strcat spc spc)))
           (while (setq str (read-line ds1))
               (write-line (substr (strcat str spc) 1 256) ds2)
           )
       )
   )
   (foreach x (list ds1 ds2)
       (if (= 'file (type x)) (close x))
   )
   (princ)
)

Posted
Thank shmsilva, that looks perfect ... however i am getting an "; error: bad argument type: stringp nil" on the second "(setq c (append c (list b)))"

 

what have i done wrong??

 

You you should not get any error on the second 'append', it's only to append the list, if the string have more characters then the value setted in variable 'n'...

I can not reproduce that error.

 

I believe the error stems from this expression:

(setq a (open myfile "w"))

Variable 'myfile' is not defined.

Posted
I believe the error stems from this expression:

(setq a (open myfile "w"))

Variable 'myfile' is not defined.

 

 

You're correct Lee, my bad...

I could not reproduce the error, because I was testing with my original code, with the original variables... :oops:

Code in post #3, corrected.

Thank you Lee!

 

Henrique

Posted

...
            (setq fn2 (vl-filename-mktemp (vl-filename-base fn1) (vl-filename-directory fn1) ".txt"))
            (setq ds2 (open fn2 "w"))...

 

Very nice :thumbsup:

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...