写入文件并处理重复的条目

编程入门 行业动态 更新时间:2024-10-24 12:26:14
本文介绍了写入文件并处理重复的条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

1按钮 1需要写入的文件1 TextBox 1 NumericUpDown

1 Button 1 File That Needs To Be Written To 1 TextBox 1 NumericUpDown

因此,在我的应用程序中,我需要写入一个包含多行的文件。输入来自TextBox和NumericUpDown控件,它包含一般格式的字符串 string.Format({0} | {1},TextBoxInput,NumericUpDownInput); 。

So, in my application I need to write to a file that will contain several lines. Input is taken from a TextBox and a NumericUpDown control and it comprises a string of general format string.Format("{0}|{1}", TextBoxInput, NumericUpDownInput);.

我需要帮助的是在添加新行之前实际检查重复的条目。基本上,如果用户决定输入他们已经拥有的东西(用更多的时间更新它),程序应检查新输入的输入是否与其中一行相匹配,如果匹配,则 {1} 部分应加在一起并替换原始值,同时保持 {0} 部分。

What I need help with is actually checking for duplicate entries before adding a new line. Basically, if the user decides to enter something they already had (to update it with more "times" of it), the program should check whether the newly entered input matches one of the lines, and if it does, the {1} parts should be added together and replace the original value, while maintaining the {0} part.

我尝试解决这个问题的方法是创建一个名为 newFile的字符串列表和 for循环用于循环 originalFile 以查找是否为新输入与已输入的输入匹配。

The way I tried to approach this is by creating a list of type string called newFile and a for loop used to loop through the originalFile so as to find if the new input matches the already entered one.

然后,两种可能的情况:a)如果是,只需替换数字输入部分并将其添加到newFile,b)如果它没有,只需将其添加到newFile。

Then, two possible cases: a) if it does, just replace the numeric input part and add it to the newFile, b) if it doesn't, just add it to the newFile.

最后,我使用StreamWriter,以便用newFile覆盖originalFile。

At the end, I make use of a StreamWriter so as to overwrite the originalFile with the newFile.

不幸的是,作为我的方法,我遇到了一些麻烦无论出于何种原因,都要测试一个空如果我只是使用StreamWriter部分而不考虑重复的条目,它实际上工作正常。

Unfortunately, I'm having quite a bit of trouble as my approach generates a blank file for whatever reason. If I was to just use the StreamWriter part without taking duplicate entries into account, it actually works just fine.

哦,还有一件事;如果程序还可以检查文件是否存在于第一位,以避免异常,那将是很好的。我已经设法了,但我不认为这是最好的方法。请帮帮我。提前谢谢你。

Oh, and one more thing; it would be neat if the program could also check for whether the file exists in the first place so as to avoid exceptions. I have managed this but I do not think it is the best way to go about it. Help me please. Thank you in advance.

下面是按钮点击事件的代码,它基本上更新/添加到文件中(毕竟这是你需要的唯一代码):

Below is the code for the button click event which basically updates/adds to the file (this is the only code you need, after all):

private void btnAdd_Click(object sender, EventArgs e) { // If input is either blank or invalid [-] (the input is verified through a database), show error message. if (cardTitle == "" || cardTitle == "-") MessageBox.Show("Correct any errors before trying to add a card to your Resources.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); // If the file does not exist, create it and close the stream object so as to further use the file. if (!File.Exists("CardResources.ygodc")) using (File.Create("CardResources.ygodc")) { }; string[] originalFile = File.ReadAllLines("CardResources.ygodc"); List<string> newFile = new List<string>(); for (int count = 0; count < originalFile.Length; count++) { string[] split = originalFile[count].Split('|'); string title = split[0]; string times = split[1]; if (title == cardTitle) newFile[count].Replace(string.Format("{0}|{1}", title, times), string.Format("{0}|{1}", title, (nudTimes.Value + int.Parse(times)).ToString())); else newFile.Add(string.Format("{0}|{1}", cardTitle, nudTimes.Value.ToString())); } using (StreamWriter sw = new StreamWriter("CardResources.ygodc", true)) { foreach (string line in newFile) { sw.WriteLine(line); } } }

PS:对不起,如果我不够清楚,我不是母语为英语的人。

P.S: Sorry if I don't come out as clear enough, I'm not a native English speaker.

编辑:如果你想知道 cardTitle 代表什么,它基本上是来自TextBox的输入。

If you're wondering what cardTitle stands for, it's basically the input from the TextBox.

编辑2: 我认为我的方法中的主要错误是我从一个空的 newFile 列表开始,而不是仅仅编辑 originalFile 一个。您怎么看?

EDIT 2: I figure the main mistake in my approach is the fact that I begin with an empty newFile list, instead of just editing the originalFile one. What do you think about that?

推荐答案

始终将字符串添加到newFile(String.Format在转换类型时非常聪明到它的字符串表示,所以你不需要在 int 上调用ToString。并且不要追加,而是将完整的文件写回磁盘。

Always ADD the string to the newFile (and String.Format is pretty clever in converting types to its string representation so you don't need to call ToString on an int). And don't append but the write the complete file back to disk.

List<string> newFile = new List<string>(); bool isMatched = false; if (File.Exists("CardResources.ygodc")) { string[] originalFile = File.ReadAllLines("CardResources.ygodc"); for (int count = 0; count < originalFile.Length; count++) { string[] split = originalFile[count].Split('|'); string title = split[0]; string times = split[1]; if (title == cardTitle) { newFile.Add(string.Format( "{0}|{1}", title, nudTimes.Value + int.Parse(times))); isMatched =true; } else newFile.Add(string.Format( "{0}|{1}", title, times)); } } if (!isMatched) { newFile.Add(string.Format( "{0}|{1}", cardTitle, nudTimes.Value)); } using (StreamWriter sw = new StreamWriter("CardResources.ygodc")) { foreach (string line in newFile) { sw.WriteLine(line); } }

输入和输出样本:

Input | Output card| Value | ---------------------- A | 1 | A|1 Input | Output card| Value | ---------------------- B | 1 | A|1 | B|1 Input | Output card| Value | ---------------------- C | 1 | A|1 | B|1 | C|1 Input | Output card| Value | ---------------------- A | 2 | A|3 | B|1 | C|1

更多推荐

写入文件并处理重复的条目

本文发布于:2023-11-30 05:28:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648877.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条目   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!