C# 保存“X”一次写入一个 .txt 文件而不覆盖最后一个字符串

发布于 2024-10-06 10:40:15 字数 290 浏览 0 评论 0原文

好吧,现在我有一个新问题。 我正在用 C# 编写代码,

每次在文本框中输入字符串并单击“保存”按钮时,我想将 textBoxName 保存到 group.txt 文件中。它应该按此顺序保存(如果可以像 AZ 一样排序那就太好了):

1.佩塔尔·米卢蒂诺维奇
2. 利贾娜·米卢蒂诺维奇
3.斯特凡·米卢蒂诺维奇
4. ...等

我无法让它工作,我尝试使用第一个问题中的技术,但还没有解决方案:(

我想这很简单,但我仍然是初学者,我非常需要这个...

Well, now i have a new problem.
Im writing code in C#

I want to save from textBoxName into group.txt file each time i enter string into textbox and click on save button. It should save at this order (if its possible to sort it like A-Z that would be great):


1. Petar Milutinovic
2. Ljiljana Milutinovic
3. Stefan Milutinovic
4. ... etc

I cant get it to work, i tried to use tehniques from my first question, and no solution yet :(

This is easy one i guess, but im still a beginer and i need this baddly...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

滿滿的愛 2024-10-13 10:40:15

尝试从自上而下的方法来解决这个问题。写出应该发生什么,因为从你的问题来看这并不明显。

示例:

  1. 用户在(单行?)文本框中输入一个值
  2. 用户单击“保存”
  3. 将在文件末尾添加一个新行,其中包含步骤 1 中文本框的内容

注意:每行都以行号为前缀,采用“X.Sample”形式,其中 X 是行号,Sample 是文本框中的文本。

以上准确吗?

(如果您只想向文本文件添加一行,请参阅http://msdn.microsoft.com/en-us/library/ms143356.aspx - File.AppendAllText(filename , myTextBox.Text + Environment.NewLine); 可能是你想要的)

Try to tackle this from a top-down approach. Write out what should happen, because it's not obvious from your question.

Example:

  1. User enters a value in a (single-line?) textbox
  2. User clicks Save
  3. One new line is appended to the end of a file, with the contents of the textbox in step 1

Note: each line is prefixed with a line number, in the form "X. Sample" where X is the line number and Sample is the text from the textbox.

Is the above accurate?

(If you just want to add a line to a text file, see http://msdn.microsoft.com/en-us/library/ms143356.aspx - File.AppendAllText(filename, myTextBox.Text + Environment.NewLine); may be what you want)

蒲公英的约定 2024-10-13 10:40:15

这是一个简单的小例程,可用于读取、排序和写入文件。有很多方法可以做到这一点,我的可能不是最好的。即使现在我也在想“我可以使用 FileStream 编写它并完成计数迭代”,但它们是可以稍后完成的微优化如果您遇到多兆字节文件的性能问题。

    public static void AddUserToGroup(string userName)
    {
        // Read the users from the file
        List<string> users = File.ReadAllLines("group.txt").ToList();
        // Strip out the index number
        users = users.Select(u => u.Substring(u.IndexOf(". ") + 2)).ToList();
        users.Add(userName); // Add the new user
        users.Sort((x,y) => x.CompareTo(y)); // Sort
        // Reallocate the number
        for (int i = 0; i < users.Count; i++)
        {
            users[i] = (i + 1).ToString() + ". " + users[i];
        }
        // Write to the file again
        File.WriteAllLines("group.txt", users);
    }

Here's a simple little routine you can use to read, sort, and write the file. There are loads of ways this can be done, mine probably isn't even the best. Even now I'm thinking "I could have written that using a FileStream and done the iteration for counting then", but they're micro-optimizations that can be done later if you have performance issues with multi-megabyte files.

    public static void AddUserToGroup(string userName)
    {
        // Read the users from the file
        List<string> users = File.ReadAllLines("group.txt").ToList();
        // Strip out the index number
        users = users.Select(u => u.Substring(u.IndexOf(". ") + 2)).ToList();
        users.Add(userName); // Add the new user
        users.Sort((x,y) => x.CompareTo(y)); // Sort
        // Reallocate the number
        for (int i = 0; i < users.Count; i++)
        {
            users[i] = (i + 1).ToString() + ". " + users[i];
        }
        // Write to the file again
        File.WriteAllLines("group.txt", users);
    }
深者入戏 2024-10-13 10:40:15

如果您需要在每次添加新行时对文件进行排序,则必须将文件加载到列表中,添加行并对其进行排序,或者使用某种搜索(我建议使用二进制search)来确定新行所属的位置并相应地插入它。不过,第二种方法没有太多优点,因为您基本上必须重写整个文件才能插入一行 - 它只会在最好的情况下节省您的时间,这种情况发生在要插入的行落在文件末尾。此外,第二种方法对处理器的影响较小,因为您并不尝试对每一行进行排序 - 但是对于小文件,差异将不明显。

If you need the file to be sorted every time a new line is added, you'll either have to load the file into a list, add the line, and sort it, or use some sort of search (I'd recommend a binary search) to determine where the new line belongs and insert it accordingly. The second approach doesn't have many advantages, though, as you basically have to rewrite the entire file in order to insert a line - it only saves you time in the best case scenario, which occurs when the line to be inserted falls at the end of the file. Additionally, the second method is a bit lighter on the processor, as you aren't attempting to sort every line - for small files however, the difference will be unnoticeable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文