Jump to content

write a list into text file and return back as list


muthu123

Recommended Posts

Dear all,

 

I am developing one project for my cencern, in this project i have so many details in a list format. I want to write all the lists in a text file as a organized manner and get it back as a list whenever required.

 

Please try forme.

 

my mail.

mkumar@mabanisteel.com

Link to comment
Share on other sites

Use PRINT statement with the list as argument to record it in the data file.

 

After call READ-LINE to retrieve it from file, respectively READ to evaluate the string into a list.

Link to comment
Share on other sites

Dear msasu,

Thank you so much. I wasted full day for this task.

I didn't use both the function till now which make me mad.

Anyhow thank you so much for your immediate response.

 

Can you explain in detail about that functions.

Link to comment
Share on other sites

You are welcomed!

 

 

The below examples may help you:

 

To record a list into a text file:

(setq TargetFile (open "c:\\test_20091210.txt" "w"))   ;open file to write
(princ '(1 2 3 4) TargetFile)                          ;record the list into file
(close TargetFile)                                     ;close the file

 

To retrieve that list from the file:

(setq SourceFile (open "c:\\test_20091210.txt" "r"))   ;open file to read
(setq FileLine (read-line SourceFile))                 ;read file content (one line in this case)
(close SourceFile)                                     ;close the file
(setq theListFromFile (read FileLine))                 ;evaluate the string into a list

 

For more and better explanations may use AutoLISP’s help.

 

Regards,

Link to comment
Share on other sites

can you give me your mail ID to contact you if i face any problem in my task.

my mail Id

 

You will get better support if continue to post on forum – there are many great programmers that can help you and a lot of good approaches for each issue to choose from.

 

Also, is a good practice to avoid posting in clear your e-mail address since this sound like an invitation to spammers. For the case when you cannot avoid doing so, just spell it like:

user at domain dot extension

at list this will avoid automatically parsing.

Link to comment
Share on other sites

Dear friends,

can you help me to modify this following lisp

 

(defun save_input_as_a_text_file_from_list ()

(setq input_text_file (strcat path_input_endwall "/endwall.txt"))

(setq file_descr (open input_text_file "w"))

(close file_descr)

 

(setq list_type_variables (list "main_input_building_details" "clear_span_dialog_details" "left_end_column_size" ))

 

(setq file_descr (open input_text_file "a"))

(setq i 0)

(repeat (length list_type_variables)

(write-line (nth i list_type_variables) file_descr)

(princ (eval (read (nth i list_type_variables))) file_descr)

(princ "\n" file_descr)

(setq i (1+ i))

)

(close file_descr)

)

 

I need a text file like this

firstline will be varible name

nextline will be the whole list

 

main_input_building_details

(("frame_type" "typical_endwall" "endwall_type_left" "endwall_girt_type_left" "corner_column_left_endwall" "endwall_column_spacings_left" "bay_spacings_sidewall" "building_length" "building_width" "eave_height" "expansion_joint" "gen_block_wall_ht") ("clear_span" "typical_endwall_yes" "bearing_frame_left" "by_pass_left" "306" "5@6000" "10@6000" "60000" "30000" "6000" "0" "2500"))

clear_span_dialog_details

(("building_width" "ridge_distance" "frame_profile" "eave_height_left" "ridge_slope_left" "girt_spacings_left" "purlin_spacings_left" "eave_height_right" "ridge_slope_right" "girt_spacings_right" "purlin_spacings_right") ("30000" "15000" "symmetrical" "6000" "1" "10@1000" "10@1500" "6000" "1" "10@1000" "10@1500"))

left_end_column_size

("Ipea-200" "Ipea-200" "Ipea-200" "Ipea-200" "Ipea-200" "Ipea-200")

 

 

But my lisp is removing the double quote inside the list and writing like this as a text file.

 

main_input_building_details

((frame_type typical_endwall endwall_type_left endwall_girt_type_left corner_column_left_endwall endwall_column_spacings_left bay_spacings_sidewall building_length building_width eave_height expansion_joint gen_block_wall_ht) (clear_span typical_endwall_yes bearing_frame_left by_pass_left 306 5@6000 10@6000 60000 30000 6000 0 3000))

clear_span_dialog_details

((building_width ridge_distance frame_profile eave_height_left ridge_slope_left girt_spacings_left purlin_spacings_left eave_height_right ridge_slope_right girt_spacings_right purlin_spacings_right) (30000 15000 symmetrical 6000 1 10@1000 10@1500 6000 1 10@1000 10@1500))

left_end_column_size

(Ipea-200 UB-203 UB-203 UB-203 Ipea-200 Ipea-200)

Link to comment
Share on other sites

No msasu.

 

I need the value of that variable to be written. I hope we have to use eval function. Am i correct? otherwise it will print the variable name only.

Link to comment
Share on other sites

Not sure why you are parsing the lists instead of recording it directly – the below code is producing for me the result that you expect:

 

 
(setq Var1st "1"
     Var2nd "2"
     Var3rd "3")

(setq theFile (open "C:\\string_list_test.txt" "w"))
(prin1 (list Var1st Var2nd Var3rd) theFile)
(close theFile)

Link to comment
Share on other sites

Use Print instead of Write-Line.

 

 

print

(print [expr [file-desc]])

This function is the same as prin1, except it prints a newline character before

expr, and prints a space following expr.

Arguments

expr A string or AutoLISP expression. Only the specified expr is

printed; no newline or space is included.

Prints an expression to the command line, or writes an expression to an open file

Prints an expression to the command line, or writes an expression to an open file

144 | AutoLISP Reference

file-desc A file descriptor for a file opened for writing.

Return Values

The value of the

Link to comment
Share on other sites

Examples for you to look over:

 

;; Writing the File

(defun c:list_write (/ f lst1 lst2)
 (vl-load-com)

 (if (setq f (getfiled "Output" "" "txt" 1))
   (progn

     (setq lst1 '("A" "B" "C" "D" "E" "F")
           lst2 '("1" "2" "3" "4" "5" "6"))

     (setq f (open f "w"))

     (foreach x '(lst1 lst2)

       (write-line (vl-princ-to-string x) f)
       (write-line (vl-prin1-to-string (eval x)) f))

     (close f)))

 (princ))

;; Reading the File back

(defun c:list_read (/ f var lst)

 (if (setq f (getfiled "Input" "" "txt" 16))
   (progn

     (setq f (open f "r"))

     (while (and (setq var (read-line f))
                 (setq lst (read-line f)))
       (set (read var) (read lst)))

     (close f)))

 (princ))

 

Of course, you could use "print", or "prin1" over "vl-princ-to-string" and "vl-prin1-to-string", but thats just me :)

Link to comment
Share on other sites

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