My goal is to check all the lines of the file (there is one word per line) and if the string is not present in the whole file, I have to add it at the bottom.
The problem is that the "is_Cursor" loop changes my "tx.Data" and therefore I have to give the for loop several times and start again several times from the beginning
fileWrite, err := os.OpenFile(fileName, os.O_APPEND|os.O_RDWR, os.ModeAppend)
if err != nil {
log.Fatalln(err)
}
defer fileWrite.Close()
fileRead, err := os.Open(fileName)
if err != nil {
log.Fatalln(err)
}
defer fileRead.Close()
isWrite := false
for is_Cursor {
for i := 0; i < len(tx.Data) && i < maxMemoryTx; i++ {
isWrite = false
//scannerWrite := bufio.NewScanner(fileWrite)
scannerRead := bufio.NewScanner(fileRead)
for scannerRead.Scan() {
fmt.Println(scannerRead.Text())
if scannerRead.Text() == tx.Data[i].Type {
println("block")
isWrite = true
}
}
if !isWrite {
if scannerRead.Scan() == false {
println("new")
fileWrite.WriteString(tx.Data[i].Type)
}
if _, err := fileWrite.WriteString("\n"); err != nil {
log.Fatal(err)
}
}
}
//other
}
Where am I doing wrong?
how do i check and then insert in the file if necessary?