Login Register
Frontpage Code library Pastebin

ReadLineFromFile()

Author: Jare
Added: 6. maaliskuuta 2021 kello 12.32
Edited: 6. maaliskuuta 2021 kello 12.32
Category: Tiedostot

Description

Avaa tiedoston ja lukee siitä halutun yksittäisen rivin. Tiedosto myös suljetaan lopuksi, eikä sinun tarvitse käsitellä tiedoston osoitinta lainkaan.

Code

Select all
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//
// Opens a file in read mode and finds the desired line number and returns the line string.
// Parameters:
// - file_path$: The file name that should be read.
// - line_number: The line number that we want to get. Starts from one. Defaults to one.
// - if_no_file: What to return if the file does not exist. Defaults to an empty string.
// - if_no_line: What to return if the file exists but if it does not have enough lines. Defaults to an empty string.
//
Function ReadLineFromFile(file_path$, line_number = 1, if_no_file$="", if_no_line$="")
	Dim file, current_line$, current_line_number
	
	If FileExists(file_path) Then
		file = OpenToRead(file_path)
		current_line_number = 0
		While Not EOF(file)
			current_line = ReadLine(file)
			current_line_number + 1
			If current_line_number = line_number Then
				CloseFile file
				Return current_line
			EndIf
		Wend
		CloseFile file
		Return if_no_line
	Else
		Return if_no_file
	EndIf
EndFunction

Comments

No comments. You can be first!

Leave a comment

You must be logged in to comment.