简体   繁体   中英

C# Saving “X” times into one .txt file without overwriting last string

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

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)

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);
    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM