Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Julia 为什么这个for循环可以';你不能跑两次吗?(朱莉娅)_Julia - Fatal编程技术网

Julia 为什么这个for循环可以';你不能跑两次吗?(朱莉娅)

Julia 为什么这个for循环可以';你不能跑两次吗?(朱莉娅),julia,Julia,我先打开了一些文本文件 r = open("sample_text.txt","r") 我用这个代码按行分割 for line in eachline(r) println(line) println(typeof(line)) end 并取得成果 A novel is a relatively long work of narrative fiction, normally written in prose form, and whi

我先打开了一些文本文件

r = open("sample_text.txt","r")
我用这个代码按行分割

for line in eachline(r)
    println(line)
    println(typeof(line))
end
并取得成果

A novel is a relatively long work of narrative fiction, normally written in prose form, and which is typically published as a book. The present English word for a long work of prose fiction derives from the Italian: novella for "new", "news", or "short story of something new", itself from the Latin: novella, a singular noun use of the neuter plural of novellus, diminutive of novus, meaning "new".
String

String
Some novelists, including Nathaniel Hawthorne, Herman Melville, Ann Radcliffe, John Cowper Powys, preferred the term "romance" to describe their novels.
String

但如果我再次运行forr循环,我不会得到任何结果。为什么会出现这种问题?

您已经从文件流中读取了所有数据

如果要再次读取,需要重置光标的位置:

seekstart(r)

eachline
迭代文件,循环结束时将指针留在文件末尾。所以在你第二次尝试阅读的时候,没有什么可以读的。。。使用
读取行
将行保存到数组中,再次打开文件,或使用
查找
重置文件指针