Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
I have a new file every few seconds that looks like this: 23 45 21 1 9 23 42 22 40 11 33 32 18 11 12 32 22 7 37 30 in this text file to be read there is one number per line that will be between 1-40. these files are generated several times a minute. I am trying to order them ascending with stringreader and writer. my logic must be flawed as nothing shows up in this file i intended to send it to, i selected append= true but still nothing is populated in my sorted file. the goal was to read from the text file with the for loop which contains 1-40 int variables and compare that to each string or int from the file read and when found copy that from the read file into the sorted file in sorted order,I have been looking at it for a while and it should work but does not please help i have hit a brick wall and don't see why it does not work. would this be easier with the file reader/writer classes or streamreader/writer as i have done,thanks

C#
public static void ProcessDirectory()
    {
        int variable1;
        StreamReader readToSort = new StreamReader(@"C:write.txt");
        StreamWriter writeSorted = new StreamWriter(@"C:Sorted_File.txt", true);
        for (int i = 1; i > 41; i++)
        {
            variable1 = (readToSort.Read());
            while (!readToSort.EndOfStream)
            {
                if (variable1 == i)
                {
                    writeSorted.Write(i.ToString() + "\n");
                }
            }
                MessageBox.Show("processing #" + variable1);
            }
            readToSort.Close();
            writeSorted.Close();
        }
Posted
Updated 4-Apr-15 8:05am
v4
Comments
PIEBALDconsult 4-Apr-15 14:11pm    
Interesting idea, but first you need to learn what readToSort.Read() actually does.
Plus, I think you have your loops inside-out.
Start by writing down, in your native language, the steps of the process you are trying teach the computer as if you are trying to teach your dog.
PIEBALDconsult 4-Apr-15 15:16pm    
Do you need to support duplicates?

C#
//Your 'for' statement looks incorrect.  Shouldn't it be i<41 NOT i>41?
for (int i = 1; i < 41; i++)
 
Share this answer
 
If You really, really, want to be Your way this would be correct code.


public static void ProcessDirectory()
{
int variable1 = 0;
int i = 0;

StreamReader readToSort;
StreamWriter writeSorted = new StreamWriter(@"C:Sorted_File.txt");

for (i = 1; i < 41; i++)
{
readToSort = new StreamReader(@"C:write.txt");
while (!readToSort.EndOfStream)
{
variable1 = int.Parse(readToSort.ReadLine().ToString());
if (variable1 == i)
{
writeSorted.WriteLine(variable1.ToString());
}
}
readToSort.Close();
}
writeSorted.Close();
}



This would be my solution for such problem if I have understood it correctly.


List <int> sorted_data = new List<int>();

StreamReader readToSort = new StreamReader("Input.txt");
while (!readToSort.EndOfStream)
{
sorted_data.Add(int.Parse(readToSort.ReadLine().ToString()));
}
readToSort.Close();
readToSort.Dispose();

sorted_data.Sort();

StreamWriter writeSorted = new StreamWriter("Output.txt");
foreach (var element in sorted_data)
{
writeSorted.WriteLine(element.ToString());
}
writeSorted.Close();
writeSorted.Dispose();


And do not forget using directives.

C#
using System.Collections.Generic;
using System.IO;



All the best,
Perić Željko
 
Share this answer
 
v10
if you have set of numbers unordered in one file and need to order them and save to a another file, then
C#
var numbers =File.ReadAllLines(inFilePath)
        .Select(line => int.Parse(line))
        .OrderBy(x=>x)
        .Select(x=>x.ToString());
File.WriteAllLines(outFilePAth, numbers);
 
Share this answer
 
Comments
PIEBALDconsult 4-Apr-15 15:02pm    
That seems a more-general solution than he's describing. It looks like he's trying to implement a much more efficient technique.
Perić Željko 5-Apr-15 16:09pm    
This example works excellent.
Do not forget the 'using System.Linq' directive.

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