| | |
Putting Auto Numbers in a File
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2007
Posts: 29
Reputation:
Solved Threads: 0
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,
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,
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.
>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*
>can you example of a counter please?
>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
C# Syntax (Toggle Plain Text)
counter = 0 While read in a line counter = counter + 1 writeToConsole + counter + "." + line 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*
•
•
•
•
>can you example of a counter please?
C# Syntax (Toggle Plain Text)
counter = 0 While read in a line counter = counter + 1 writeToConsole + counter + "." + line 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
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!
•
•
•
•
oh, I never heard of the truncate method. Do you have a link or an example?

so he opens the file and does what he wants
C# Syntax (Toggle Plain Text)
System.IO.Stream foo = new FileStream("pathToStream", FileMode.Open, FileAccess.Read); //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)
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!
•
•
Join Date: Mar 2007
Posts: 29
Reputation:
Solved Threads: 0
Hi,
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,
C# Syntax (Toggle Plain Text)
Console.WriteLine("Please Enter your username :"); int Counter = 0; s = Console.ReadLine(); Counter = Counter + 1; sw.WriteLine(Counter + " " + s); sw.Write(sw.NewLine);
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,
•
•
Join Date: Mar 2007
Posts: 29
Reputation:
Solved Threads: 0
C# Syntax (Toggle Plain Text)
override public void getUserName() { try { file = new FileStream(@"C:\username.txt", FileMode.Append, FileAccess.Write); // Create a new stream to write to the file sw = new StreamWriter(file); Console.WriteLine("Please Enter your username :"); s = Console.ReadLine(); Counter = Counter + 1; sw.WriteLine(Counter + " . " + s); sw.Write(sw.NewLine); Console.WriteLine("Please Enter your Phone Number :"); String ph = Console.ReadLine(); int i = Int32.Parse(ph); sw.WriteLine(i); // Close StreamWriter sw.Close(); // Close file file.Close(); }
![]() |
Similar Threads
Other Threads in the C# Forum
- Previous Thread: C# scripting language
- Next Thread: How can I pause program?
| Thread Tools | Search this Thread |
.net access ado.net algorithm array backup barchart bitmap box broadcast buttons c# check checkbox client combobox control conversion csharp custom database databasesearch datagrid datagridview datagridviewcheckbox dataset datetime degrees development draganddrop drawing dynamiccreation encryption enum equation event excel file form format formatting forms function gdi+ httpwebrequest image index input install interface java label list listbox mandelbrot math mouse mouseclick mysql namevaluepairs operator path photoshop picturebox pixelinversion post powerpacks programming property radians regex remote remoting resource restore richtextbox server sleep socket sql statistics stream string table text textbox thread time timer update usercontrol validation visualstudio wait webbrowser windows winforms working wpf xml






