Click here to Skip to main content
16,020,811 members
Please Sign up or sign in to vote.
2.75/5 (4 votes)
See more:
Hello everyone!
I have a .Dat file and "a" variable. Numbers are written line by line in the .Dat file. I need to modify that file by first writing the elements smaller than a, then the equal elements, and finally the smallest. Publish it all in a textBox.
I just started learning C#. Please, Help me.
Thank you.
C# windows application.

What I have tried:

i Just tried it.
string filePath = @"C:\Y\Y\DD1.DAT";
StreamReader stream = new StreamReader(filePath);
string filedata = stream.ReadToEnd();
var paragraphs = filedata.Split('\n');
txtTask.Text = paragraphs.ToString();
stream.Close();
Posted
Updated 11-Dec-21 12:10pm
Comments
Herman<T>.Instance 10-Dec-21 10:28am    
Have you tried Array.Sort()?
PIEBALDconsult 10-Dec-21 10:32am    
I assume a text sort won't be sufficient.
https://www.codeproject.com/Questions/5319852/Sort-txt-file-from-small-to-large
Member 15461778 10-Dec-21 10:33am    
No. I will. Thank you.
[no name] 10-Dec-21 11:23am    
You will need to convert all the input numbers to integer values, in order to sort them. Sorting them as characters will not get the correct order.
BillWoodruff 10-Dec-21 13:39pm    
Are the numbers represented by the line-strings always of the same Type ? i.e. : Int32, or Double /

To add to the suggestion by Herman, you don't need the stream or the split:
C#
string filePath = @"C:\Y\Y\DD1.DAT";
string[] paragraphs = File.ReadAllLines(filePath);

But this won't work anyway:
C#
txtTask.Text = paragraphs.ToString();
The system does not do anythign useful with this: it returns the name of the type, rather than any content:
System.String[]

It you want the content as a single string, you need to use the String.Join Method (System) | Microsoft Docs[^]
 
Share this answer
 
Comments:

0) requires Linq, uses the 'Func Delegate syntax for economy.

1) fortunately, conversion to Double from all the other numeric Types will work: otherwise, this would be a mess to implement.

2) the code here shows examples of extracting converted, filtered, numbers based on a function you supply. left for you to write is taking the sets of filtered data and turning them back into a .dat file you save ... hint: use a StringBuilder

public static class SelectNumbers
{   
    private static char[] split1 = new char[] {'\r', '\n'};

    public static double[] LinesToDouble(string data)
    {
        data = data.Trim();

        string[] lines = data.Split(split1, StringSplitOptions.RemoveEmptyEntries);

        var trimlines = lines
            .Select(line => line.Trim());

        return trimlines.Select(line => line.StringToDouble()).ToArray();
    }

    public static double StringToDouble(this string str)
    {
        double value;

        if (double.TryParse(str, out value))
        {
            return value;
        }

        throw new InvalidCastException($"{str} ... is not a number");
    }
}
usage example:
var ns = SelectNumbers.LinesToDouble(@"
    1
    2
    5
    3.999
    -4
    533
    5
    -6.456
");

    double[] matchlta = GetMatchingValues(4.0, ns, (double d1, double d2) => d1 < d2);

    double[] matcheqa = GetMatchingValues(5.0, ns, (double d1, double d2) => d1 == d2);
Feel free to ask questions.
 
Share this answer
 
v2
Comments
PIEBALDconsult 10-Dec-21 18:41pm    
But you lose the original formatting of the string values -- leading zeroes, scientific notation, etc.
Were I the teacher, that would be required in the output.
BillWoodruff 11-Dec-21 0:15am    
look ma, no errors:

000533 => 533
6.626E-34 => 6.626E-34
PIEBALDconsult 11-Dec-21 11:13am    
You lost the leading zeroes from the source -- fail.
BillWoodruff 11-Dec-21 11:19am    
you just go ahead and quibble all you need to :)
PIEBALDconsult 11-Dec-21 11:27am    
Well, the spec is unclear anyway.
The spec is unclear on a number of points, including edge cases, such as what if there are no values less than A and what if all the provided values are greater than A. I tried to address this as best I could.

C#
public sealed partial class ParsedDecimal : System.IComparable<ParsedDecimal>
{
  public static System.Globalization.NumberStyles NumberStyles ;

  public static ParsedDecimal MinValue { get ; private set ; }
  public static ParsedDecimal MaxValue { get ; private set ; }

  static ParsedDecimal
  (
  )
  {
    NumberStyles = System.Globalization.NumberStyles.Float ;

    MinValue = new ParsedDecimal ( System.Decimal.MinValue ) ;
    MaxValue = new ParsedDecimal ( System.Decimal.MaxValue ) ;

    return ;
  }

  public decimal Value { get ; private set ; }

  private string original ;

  private ParsedDecimal
  (
    decimal Value
  )
  {
    this.Value = Value ;

    this.original = Value.ToString() ;

    return ;
  }

  public ParsedDecimal
  (
    string Value
  )
  {
    this.Value = System.Decimal.Parse ( Value , NumberStyles ) ;

    this.original = Value ;

    return ;
  }

  public int
  CompareTo
  (
    ParsedDecimal Op1
  )
  {
    int result = this.Value.CompareTo ( Op1.Value ) ;

    if ( result == 0 )
    {
      result = this.original.CompareTo ( Op1.original ) ;
    }

    return ( result ) ;
  }

  public override string
  ToString
  (
  )
  {
    return ( this.original ) ;
  }
}

public static string
Separate
(
  decimal         A
,
  params string[] List
)
{
  System.Text.StringBuilder result =
    new System.Text.StringBuilder() ;

  ParsedDecimal least = ParsedDecimal.MaxValue ;

  System.Collections.Generic.List<ParsedDecimal> lest =
    new System.Collections.Generic.List<ParsedDecimal>() ;

  System.Collections.Generic.List<ParsedDecimal> less =
    new System.Collections.Generic.List<ParsedDecimal>() ;

  System.Collections.Generic.List<ParsedDecimal> same =
    new System.Collections.Generic.List<ParsedDecimal>() ;

  System.Collections.Generic.List<ParsedDecimal> more =
    new System.Collections.Generic.List<ParsedDecimal>() ;

  for ( int i = 0 ; i < List.Length ; i++ )
  {
    ParsedDecimal d = new ParsedDecimal ( List [ i ] ) ;

    if ( least.Value > d.Value )
    {
      least = d ;
    }

    if ( d.Value < A )
    {
      less.Add ( d ) ;
    }
    else if ( d.Value > A )
    {
      more.Add ( d ) ;
    }
    else
    {
      same.Add ( d ) ;
    }
  }

  for ( int i = 0 ; i < less.Count ; i++ )
  {
    if ( less [ i ].Value == least.Value )
    {
      lest.Add ( less [ i ] ) ;
    }
    else
    {
      result.Append ( less [ i ] ) ;
      result.AppendLine() ;
    }
  }

  if ( lest.Count == 0 )
  {
    lest = same ;
  }
  else
  {
    for ( int i = 0 ; i < same.Count ; i++ )
    {
      result.Append ( same [ i ] ) ;
      result.AppendLine() ;
    }
  }

  if ( lest.Count == 0 )
  {
    for ( int i = 0 ; i < more.Count ; i++ )
    {
      if ( more [ i ].Value == least.Value )
      {
        lest.Add ( more [ i ] ) ;
      }
    }
  }

  for ( int i = 0 ; i < lest.Count ; i++ )
  {
    result.Append ( lest [ i ] ) ;
    result.AppendLine() ;
  }

  return ( result.ToString() ) ;
}
 
Share this answer
 
v3

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