Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why this for loop can't run twice? (Julia)

Tags:

julia

I opened some text file first.

r = open("sample_text.txt","r")

And I splited by line with using this code.

for line in eachline(r)
    println(line)
    println(typeof(line))
end

And get results

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

But if I run forr loop again, I didn't get any results. why do I get this kind of problem?

like image 689
Deliciouscat Avatar asked Dec 02 '25 09:12

Deliciouscat


1 Answers

You have read all of the data from the file stream.

If you want to read it again you need to reset the position of the cursor:

seekstart(r)
like image 106
Przemyslaw Szufel Avatar answered Dec 04 '25 01:12

Przemyslaw Szufel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!