Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find EOF while reading from a file

Tags:

I am using the following code to read a file in Go:

spoon , err := ioutil.ReadFile(os.Args[1])
if err!=nil {
        panic ("File reading error")
}

Now I check for every byte I pick for what character it is. For example:

spoon[i]==' ' //for checking space

Likewise I read the whole file (I know there maybe other ways of reading it) but keeping this way intact, how can I know that I have reached EOF of the file and I should stop reading it further?

Please don't suggest to find the length of spoon and start a loop. I want a sure shot way of finding EOF.

like image 327
Worlock Avatar asked Jan 22 '13 22:01

Worlock


People also ask

How do I know if a file is EOF?

feof() The function feof() is used to check the end of file after EOF. It tests the end of file indicator. It returns non-zero value if successful otherwise, zero.

How do I find my EOF character?

The EOF in C/Linux is control^d on your keyboard; that is, you hold down the control key and hit d. The ascii value for EOF (CTRL-D) is 0x05 as shown in this ascii table . Typically a text file will have text and a bunch of whitespaces (e.g., blanks, tabs, spaces, newline characters) and terminate with an EOF.

Where is EOF in a file?

Master C and Embedded C Programming- Learn as you go The End of the File (EOF) indicates the end of input. After we enter the text, if we press ctrl+Z, the text terminates i.e. it indicates the file reached end nothing to read.


3 Answers

Use io.EOF to test for end-of-file. For example, to count spaces in a file:

package main  import (     "fmt"     "io"     "os" )  func main() {     if len(os.Args) <= 1 {         fmt.Println("Missing file name argument")         return     }     f, err := os.Open(os.Args[1])     if err != nil {         fmt.Println(err)         return     }     defer f.Close()     data := make([]byte, 100)     spaces := 0     for {         data = data[:cap(data)]         n, err := f.Read(data)         if err != nil {             if err == io.EOF {                 break             }             fmt.Println(err)             return         }         data = data[:n]         for _, b := range data {             if b == ' ' {                 spaces++             }         }     }     fmt.Println(spaces) } 
like image 121
peterSO Avatar answered Oct 27 '22 11:10

peterSO


ioutil.ReadFile() reads the entire contents of the file into a byte slice. You don't need to be concerned with EOF. EOF is a construct that is needed when you read a file one chunk at a time. You need to know which chunk has reached the end of the file when you're reading one chunk at a time.

The length of the byte slice returned by ioutil.ReadFile() is all you need.

data := ioutil.ReadFile(os.Args[1])

// Do we need to know the data size?
slice_size := len(data)

// Do we need to look at each byte?
for _,byte := range data {
    // do something with each byte
}
like image 26
Daniel Avatar answered Oct 27 '22 11:10

Daniel


This is what you need to look for to find out about End Of File(EOF)

if err != nil {
        if errors.Is(err, io.EOF) { // prefered way by GoLang doc
            fmt.Println("Reading file finished...")
        }
        break
    }
like image 27
MoHo Avatar answered Oct 27 '22 11:10

MoHo