Putting Auto Numbers in a File

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2007
Posts: 29
Reputation: shsh_shah is an unknown quantity at this point 
Solved Threads: 0
shsh_shah shsh_shah is offline Offline
Light Poster

Putting Auto Numbers in a File

 
0
  #1
Apr 5th, 2007
Hi,

Is there anyway i can put my every entry of username in a file like this e.g. using any auto number generation with every entry
1. Test
2. Hello

After i put them in a file like this i want to search and delete based on the numbers. e.g.

Please enter the number to delete the data: 1
"should delete the test from a file"

Please help. As any suggestions and location of any website will also be helpful.
Regards,
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Putting Auto Numbers in a File

 
0
  #2
Apr 6th, 2007
If you set up a counter, you can just write that to the beginning of each line.

>After i put them in a file like this i want to search and delete based on the numbers. e.g.

You can't delete lines in a file. You probably have to write a new file with the line you wish to delete omitted.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 29
Reputation: shsh_shah is an unknown quantity at this point 
Solved Threads: 0
shsh_shah shsh_shah is offline Offline
Light Poster

Re: Putting Auto Numbers in a File

 
0
  #3
Apr 8th, 2007
can you example of a counter please?

And can you explain please what you mean by "You probably have to write a new file with the line you wish to delete omitted"
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Putting Auto Numbers in a File

 
0
  #4
Apr 8th, 2007
>can you example of a counter please?
  1. counter = 0
  2. While read in a line
  3. counter = counter + 1
  4. writeToConsole + counter + "." + line
  5. endWhile


>After i put them in a file like this i want to search and delete based on the numbers. e.g.

1. You'd have to read each line of your text file into an array or arrayList.

2. Delete the original File (Using the File I.O Api)
e.g

3. Write the arrayList to another file with the original file name. Obviously you can remove the line number from the arrayList with the appropriate method.
e.g
Last edited by iamthwee; Apr 8th, 2007 at 7:17 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: Putting Auto Numbers in a File

 
0
  #5
Apr 9th, 2007
Originally Posted by iamthwee View Post
>can you example of a counter please?
  1. counter = 0
  2. While read in a line
  3. counter = counter + 1
  4. writeToConsole + counter + "." + line
  5. endWhile


>After i put them in a file like this i want to search and delete based on the numbers. e.g.

1. You'd have to read each line of your text file into an array or arrayList.

2. Delete the original File (Using the File I.O Api)
e.g

3. Write the arrayList to another file with the original file name. Obviously you can remove the line number from the arrayList with the appropriate method.
e.g
no i would not do it that way.

his best option would be to read the file into the application itself. use his parsing methods to read the file and then re-open the existing file using truncate method.

this will open the already existing file and empty the file to 0 bytes allowing him to re-write his data back in.
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Putting Auto Numbers in a File

 
0
  #6
Apr 10th, 2007
oh, I never heard of the truncate method. Do you have a link or an example?
Last edited by iamthwee; Apr 10th, 2007 at 4:41 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: Putting Auto Numbers in a File

 
0
  #7
Apr 10th, 2007
Originally Posted by iamthwee View Post
oh, I never heard of the truncate method. Do you have a link or an example?
sure do have an example

so he opens the file and does what he wants

  1. System.IO.Stream foo = new FileStream("pathToStream", FileMode.Open, FileAccess.Read);
  2.  
  3. //some random code to remove the line he wants removed

now when he goes to open the file stream again and use his newly modified string that has the new file contents he opens using the truncate method

  1. System.IO.Stream bar = new FileStream("PathToExistingFile", FileMode.Truncate, FileAccess.ReadWrite);

this opens the file and clears it out so that new information can be written to the file.
Last edited by Killer_Typo; Apr 10th, 2007 at 5:21 pm.
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 29
Reputation: shsh_shah is an unknown quantity at this point 
Solved Threads: 0
shsh_shah shsh_shah is offline Offline
Light Poster

Re: Putting Auto Numbers in a File

 
0
  #8
Apr 12th, 2007
Hi,
  1. Console.WriteLine("Please Enter your username :");
  2. int Counter = 0;
  3. s = Console.ReadLine();
  4. Counter = Counter + 1;
  5. sw.WriteLine(Counter + " " + s);
  6. sw.Write(sw.NewLine);
1. Where to put while condition so it can read file and increment the counter?
2. When every time i enter one name it comes out of getusername function and display the menu options again. How to prevent that?
Regards,
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 29
Reputation: shsh_shah is an unknown quantity at this point 
Solved Threads: 0
shsh_shah shsh_shah is offline Offline
Light Poster

Re: Putting Auto Numbers in a File

 
0
  #9
Apr 12th, 2007
  1. override public void getUserName()
  2. {
  3.  
  4. try
  5. {
  6. file = new FileStream(@"C:\username.txt", FileMode.Append, FileAccess.Write);
  7. // Create a new stream to write to the file
  8. sw = new StreamWriter(file);
  9.  
  10. Console.WriteLine("Please Enter your username :");
  11.  
  12. s = Console.ReadLine();
  13. Counter = Counter + 1;
  14. sw.WriteLine(Counter + " . " + s);
  15. sw.Write(sw.NewLine);
  16.  
  17. Console.WriteLine("Please Enter your Phone Number :");
  18. String ph = Console.ReadLine();
  19. int i = Int32.Parse(ph);
  20. sw.WriteLine(i);
  21.  
  22. // Close StreamWriter
  23. sw.Close();
  24. // Close file
  25. file.Close();
  26.  
  27. }
This is the code for above my query now i want to delete on above entry?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC