0

I have a txt file , In this file I need only int value , how can I get this value ?

txt file - Profit = 10 at the Time of ->04-06-2021 20:21:59

I am getting this file by using this code ..!

    func Profit() string {
    TargetClosePrice := 110
    ontickerPrice := 100
    Time := time.Now()
    totalProfit := TargetClosePrice - ontickerPrice
    str := strconv.Itoa(totalProfit)
    value := `Profit = ` + str + ` at the Time of ->` + Time.Format("01-02-2006 15:04:05") + "\n"

    data, err := os.OpenFile("Profit.txt", os.O_APPEND, 0644)
    if err != nil {
        log.Fatal("whoops", err)
    }
    io.Copy(data, strings.NewReader(value))
    return str
} 

but now I need only 10 from txt file..? how can I get ?

1 Answer 1

1

I think this does what you want:

package main

import (
   "bufio"
   "fmt"
   "os"
)

func main() {
   f, e := os.Open("Profit.txt")
   if e != nil {
      panic(e)
   }
   defer f.Close()
   s := bufio.NewScanner(f)
   for s.Scan() {
      var n int
      fmt.Sscanf(s.Text(), "Profit = %v", &n)
      fmt.Println(n)
   }
}

https://golang.org/pkg/fmt#Scanf

Sign up to request clarification or add additional context in comments.

1 Comment

thank's ..! can you tell me how can I get sum of all the data @StevenPenny

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.