第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

讀取剛剛寫入臨時(shí)文件的數(shù)據(jù)

讀取剛剛寫入臨時(shí)文件的數(shù)據(jù)

Go
湖上湖 2022-07-04 16:57:46
在 Go 中,我試圖將數(shù)據(jù)寫入一個(gè)臨時(shí)文件,然后我轉(zhuǎn)身閱讀但沒有成功。下面是一個(gè)精簡(jiǎn)的測(cè)試程序。我已經(jīng)通過檢查臨時(shí)文件驗(yàn)證了數(shù)據(jù)是否正在寫入文件。所以,至少我知道數(shù)據(jù)正在進(jìn)入文件中。我只是無法讀出來。提前謝謝你的幫助package mainimport (    "bufio"    "fmt"    "io/ioutil"    "log"    "os"    "path/filepath")func main() {    tmpFile, err := ioutil.TempFile("", fmt.Sprintf("%s-", filepath.Base(os.Args[0])))    if err != nil {        log.Fatal("Could not create temporary file", err)    }    fmt.Println("Created temp file: ", tmpFile.Name())    //  defer os.Remove(tmpFile.Name())    fmt.Println("Writing some data to the temp file")    if _, err = tmpFile.WriteString("test data"); err != nil {        log.Fatal("Unable to write to temporary file", err)    } else {        fmt.Println("data should have been written")    }    fmt.Println("Trying to read the temp file now")    s := bufio.NewScanner(tmpFile)    for s.Scan() {        fmt.Println(s.Text())    }    err = s.Err()    if err != nil {        log.Fatal("error reading temp file", err)    }}
查看完整描述

2 回答

?
倚天杖

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊

ioutil.TempFile創(chuàng)建一個(gè)臨時(shí)文件并打開文件進(jìn)行讀寫并返回結(jié)果*os.File(文件描述符)。因此,當(dāng)您在文件中寫入時(shí),指針會(huì)移動(dòng)到該偏移量,即,它當(dāng)前位于文件末尾。但是由于您的要求是從文件中讀取的,您需要Seek使用方法返回到開頭或任何所需的偏移量*os.File.Seek。因此,添加tmpFile.Seek(0, 0)將為您提供所需的行為。


另外,作為一個(gè)好習(xí)慣,不要忘記關(guān)閉文件。請(qǐng)注意,我使用defer tmpFile.Close()了在退出之前關(guān)閉文件的方法。


請(qǐng)參考以下示例:


package main


import (

    "bufio"

    "fmt"

    "io/ioutil"

    "log"

    "os"

    "path/filepath"

)


func main() {

    tmpFile, err := ioutil.TempFile("", fmt.Sprintf("%s-", filepath.Base(os.Args[0])))

    if err != nil {

        log.Fatal("Could not create temporary file", err)

    }

    defer tmpFile.Close()


    fmt.Println("Created temp file: ", tmpFile.Name())


    fmt.Println("Writing some data to the temp file")

    if _, err = tmpFile.WriteString("test data"); err != nil {

        log.Fatal("Unable to write to temporary file", err)

    } else {

        fmt.Println("Data should have been written")

    }


    fmt.Println("Trying to read the temp file now")


    // Seek the pointer to the beginning

    tmpFile.Seek(0, 0)

    s := bufio.NewScanner(tmpFile)

    for s.Scan() {

        fmt.Println(s.Text())

    }

    if err = s.Err(); err != nil {

        log.Fatal("error reading temp file", err)

    }

}

更新:來自 OP 的評(píng)論:


鑒于刪除實(shí)際文件也被延遲,是否需要延遲關(guān)閉?如果是這樣,我想延期的順序很重要。


所以,這是一個(gè)很好的問題。所以基本的經(jīng)驗(yàn)法則是關(guān)閉文件然后刪除。因此,甚至可以先刪除然后關(guān)閉它,但這取決于操作系統(tǒng)。


如果您參考C++ 的文檔:


如果文件當(dāng)前由當(dāng)前進(jìn)程或其他進(jìn)程打開,則此函數(shù)的行為是實(shí)現(xiàn)定義的(特別是,POSIX 系統(tǒng)取消鏈接文件名,盡管文件系統(tǒng)空間不會(huì)回收,即使這是最后一次硬鏈接到文件直到最后運(yùn)行的進(jìn)程關(guān)閉文件,Windows 不允許刪除文件)


因此,在 Windows 上,如果您先嘗試刪除它而不關(guān)閉它,那肯定會(huì)出現(xiàn)問題。


所以,因?yàn)閐efer's 是堆疊的,所以執(zhí)行的順序是


defer os.Remove(tmpFile.Name()) // Called 2nd

defer tmpFile.Close() // Called 1st


查看完整回答
反對(duì) 回復(fù) 2022-07-04
?
桃花長(zhǎng)相依

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個(gè)贊

package main


import (

    "fmt"

    "io/ioutil"

    "log"

)


func main() {

    content, err := ioutil.ReadFile("testdata/hello")

    if err != nil {

        log.Fatal(err)

    }


    fmt.Printf("File contents: %s", content)

根據(jù) golang 官方文檔。


查看完整回答
反對(duì) 回復(fù) 2022-07-04
  • 2 回答
  • 0 關(guān)注
  • 132 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)