-1

I'm changing a value within my text, but instead, it's overwriting my whole .txt file with nothing.

static void editClassMates(classMates[] classMateInfo)
{
    StreamWriter sw = new StreamWriter(@"C:\Users\Callum\Documents\class.txt");
    Console.Write("Who's number would you like to change? ");
    string classMateInput = Console.ReadLine();
    for (int i = 0; i < classMateInfo.Length; i++)
    {
        if (classMateInfo[i].last.Equals(classMateInput))
        {
            Console.Write("Enter new number: ");
            string temp = Console.ReadLine();
            int classMateNumber = Convert.ToInt32(temp);
            classMateInfo[i].ID = classMateNumber;
        }   
    }
    sw.Close();
}

My code asks for a name within my .txt file. Once I have specified the name, it loops through until my input matches the same exact name in my .txt file. I now want to change his/her number. After I input a number, I then have a method that displays the list and the change I have made. That works, it shows me my original data along with the value I've edited.

However, I go into my class.txt file and it's completely blank now.

My question is, what part of my code is overwriting my whole file? Also, how can I properly change/edit/replace a specific value inside my .txt file?

Thanks

crashmstr
  • 28,043
  • 9
  • 61
  • 79
bsd u
  • 77
  • 1
  • 6
  • 4
    `new StreamWriter(@"C:\Users\Callum\Documents\class.txt", true)` you are not using the overload to append. This is easily viewed in Intellisense as you type – Ňɏssa Pøngjǣrdenlarp Jun 11 '15 at 12:19
  • 1
    @Plutonix Why don't you put this as an answer? Are you waiting someone to copy-paste it as his own answer? –  Jun 11 '15 at 12:20
  • 1
    Hint: The `File.Open`-Method is even more easy to use. Topic in [this SO article](http://stackoverflow.com/questions/7405828/streamwriter-rewrite-the-file-or-append-to-the-file). @Desolator It's not worth an answer ^.^ – Patrik Jun 11 '15 at 12:22
  • 1
    @Desolator Well, some people is not interested in reputation points and just want to help others ;-) – Oscar Jun 11 '15 at 12:22
  • 1
    possible duplicate of [Append lines to a file using a StreamWriter](http://stackoverflow.com/questions/7306214/append-lines-to-a-file-using-a-streamwriter) – Ňɏssa Pøngjǣrdenlarp Jun 11 '15 at 12:23
  • @PatrikEckebrecht I am interested in them too :) I put it as a community-wiki answer ! –  Jun 11 '15 at 12:28
  • @Oscar I am not interested in them too :) I put it as a community-wiki answer ! –  Jun 11 '15 at 12:29
  • As a side point: you only show code to *open* the file and *close* it, but no code to actually write anything to the file (and since you don't append, your file is empty). – crashmstr Jun 11 '15 at 12:30
  • asking which part overwriting the content of the file is okay.. people answered it.. but immediately asking who to read/update/delete value form text file is different. that should be separate one.. – Deepak Sharma Jun 11 '15 at 12:32
  • Show us how you are **reading** your file initially, then we can help you with modifying it... – Idle_Mind Jun 11 '15 at 14:17

1 Answers1

0
new StreamWriter(@"C:\Users\Callum\Documents\class.txt", true)

You are not using the overload to append. This is easily viewed in Intellisense as you type.

Hint: The File.Open-Method is even more easy to use. Topic in this SO article

Community
  • 1
  • 1
  • This is not writing to my .txt file. – bsd u Jun 11 '15 at 12:45
  • See the comments above by @crashmstr –  Jun 11 '15 at 12:46
  • I'm not trying to add a new value to the end. I'm simply trying to edit a value somewhere in between my .txt file. Hence the looping until the correct value is found. Once found, replace their number with my input. I have that set up, but it's really not editing the value at all. – bsd u Jun 11 '15 at 12:52
  • @bsdu Can your file be large (more than 10MBs)? If no, simple read the entire file in a string, replace values in it as you want, and write this string back to the file. –  Jun 11 '15 at 12:56
  • @bsdu your code posted does not write anything nor read anything! To *change* a file, the simplest way is to write out the whole file with the changes. So in other words: write out every element of `classMateInfo` to your file (`WriteAllLines` is very good for this). – crashmstr Jun 11 '15 at 13:20