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,

Member Avatar for iamthwee

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.

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"

Member Avatar for iamthwee

>can you example of a counter please?

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

>can you example of a counter please?

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

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.

Member Avatar for iamthwee

oh, I never heard of the truncate method. Do you have a link or an example?

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

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

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.

Hi,

Console.WriteLine("Please Enter your username :");
                    int Counter = 0;
                    s = Console.ReadLine();
                    Counter = Counter + 1;
                    sw.WriteLine(Counter + " " + s);
                    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,

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();
                    
                }

This is the code for above my query now i want to delete on above entry?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.