473,503 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

counter increment in a program

2 New Member
First of all, here is my C# code.
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.IO;
  3.  
  4. class funny
  5. {
  6.     static int i;
  7.     public static void Main()
  8.  
  9.     {
  10.  
  11.      //File.Create(@"E:\a.txt");
  12.      //Console.WriteLine("file a.txt successfully created");
  13.                                       File.AppendAllText(@"E:\a.txt", ++i + " hey baby\r\n");
  14.  
  15.     }
  16. }
  17.  
Now what i need is that everytime i run this program, the text "hey baby" should be written onto a.txt file.
But I do also need that there should be a serial number for each execution of the program just before the "hey baby".
Just like
1 hey baby
2 hey baby
3 hey baby
4 hey baby ...

I tried so hard, but i could not hit the target, any Happy Coders to help me out please !!
Jul 13 '11 #1
5 3180
DEEPAK SAHU
2 New Member
Sorry I have wrongly commented the following statement
[code]
Console.WriteLine("file a.txt successfully created");
File.AppendAllText(@"E:\a.txt", ++i + " hey baby\r\n");

[\CODE]
Its actually meant to be without comments.
Please somebody help.
Jul 14 '11 #2
Subin Ninan
91 New Member
Try this, (++i).ToString() + " hello baby"
Jul 14 '11 #3
AndrewBuchan
14 New Member
What you need to do is read from the a.txt file before you write your next 'hey baby' line, increment the value and then write out your text.
Jul 15 '11 #4
bvrwoo
16 New Member
Before you can append a file you must have some content (line(s)) to append.
First, use StreamWriter/TextWriter to add you the lines and append the ones you want to changed.
Aug 29 '11 #5
bvrwoo
16 New Member
You need to read the lines from the file first and then you need to pass in the line(s) you want to append with File.AppendAllText with the file name and line which is the context.
Aug 29 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

16
10626
by: Paul Rubin | last post by:
I'd like to have a function (or other callable object) that returns 0, 1, 2, etc. on repeated calls. That is: print f() # prints 0 print f() # prints 1 print f() # prints 2 # etc. ...
2
1983
by: Mike Brearley | last post by:
I have a counter (alright one I found on asp101.com) that checks for a session variable to prevent the counter from incrmenting if a user refreshes the page or returns to the page during the same...
52
4056
by: mavic | last post by:
hi to everybody,i need help for my project....how to make a program that accept any integer and convert it binary,octal and hexadecimal.pls help me...thanks
66
5278
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it...
5
5525
by: kiarash | last post by:
In a called function,I want to save the "program counter" , when program reached to a specified location ,& then return from the function . In the next calling of this function , I want to restore...
1
2507
by: k1ckthem1dget | last post by:
I am trying to write a program which sorts an array of supplied names. These are the errors i am getting. .cpp:39: error: expected identifier before ',char); void selectionSort(int , int);...
3
10116
blackstormdragon
by: blackstormdragon | last post by:
Here were our instructions: "My mother always took a little red counter to the grocery store. The counter was used to keep tally of the amount of money she would have spent so far on that visit to...
3
1621
by: CodeTilYaDrop | last post by:
Hi, I can not seem to get my counter to print out in Java. I have it setup to count each iteration of a loop in my program. Then, I want to print out the number of iterations of the loop. ...
30
2803
by: Paul W Smith | last post by:
I have written a hit counter, which I believe counts the times my page is hit by a user for each unique session by the user. The relevant part of my code is below: If...
9
2980
by: C#_Help_needed | last post by:
I need help with the following question. THANKS :) Write a program in c# that takes in a directory as a command line parameter, and returns the longest repeated phrase in ALL text files in that...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7348
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7006
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5592
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
397
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.