Deliciouscat
Deliciouscat

Reputation: 78

Why this for loop can't run twice? (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?

Upvotes: 3

Views: 87

Answers (1)

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42264

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)

Upvotes: 3

Related Questions