473,657 Members | 2,450 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 3186
DEEPAK SAHU
2 New Member
Sorry I have wrongly commented the following statement
[code]
Console.WriteLi ne("file a.txt successfully created");
File.AppendAllT ext(@"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.AppendAllT ext 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
10642
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. There should never be any possibility of any number getting returned twice, or getting skipped over, even if f is being called from
2
1994
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 session. The problem is, it also prevents other page counters from incrementing. Here's the code for this part: ' Increment the count if Session("iCount") <> iCount then iCount = iCount + 1 Session("iCount") = iCount
52
4090
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
5337
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 occurs. After the program has read the file, it should offer a menu with three choices. the first is to list all the words along with the number of occurences. The second is to let you enter a word, with the program reporting how many times the...
5
5537
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 "pc+1" to continue the process. How can I do this?
1
2522
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); int main() { char names =
3
10124
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 the store if she bought everything in the basket. The counter had a four-digit display, increment buttons for each digit, and a reset button. An overflow indicator came up red if more money was entered than the $99.99 it would register. (This was a...
3
1629
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. I have this statement after my loop in the program: iteration_counter++ Then, I want to print it out the amount of times it went through the loop. I used this code:
30
2832
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 IsEmpty(Session("TotalCount")) Then ' Increment or reset the count If Weekday(Date(),vbSunday) = 1 And dtDate <Date then
9
2991
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 directory. Assume that all contents of all the files may be held in memory at the same time. Do not use unsafe code blocks and/ or pointers.
0
8421
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8325
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8742
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8518
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8621
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1971
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1734
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.