1. Contrary read_file()

Two similar read processes. The first works; the second doesn't.

sequence file_path 
sequence insert_path 
 
file_path = "/home/allen/UCT.dat" 
if file_exists(file_path) then 
   fn = open(file_path,"u",0) 
end if 
UCTree = read_file(fn,BINARY_MODE) 

successfully reads a 6.3 MB file into memory. fn is 3.
But

insert_nmbr = prompt_number("Which subtree?:", {1, number_of_files}) 
insert_path = splice("/home/allen/Extracted/", extracted_files[insert_nmbr][D_NAME], 23) 
if file_exists(insert_path) then 
   insert_fn = open(insert_path, "wb") 
else 
   abort(insert_fn) 
end if 
if insert_fn < 1 then 
   puts(1, "could not open insert file") 
   crash_file("Could open insert file") 
   abort(0) 
end if 
insert_tree = read_file(insert_fn, BINARY_MODE)  

gives no hint of an error. The resulting file path is /home/allen/Extracted/(name of selected file). insert_fn is 4. But insert_tree is an empty sequence, {}. The actual file is a megabyte long.

new topic     » topic index » view message » categorize

2. Re: Contrary read_file()

Sometimes writing English pseudocode helps

alrobnett said...
insert_nmbr = prompt_number("Which subtree?:", {1, number_of_files}) 
insert_path = splice("/home/allen/Extracted/", extracted_files[insert_nmbr][D_NAME], 23) 
if file_exists(insert_path) then                        --if the file exists then 
   insert_fn = open(insert_path, "wb")                  --open it for writing in binary (ie delete it and write a new one) 
else                                                    --otherwise if the file doesn't exist 
   abort(insert_fn)                                     --abort the program, and display the file handle 
                                                        --ah, if I didn't open the file for writing,  
                                                        --then where has that handle come from 
end if 
if insert_fn < 1 then                                   --if the file doesn't exist then 
                                                        --(but note I would never have got here if the file didn't exist) 
   puts(1, "could not open insert file")                --print your error reporting and stuff 
   crash_file("Could open insert file")                 --looks like this block would never be run 
   abort(0) 
end if 
insert_tree = read_file(insert_fn, BINARY_MODE)         --looks like it's empty because you deleted it 

gives no hint of an error. The resulting file path is /home/allen/Extracted/(name of selected file). insert_fn is 4. But insert_tree is an empty sequence, {}. The actual file is a megabyte long.

Cheers

Chris

new topic     » goto parent     » topic index » view message » categorize

3. Re: Contrary read_file()

Thanks, Chris. I had copied the code from a preceding operation in which a subtree was extracted, and the 'wb' was appropriate. I didn't notice that the files I attempted to insert were reduced to 0 length (they still existed). Changing 'wb' to 'rb' fixed the problem. Thanks again.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu