9 Replies - 448 Views - Last Post: 11 October 2012 - 01:41 PM Rate Topic: -----

#1 chenaz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 24-November 11

(writefile) and loop

Posted 09 October 2012 - 07:58 PM

Hi,

i created a button which will create a log file which contain a running number and other info.
the number will increase after each click. this works fine if the application is running and the number log.writeline(i) will increase the number by 1-2-3-4-5...

how do i make the application to detect the last number of the i and continue the number after i re run the application.

current problem is. everytime i restart the application, the loop will start from one again which is not wat i want. i wan it to continue from last number that i had closed.

 
Int32 i;

        #region Functions
        private void button2_Click(object sender, EventArgs e)
        {
   string titletext = "History log ";
           
            i = ++i;

            // Create a writer and open the file:

            StreamWriter log;
    
            if (!File.Exists("running.log"))
            {
                log = new StreamWriter("running.log");
                log.WriteLine(i);

            }
            else
            {
                log = File.AppendText("running.log");

                log.WriteLine(i);

            }




            // Write to the file:
            log.WriteLine(titletext + " of " + DateTime.Now);
            log.WriteLine("User:" + textBox2.Text);
            log.WriteLine("From" + dateTimePicker1);
            log.WriteLine("To" + dateTimePicker2);
 
            // Close the stream:
            log.Close();


Is This A Good Question/Topic? 0
  • +

Replies To: (writefile) and loop

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: (writefile) and loop

Posted 09 October 2012 - 09:00 PM

You'll have to read the log file and find the last number used.
Was This Post Helpful? 0
  • +
  • -

#3 chenaz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 24-November 11

Re: (writefile) and loop

Posted 09 October 2012 - 09:28 PM

View PostMomerath, on 09 October 2012 - 09:00 PM, said:

You'll have to read the log file and find the last number used.

so after reading the file. wat should i do?
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5571
  • View blog
  • Posts: 11,908
  • Joined: 02-June 10

Re: (writefile) and loop

Posted 09 October 2012 - 09:31 PM

Read the file line by line.
Get the value
When you get to the end, you know the last number used.
Continue as you previously described was working for you.

Don't over think it.
Was This Post Helpful? 0
  • +
  • -

#5 chenaz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 24-November 11

Re: (writefile) and loop

Posted 09 October 2012 - 10:53 PM

View PosttlhIn`toq, on 09 October 2012 - 09:31 PM, said:

Read the file line by line.
Get the value
When you get to the end, you know the last number used.
Continue as you previously described was working for you.

Don't over think it.


first thanks all. under
log.WriteLine("i: {0}", data["i"]);

as my running number was "i"
the system show an error saying cant find the keyword in the dictionary

so this is my new code.
 if (!File.Exists("running.log"))
            {
                log = new StreamWriter("running.log");
                log.WriteLine(i);
            }
            else 
            {
               
                var data = File
                      .ReadAllLines("running.log")
                      .Select(x => x.Split('='))
                      .Where(x => x.Length > 1)
                      .ToDictionary(x => x[0].Trim(), x => x[1]);
                log = File.AppendText("running.log");
                log.WriteLine("i: {0}", data["i"]);
                
                
            }

Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5571
  • View blog
  • Posts: 11,908
  • Joined: 02-June 10

Re: (writefile) and loop

Posted 10 October 2012 - 12:20 PM

try this as a test.


log.WriteLine(string.format("i: {0}", data["i"]));
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5571
  • View blog
  • Posts: 11,908
  • Joined: 02-June 10

Re: (writefile) and loop

Posted 10 October 2012 - 12:54 PM

Oh crap...

var data = File
                     .ReadAllLines("running.log")
                     .Select(x => x.Split('='))
                     .Where(x => x.Length > 1)
                     .ToDictionary(x => x[0].Trim(), x => x[1]);
               log = File.AppendText("running.log");
               log.WriteLine("i: {0}", data["i"]);              
           }



It looks like you are reading the entire file to get the next line number - EVERY TIME YOU WRITE TO THE FILE.

Are you kidding? So if you write 100 entries you read the entire file 99 times? Wait until your log is a few thousand lines long and you see how badly this slows down your application. It will get to the point were it takes longer to read/write than for your program to operate: IE, 3 log entires a second, but each entry takes 5 seconds to create... so the program lags more and more with every entry.

You might want to re-think your logic. Why would you have to read the entire file EVERY TIME you write? Just do it one time to get the line number you need WHEN YOU LAUNCH THE APPLICATION, and store that.

This is a great example of what we say over and over and over....
Plan, plan some more, plan again, then rethink... Then plan some more...
All before you ever type a single character.

You have to decide if you want to be a "software engineer" or a code monkey. There's work out there for both. But if you want to be an actual software engineer then you need to take the time to do the engineering part of the software. Otherwise, there's always (lower paying) work for code monkey's that just do the grunt work that someone else engineered, defined and laid out.
Was This Post Helpful? 1
  • +
  • -

#8 chenaz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 24-November 11

Re: (writefile) and loop

Posted 10 October 2012 - 07:04 PM

Yes i noe this problem will happen.

so advise me on the code as i am not a very good in coding.
and ur code does not work also.

giving error(The given key was not present in the dictionary.)

my result is somethng like this.

1
History log of 11/10/2012 10:08:12 AM
User:
FromSystem.Windows.Forms.DateTimePicker, Value: 11/10/2012 10:08:10 AM
ToSystem.Windows.Forms.DateTimePicker, Value: 11/10/2012 10:08:10 AM

after i restart the program. i wan it to continue the number 1and continue with 2.
since it is first character. wat should i use for the split?
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4525
  • View blog
  • Posts: 7,893
  • Joined: 08-June 10

Re: (writefile) and loop

Posted 11 October 2012 - 06:57 AM

*
POPULAR

Quote

and ur code does not work also.


You do realize that was your code that he copied to highlight a point? If you don't recognize your own code, that makes me believe that you didn't write it in the first place, you found it and copied it.

Quote

so advise me on the code as i am not a very good in coding.


I'm tired of people telling me how bad at coding they are. If you're bad, get better or quit. Don't get other people to do your work for you.
Was This Post Helpful? 5
  • +
  • -

#10 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5571
  • View blog
  • Posts: 11,908
  • Joined: 02-June 10

Re: (writefile) and loop

Posted 11 October 2012 - 01:41 PM

View Postchenaz, on 10 October 2012 - 08:04 PM, said:

so advise me on the code as i am not a very good in coding.


I already gave you advice on this.

Quote

Just do it one time to get the line number you need WHEN YOU LAUNCH THE APPLICATION, and store that.


You won't get better if you don't make efforts. Trial and error. You might consider learning more from books and tutorials and STOP trying to architect new programs until you know more. You can't start out designing cars: You have to first learn how engines work and the basics of aerodynamics and ergonomic for example.


My standard beginner resources post

First learn the language by working 2-5 "Learn C# in 30 days" type books cover to cover. Do a couple hundred on-line tutorial projects where you build what you're told to build, the way you are told to build it WITH AN EXPLANATION OF WHY so you can learn.

Then later you can start architecting your own simple stuff. Build a calculator. Build a DVD library program. Etc. Stuff that doesn't involve the complexity of a game. Then move up to games.



There are three routes people seem to take when learning programming.
  • Just start trying to create programs
  • Start taking apart other programs and try to figure out the language by reverse engineering
  • Follow a guided learning course (school or self-teaching books)


For the life of me I can't figure out why people try 1 & 2. I strongly suggest taking the guided learning approach. Those book authors go in a certain order for a reason: They know what they're doing and they know the best order to learn the materials.

Quote

Where do I start?


You start by learning a coding language FIRST.
Learn to plan before you type.
THEN you start designing software with a purpose.


If this sounds like you

Newbie/Rookie said:

I have a little programming experience but I need to write ...
read this section
Spoiler


Otherwise, you can just jump to the resources here:
Some of the tutorials below are for C# or Java not C, C++, VB.NET [...]. But the conceptual stuff of classes, object oriented design, events etc. are not language specific and should give you enough guidance in theory of program development for you to be able to look-up specific code example in your chosen coding language.



Resources, references and suggestions for new programmers. - Updated Mar 2012
Spoiler

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1