Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, I'm trying to combine two of my methods into one to save space and improve functionality. Can anyone advise me how to combine these two codes so that they work? The first method is to write the line number at the beginning of the text document, the second method is to write the duration to the text document. I would like each line to have a number to show how much time it is.

EDIT2:
Can someone advise me how to add line numbering to my code?

What I have tried:

C#
public void lineNumber()
        {
            {
                StreamReader sr1 = File.OpenText(path);
                string s = "";
                int counter = 1;
                StringBuilder sb = new StringBuilder();
                while ((s = sr1.ReadLine()) != null)
                {
                    var lineOutput = counter++ + " " + s;
                    sb.Append(lineOutput);
                }
                sr1.Close();
                StreamWriter sw1 = File.AppendText(path);
                sw1.Write(sb);
                sw1.Close();

            }
        }

and
C#
private void writeText(string strPath, TimeSpan tsDuration)
       {
           var list = new List<TimeSpan>();
           string line;

           var streamReader = new StreamReader(strPath);
           while ((line = streamReader.ReadLine()) != null)
           {
               list.Add(TimeSpan.Parse(line));
           }
           list.Add(tsDuration / suma);
           list = list.OrderBy(i => i).ToList();
           streamReader.Close();
           StreamWriter streamWriter = new StreamWriter(strPath, false);
           foreach (var item in list)
           {
               streamWriter.WriteLine(item);
           }
           streamWriter.Close();
       }

EDIT:
C#
private List<TimeSpan> list;
private void writeText(string strPath, TimeSpan tsDuration)
       {
         readFile(strPath, tsDuration);
         writeFile(strPath);
       }
private void readFile(string strPath, TimeSpan tsDuration)
       {
           
           string line;

           var streamReader = new StreamReader(strPath);
           while ((line = streamReader.ReadLine()) != null)
           {
               list.Add(TimeSpan.Parse(line));//here I have error: String was not recognized as a valid timespan.
           }
           list.Add(tsDuration / suma);
           list = list.OrderBy(i => i).ToList();
           streamReader.Close();
        }
private void writeFile(string strPath);
        {
         StreamWriter streamWriter = new StreamWriter(strPath, false);
         foreach (var item in list)
           {
               streamWriter.WriteLine(item);
           }
          streamWriter.Close();
 
         }

EDIT2:
I don't know how to do it at all, I'll be very happy for advice.
Thank you for any advice!
Posted
Updated 21-Jun-21 0:43am
v4

1 solution

My advice: don't (as I have suggested already in your previous question). A method should do just one job. The reading method should read the input file and create a List<t> or other appropriate collection of objects. The business methods should manipulate the data as required. The writing method should just write the final data to the output file.
 
Share this answer
 
Comments
Member 15170612 21-Jun-21 4:44am    
So do you think I should just read to the list as a method, then write as a method and then to merge into the necessary method?
Richard MacCutchan 21-Jun-21 5:21am    
No, keep them separate. Use the Single Responsibility Principle (part of the SOLID group). Each method or function should be responsible for only one job. It makes the code cleaner, easier to understand, easier to test, and when something needs to change it does not require a large rewrite.

Think about what each method needs to do, and try and use meaningful names.

In your lineNumber method you are reading an input file, adding line numbers to each line and then writing the new strings to an output file. But why would you ever need the line numbers saved in a file? The only time you are likely to need them is in a print out used by a human.

In your writeText method you are reading an input file, extracting some data and again, writing to an output file. And again, why do you need to do that, since the data can be extracted from the original file any time you need to process it?
Member 15170612 21-Jun-21 5:29am    
In the writeText method, I do this to sort the values ​​so that they are listed in ascending order.

In lineNumber method, I would like to number it so that it can be better oriented in it when opening the file.

Sorry for names my method and all other functions I am not from an English speaking country.
Richard MacCutchan 21-Jun-21 5:33am    
But they are both a waste of time. The only time you need that information is when you are processing it. Saving it in other files serves no real purpose.
Member 15170612 21-Jun-21 5:46am    
I'm trying to improve it on the issue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900