Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You would need to open the txt file with write permission.</p> <pre><code>file = File.new("#{spec}.txt", "w") </code></pre> <p>You could consult <a href="https://stackoverflow.com/questions/7911669/create-file-in-ruby">How to create a file in Ruby</a></p> <hr> <p><strong>Update</strong>: your code is not complete and looks buggy.</p> <ol> <li>Cant say what is <code>path</code></li> <li>Looks like you are trying to read the text file to which you intend to write <code>file.readlines.each</code></li> <li>spell check <code>length</code> you have it <code>l.lenght</code></li> </ol> <p>You may want to paste the actual code.</p> <hr> <p>Check this gist <a href="https://gist.github.com/4160587" rel="nofollow noreferrer">https://gist.github.com/4160587</a></p> <p>As mentioned, your code is not working because you are reading and writing to the same file.</p> <p><strong>Example</strong></p> <p>Ruby code <code>file_write.rb</code> to do the file write operation</p> <pre><code>pdf_file = File.open("in.txt") output_file = File.open("out.txt", "w") # file to which you want to write #iterate over input file and write the content to output file pdf_file.readlines.each do |l| output_file.puts(l) end output_file.close pdf_file.close </code></pre> <p>Sample txt file <code>in.txt</code></p> <pre><code>Some text in file Another line of text 1. Line 1 2. Not really line 2 </code></pre> <p>Once your run <code>file_write.rb</code> you should see new file called out.txt with same content as <code>in.txt</code> You could change the content of input file if you want. In your case you would use pdf reader to get the content and write it to the text file. Basically first line of the code will change.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload