943,709 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 3915
  • C# RSS
Apr 5th, 2007
0

Putting Auto Numbers in a File

Expand Post »
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,
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
shsh_shah is offline Offline
29 posts
since Mar 2007
Apr 6th, 2007
0

Re: Putting Auto Numbers in a File

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 8th, 2007
0

Re: Putting Auto Numbers in a File

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"
Reputation Points: 10
Solved Threads: 0
Light Poster
shsh_shah is offline Offline
29 posts
since Mar 2007
Apr 8th, 2007
0

Re: Putting Auto Numbers in a File

>can you example of a counter please?
C# Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 9th, 2007
0

Re: Putting Auto Numbers in a File

Click to Expand / Collapse  Quote originally posted by iamthwee ...
>can you example of a counter please?
C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Apr 10th, 2007
0

Re: Putting Auto Numbers in a File

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 10th, 2007
0

Re: Putting Auto Numbers in a File

Click to Expand / Collapse  Quote originally posted by iamthwee ...
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

C# Syntax (Toggle Plain Text)
  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

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Apr 12th, 2007
0

Re: Putting Auto Numbers in a File

Hi,
C# Syntax (Toggle Plain Text)
  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,
Reputation Points: 10
Solved Threads: 0
Light Poster
shsh_shah is offline Offline
29 posts
since Mar 2007
Apr 12th, 2007
0

Re: Putting Auto Numbers in a File

C# Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Light Poster
shsh_shah is offline Offline
29 posts
since Mar 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: C# scripting language
Next Thread in C# Forum Timeline: How can I pause program?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC