添加一个换行符到一个文件 [英] Adding a newline to a file

查看:123
本文介绍了添加一个换行符到一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我写的代码是:



[Full code request]

  print(Word:)#Prompt for word to scrambe 
word = gets.chomp #Inputs word
wordLength = word.length#获取字长
randomAmount = wordLength * wordLength#找出有多少种随机化方式
amountDone = 0 #Loop变量随机化
,而amountDone!= randomAmount
放置--------------------------- \\\

x = word.chars.to_a.shuffle#将每个字符更改为一个数组并随机化它们
File.open(output.txt,a +){| f | f.write(x)}
puts#{amountDone}:\\\
#{x}
amountDone + = 1
puts---------- ----------------- \\\

end
puts输入任意键以继续...
endWord = gets.chomp #关闭程序提示

-



<$ p $ File.open(output.txt,a +){| f | f.write(x)}

但是问题在于那一行^是循环的。这意味着它实际上反复重复了很多次。所以无论何时你打开输出文件,输出都被挤在一起。所以我写的程序基本上是把文字拼凑成尽可能多的方式。所以,如果我的输入是 ABCDEFGH,则该文件中的输出将被显示为一个连续的行:




bdfcegAhhbgfeAcdhcedbAgfdfcAhgebefcdbghAdAfhegbcAegdfhbcbchdgefAhAbedfcgdAfcbhgefhgdAbceefdhcgAbAefbhgcdfAcebdhgAebgchfddhcfAbegcAdfbhgecAgdfhebedAghbfcedbAchgfbhAcgfdeceghAbfddAbfehcgbAhefdgcecfghbdAAhcgdfbedchAgfbebfhAgecdedAhcbgfAfdceghbehdcAbfgcegdhbfAfdAbchgegAhbfecdgeAdhfcbcbdAehfgfhgbcAedchdgbefAfhecdAbgAbedgcfhehcgfbdAAhgcebfdbAcehgfddfchgebAhcAbegdffAbehgcdchdbgAfebeAhgdfcbegcdhfAfecbdhAgdbfehgAcdbcehgfAgdehfcbAbgedAcfhdgcAfehbdfhAgecbcAgdhebfghbAefcdgebhAfdcgecdbAfhgbcAhfedhAbfgdcebAedfhcgbdfchAge

我已经试过了

  File.open(output.txt,a +){| f | f.write(x)+} 

  File.open(output.txt,a +){| f | f.write(x),} 

  File.open(output.txt,a +){| f | f.write(x)} 

但不起作用。那么我怎么能解决这个问题呢?解决方案当我想出这个解决方案的时候,我竟然嘲笑它有多么荒唐。



我真的必须做的是单独添加行距。因此:

  File.open(output.txt,a +){| f | f.write(x)} 

所有需要做的事情就是添加这一行: p>

  File.open(output.txt,a +){| f | f.write()} 

所以它是:

  File.open(output.txt,a +){| f | f.write(x)} 
File.open(output.txt,a +){| f | f.write(x)}#这很搞笑

所以这个解决方案一直很简单,如果有更简单的解决方案(也许代码在一行而不是2)。

我认为@ seph的答案实际上虽然工作,但仍然在它写它的信件,然后一个空格。我认为如果你把puts改为write,它会写入每一个。

  File.open('testfile.txt ','a +')do | f | 
(1..3).each do | i |
f.puts(#{x}#{randomAmount})
end
end

#把$换成

.open('output.txt','a +')do | f |
(randomAmount).each do | i |
f.write(#{amountDone}:\\\
#{x})
end
end

然后我认为它可以工作。我不知道肯定,因为我试过这个实际工作或没有,因为我错过了我的output.txt文件,所以我认为我的主目录中的空的一个有输出(这是不正确的),所以我得到了错误的想法。

但是要感谢所有试过的人!

How do you write a newline to a file in Ruby?

My code to write to the file is:

[Full code request]

print("Word:")                                  #Prompt for word to scrambe
word = gets.chomp                               #Inputs word
wordLength = word.length                        #Gets word length
randomAmount = wordLength * wordLength          #Finds out how many ways to randomize
amountDone = 0                                  #Loop variable for process of randomizing
while amountDone != randomAmount
puts "---------------------------\n"
x = word.chars.to_a.shuffle                 #Changes each character to an array and randomizez them
File.open("output.txt","a+") {|f| f.write(x)}
puts "#{amountDone}:\n #{x}"                
amountDone += 1
puts "---------------------------\n"
end
puts "Type any key to continue..."
endWord = gets.chomp                            #Close program prompt

-

File.open("output.txt","a+") {|f| f.write(x) }

But the problem is that that line ^ is in a loop. Meaning it's repeated over and over again quite a lot of times actually. So whenever you open the output file, the output is squashed together. So what the program I've wrote basically does is it scrambles words into as many ways as possible. So if my input was "Abcdefgh", then the output in the file is going to be displayed as one continuous line:

bdfcegAhhbgfeAcdhcedbAgfdfcAhgebefcdbghAdAfhegbcAegdfhbcbchdgefAhAbedfcgdAfcbhgefhgdAbceefdhcgAbAefbhgcdfAcebdhgAebgchfddhcfAbegcAdfbhgecAgdfhebedAghbfcedbAchgfbhAcgfdeceghAbfddAbfehcgbAhefdgcecfghbdAAhcgdfbedchAgfbebfhAgecdedAhcbgfAfdceghbehdcAbfgcegdhbfAfdAbchgegAhbfecdgeAdhfcbcbdAehfgfhgbcAedchdgbefAfhecdAbgAbedgcfhehcgfbdAAhgcebfdbAcehgfddfchgebAhcAbegdffAbehgcdchdbgAfebeAhgdfcbegcdhfAfecbdhAgdbfehgAcdbcehgfAgdehfcbAbgedAcfhdgcAfehbdfhAgecbcAgdhebfghbAefcdgebhAfdcgecdbAfhgbcAhfedhAbfgdcebAedfhcgbdfchAge

So, what I want to do is I want to have some sort of separation between the output. So every time it's outputted there is either a new line, a space, a pipe, a slash, etc.

I've already tried

File.open("output.txt","a+") {|f| f.write(x)+" " }

and

File.open("output.txt","a+") {|f| f.write(x)," " }

and

File.open("output.txt","a+") {|f| f.write(x)" " }

but it doesn't work. So how might I be able to solve this?

解决方案

When I figured out this solution I actually laughed at how ridiculously simple it was.

All I really had to do was add the spacing separately along with the line. So for:

File.open("output.txt","a+") {|f| f.write(x) }

all that needed to be done was to add this line:

File.open("output.txt","a+") {|f| f.write(" ") }

so it would be:

File.open("output.txt","a+") {|f| f.write(x) }
File.open("output.txt","a+") {|f| f.write(x) }      #This is hilarious

So the solution was quite simple all along, although if there are more simple solutions(perhaps code on one line instead of 2).

I think @seph's answer actually worked though, but still in the same way where it wrote it letter by letter then a space. I think it would write each individually if you changed "puts" to "write".

File.open('testfile.txt', 'a+') do |f|
(1..3).each do |i|
f.puts("#{x} #{randomAmount}")
end
end

#change that to

File.open('output.txt', 'a+') do |f|
(randomAmount).each do |i|
f.write("#{amountDone}:\n#{x}")
end
end

and then I think it works. I don't know for certain since the time I tried this it actually worked or not because I had misplaced my output.txt file and so I assumed the empty one in my home directory had the output (which wasn't true) so I got the wrong idea.

But thanks to all who tried!

这篇关于添加一个换行符到一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆