ZetCode

FreeBasic Line Input Keyword

Last modified April 20, 2026

The FreeBasic Line Input keyword reads an entire line of text from a file or the console. It captures all characters until a newline is encountered, making it ideal for reading complete text lines.

Basic Definition

In FreeBasic, Line Input is a statement that reads a complete line of text from an input source. It stops reading when it encounters a newline character or reaches the end of the file.

Unlike Input, which stops at commas or spaces, Line Input reads everything until the line terminator. This makes it perfect for reading text that might contain delimiters.

Basic Console Input

This example demonstrates reading a line of text from the console.

line_input_console.bas
Dim userInput As String
Print "Enter your name: "
Line Input userInput
Print "Hello, "; userInput; "!"

Here we use Line Input to read a complete line from the user. The entire input, including spaces, is stored in userInput. This is useful when you need to capture names or sentences.

Reading From a File

Line Input is commonly used to read text files line by line.

line_input_file.bas
Dim fileNum As Integer = FreeFile()
Dim lineText As String
Open "example.txt" For Input As #fileNum
While Not EOF(fileNum)
 Line Input #fileNum, lineText
 Print lineText
Wend
Close #fileNum

This code opens a text file and reads it line by line. Each line is printed to the console. The loop continues until EOF (End Of File) is reached. Always close files after use.

Handling Empty Lines

Line Input can read empty lines from files or console input.

line_input_empty.bas
Dim lines(3) As String
Dim i As Integer
Print "Enter 4 lines (can be empty):"
For i = 0 To 3
 Line Input lines(i)
Next
Print "You entered:"
For i = 0 To 3
 Print i; ": '"; lines(i); "'"
Next

This example shows that Line Input correctly handles empty lines. The program stores each line in an array, then displays them with their line numbers. Empty lines show as empty strings.

Reading Special Characters

Line Input can read lines containing commas, quotes, and other special characters that would break regular Input.

line_input_special.bas
Dim csvLine As String
Print "Enter CSV data (with commas):"
Line Input csvLine
Print "You entered: "; csvLine
Dim parts() As String
Split(csvLine, ",", parts())
Print "First field: "; parts(0)

This demonstrates reading CSV data containing commas. Line Input captures the entire line, which can then be processed. The Split function separates the fields.

Combining With File Operations

This example shows a complete file processing routine using Line Input.

line_input_processing.bas
Function CountLines(filename As String) As Integer
 Dim fileNum As Integer = FreeFile()
 Dim lineCount As Integer = 0
 Dim dummy As String
 Open filename For Input As #fileNum
 
 While Not EOF(fileNum)
 Line Input #fileNum, dummy
 lineCount += 1
 Wend
 
 Close #fileNum
 
 Return lineCount
End Function
Print "File has "; CountLines("data.txt"); " lines"

This function counts lines in a file using Line Input. Each Line Input call advances through the file. The counter increments for each line read. The file is properly closed after counting.

Error Handling

Proper error handling is important when using Line Input with files.

line_input_error.bas
Dim fileNum As Integer
Dim lineText As String
On Error Goto errorHandler
fileNum = FreeFile()
Open "missing.txt" For Input As #fileNum
Line Input #fileNum, lineText
Print lineText
Close #fileNum
End
errorHandler:
Print "Error: "; Err
If fileNum Then Close #fileNum

This example shows basic error handling for file operations. The On Error statement redirects to the error handler if the file does not exist. The handler prints the error and ensures the file is closed if it was opened.

Advanced File Processing

This example demonstrates more complex file processing with Line Input.

line_input_advanced.bas
Sub ProcessLogFile(filename As String)
 Dim fileNum As Integer = FreeFile()
 Dim lineText As String
 Dim errorCount As Integer = 0
 
 Open filename For Input As #fileNum
 
 While Not EOF(fileNum)
 Line Input #fileNum, lineText
 
 If InStr(lineText, "ERROR") Then
 Print "Found error: "; lineText
 errorCount += 1
 End If
 Wend
 
 Close #fileNum
 
 Print "Total errors found: "; errorCount
End Sub
ProcessLogFile("app.log")

This subroutine processes a log file, counting lines containing "ERROR". Each line is read with Line Input and checked for the keyword. The total count is displayed after processing the entire file.

Best Practices

  • File Handling: Always check for EOF when reading files.
  • Error Handling: Implement proper error handling for file operations.
  • Memory: Be cautious with very long lines that might exhaust memory.
  • Closing Files: Always close files after processing.
  • Validation: Validate input when reading from untrusted sources.

This tutorial covered the FreeBasic Line Input keyword with practical examples showing its usage in different scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all FreeBasic Tutorials.

AltStyle によって変換されたページ (->オリジナル) /