Ruby 过早 EOF?

发布于 2024-11-16 22:05:24 字数 550 浏览 2 评论 0原文

我正在尝试用 Ruby 将一个文件写入另一个文件,但输出似乎过早停止。

输入文件 - 带有 base64 嵌入字体的大型 CSS 文件

输出文件 - 基本 html 文件。

#write some HTML before the CSS (works)
...
#write the external CSS (doesn't work, output finished prematurely)
while !ext_css_file.eof()        
    out_file.puts(ext_css_file.read())
end
...
#write some HTML after the CSS (works)

生成的文件基本上是一个有效的 HTML 文件,带有截断的 CSS(在嵌入字体的中间)

当对 read() 的结果执行 put 时,我得到相同的结果: CSS 文件只读到此最后一个字符串:“RMSHhoPCAGt/mELDBESFBQSggGfAgESKCUAAAAAAAwAlgABAAAAAAABAAUADAABAAAAAAAC”

I'm trying to write one file into another one in Ruby, but the output seems to stop prematurely.

Input file - large CSS file with base64 embedded fonts

Output file - basic html file.

#write some HTML before the CSS (works)
...
#write the external CSS (doesn't work, output finished prematurely)
while !ext_css_file.eof()        
    out_file.puts(ext_css_file.read())
end
...
#write some HTML after the CSS (works)

The resulting file is basically a valid HTML file, with a truncated CSS (in the middle of an embedded font)

When doing a puts on the result of read(), I get the same result: The CSS file is read only up to this last string: "RMSHhoPCAGt/mELDBESFBQSggGfAgESKCUAAAAAAAwAlgABAAAAAAABAAUADAABAAAAAAAC"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耶耶耶 2024-11-23 22:05:24

如果不深入了解 CSS 文件实际包含的内容,就很难提供详细的解决方案。根据上面的代码,我会尝试这样的方法:

#write some HTML before the CSS (works)
...
#write the external CSS (doesn't work, output finished prematurely)
out_file.puts(ext_css_file.read())
...
#write some HTML after the CSS (works)

我认为您不需要 .eof 检查,因为 read 方法读取并返回整个文件内容,或者空字符串,如果位于文件末尾则为 nil。请参阅此处: http://apidock.com/ruby/IO/read

我倾向于阅读并写入相同类型的数据。例如,如果我使用 puts 将数据写入新文件,我将使用 readlines 读取数据。如果我使用 write 写入二进制数据,我将使用 read 读取数据。我会与字符串或字节保持一致,而不是混合两者。

尝试这样的事情......

File.open('writable_file_path', 'w') do |f|
  # f.puts "some html"
  f.puts IO.readlines('css_file_path')
  # f.puts "some more html"
end

It is difficult to provide a detailed solution without more insight into what the CSS file actually contains. Based on your code above, I would try something like this instead:

#write some HTML before the CSS (works)
...
#write the external CSS (doesn't work, output finished prematurely)
out_file.puts(ext_css_file.read())
...
#write some HTML after the CSS (works)

I don't think you need the .eof check because the read method reads and returns the entire file contents, or an empty string or nil if at the end of file. See here: http://apidock.com/ruby/IO/read

I would tend to read and write the same type of data. For instance if I were writing data into the new file using puts, I would read data using readlines. If I were writing binary data using write, I would read the data using read. I would be consistent with either strings or bytes and not mix the two.

Try something like this...

File.open('writable_file_path', 'w') do |f|
  # f.puts "some html"
  f.puts IO.readlines('css_file_path')
  # f.puts "some more html"
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文