检查文件并添加新的字符串。

huangapple go评论71阅读模式
英文:

check the file and add a new string

问题

我的目标是检查文件的所有行(每行一个单词),如果整个文件中不存在该字符串,则必须将其添加到文件底部。

问题在于“is_Cursor”循环会改变我的“tx.Data”,因此我必须多次给出for循环,并多次从头开始。

我在哪里做错了?

如何检查并在必要时插入文件中?

英文:

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?

答案1

得分: 1

检查文件的所有行(每行一个单词),如果整个文件中不存在该字符串,则将其添加到文件底部。


按照指示操作。读取文件中的所有单词,然后将任何新单词添加到末尾。例如,

func appendNewWords(fileName string, newWords []string) error {
    fileWords := make(map[string]bool)
    file, err := os.OpenFile(fileName, os.O_APPEND|os.O_RDWR, os.ModeAppend)
    if err != nil {
        return err
    }
    defer file.Close()

    scnr := bufio.NewScanner(file)
    for scnr.Scan() {
        fileWords[scnr.Text()] = true
    }
    if err := scnr.Err(); err != nil {
        return err
    }

    wtr := bufio.NewWriter(file)
    for _, word := range newWords {
        if !fileWords[word] {
            fileWords[word] = true
            _, err := wtr.WriteString(word)
            if err != nil {
                return err
            }
            err = wtr.WriteByte('\n')
            if err != nil {
                return err
            }
        }
    }
    err = wtr.Flush()
    if err != nil {
        return err
    }

    err = file.Close()
    if err != nil {
        return err
    }
    return nil
}
英文:

> 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.


Follow the instructions. Read all the words in the file. Then, add any new words at the end. For example,

func appendNewWords(fileName string, newWords []string) error {
	fileWords := make(map[string]bool)
	file, err := os.OpenFile(fileName, os.O_APPEND|os.O_RDWR, os.ModeAppend)
	if err != nil {
		return err
	}
	defer file.Close()

	scnr := bufio.NewScanner(file)
	for scnr.Scan() {
		fileWords[scnr.Text()] = true
	}
	if err := scnr.Err(); err != nil {
		return err
	}

	wtr := bufio.NewWriter(file)
	for _, word := range newWords {
		if !fileWords[word] {
			fileWords[word] = true
			_, err := wtr.WriteString(word)
			if err != nil {
				return err
			}
			err = wtr.WriteByte('\n')
			if err != nil {
				return err
			}
		}
	}
	err = wtr.Flush()
	if err != nil {
		return err
	}

	err = file.Close()
	if err != nil {
		return err
	}
	return nil
}

huangapple
  • 本文由 发表于 2022年10月10日 05:07:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/74008364.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定