4 Replies - 611 Views - Last Post: 06 February 2013 - 07:18 PM Rate Topic: -----

#1 aya_eltokhy   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 07-November 12

Edit in file code

Posted 06 February 2013 - 05:02 PM

hi everyone
this code is for edit a txt file but the there is no change in file can any one detect where is the error
 private void button1_Click(object sender, EventArgs e)
         {

             Sname = textBox1.Text;
             Spass = textBox2.Text;
             ArrayList all = new ArrayList();
                        ArrayList name = new ArrayList();
                        ArrayList pass = new ArrayList();
                        int k=0,i;
                        try
                         {
                            StreamReader z = new StreamReader("D:\\users.txt");

                       while (true)
                         {

                              string line = z.ReadLine();

                              if (line == null)
                               {  
                                  z.Close();
                                  break;
                               }
                             string[]words= line.Split(',');

                             all.Add(line);
                             name.Add(words[0]);
                             pass.Add(words[1]);

                           }
                              z.Close();
                             
                        
                          }
         
               catch (FileNotFoundException)
                              { MessageBox.Show("file not found"); }
 

             for ( i = 0; i < name.Count; i++)
                         {
                            if (name.Contains(label3.Text) && pass.Contains(label6.Text))
                               {
                                  k = pass.IndexOf(Spass);
                                  break;
                               }
                            if (k < all.Count)
                            {
                                all.RemoveAt(k);
                                all.Insert(k, Sname + "," + Spass);
                            }
                           
                            
                          
                            
                           
 try
 {
     StreamWriter m = new StreamWriter("D:\\users.txt");
    foreach (String s in all)
     { m.WriteLine(s);
     
     }
     m.Flush();
     m.Close();
 }

catch (FileNotFoundException)
 { MessageBox.Show("file not found"); }

 }
             this.Close();
         }

 


Is This A Good Question/Topic? 0
  • +

Replies To: Edit in file code

#2 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4240
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Edit in file code

Posted 06 February 2013 - 05:13 PM

Moved to the C# help forum. Please make sure that you don't post help requests in the Tutorials sections!
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6534
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Edit in file code

Posted 06 February 2013 - 06:00 PM

maybe your if at line 42 just never finds a match.
These are case sensitive comparisons after all.

I'm not sure I can figure out what you are trying to accomplish.
LIne 49 removes the spass if it is found then line 40 inserts it back in?

None of this makes much sense and you gave us no idea what you're really trying to do.
Was This Post Helpful? 1
  • +
  • -

#4 aya_eltokhy   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 07-November 12

Re: Edit in file code

Posted 06 February 2013 - 06:20 PM

I have a file contauns a number of users each user has name and password is
supposed to make chane in one of this data users

and iam sure that this user its found in file by another code in another gui
form
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,454
  • Joined: 05-May 12

Re: Edit in file code

Posted 06 February 2013 - 07:18 PM

OMG! Use some consistent indent style!

Looking at your code above, it looks like the loop at line 40 is missing a closing curly brace. ... or is it the curly brace on line 71?

Anyway, the reason for no change in your file is because you never get a chance to write out the file with the modified list. You modify the list on lines 47-51, but you break out of loop before you write out the modified list.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1