-3

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 StringWriter. My logic must be flawed as nothing shows up in this file I intended to send it to. I passed true as the append parameter but still nothing is populated in my sorted file.

The goal was to read from the text file with the for loop which iterates over 1-40 int values 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. Would this be easier with the file reader/writer classes or streamreader/writer as I have done?

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();
}
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
Deland
  • 1
  • 3
  • Are you familiar with `List` of String..? List is a collection which will have Sort functionality.. also why not change the function that reads a stream and use the `File.ReadAllLines` Function into a List then sort it..? I can do this without a for loop and just looking with actual code 3 to 4 lines of code if not less.. – MethodMan Apr 04 '15 at 17:22
  • the file will be very large and will not fit in RAM i need to do it piece by piece why crap on somebody for a question and just answer it??? – Deland Apr 04 '15 at 17:28
  • what's considered very large.. can you elaborate..? – MethodMan Apr 04 '15 at 17:33
  • 3
    You are much more likely to get useful answers if you don't bite the hand trying to feed you. Nobody is "crapping" on you; I see the previous comment as a simple attempt to elicit more information from you, so as to try to understand the problem better. Sometimes when obvious solutions are not used, it's because the OP wasn't aware of them; other times it's because there's some specific requirement not to use them. In any case, hostile reactions to attempts to help are counter-productive to your goals. – Peter Duniho Apr 04 '15 at 17:33
  • the negative stroke i thought came from that person, sorry i'm new here – Deland Apr 04 '15 at 17:35
  • I need to organize many files that have 20 random numbers one per line into another file many times the method that does this will be called many times i just thought it would be easier to receive and copy organized rather than bring it into memory as an array each time – Deland Apr 04 '15 at 17:39
  • 1
    I am still having trouble understanding what you are actually trying to accomplish here. The code you posted has obvious errors in it -- reading a single character instead of a whole line at a time, infinite loop after the first read, to name a couple -- and really doesn't have anything that would help understand what output you want for a given input. If you literally want to sort a file, you have to sort all of the data together; with a 6GB file, maybe this means you want to do a merge sort. Or maybe you want something other than a whole-data sort? It's not clear from your question. – Peter Duniho Apr 04 '15 at 17:40
  • agreed, that is what i'm trying to do get the random file read it write it back to another file ordered using the least assets in memory – Deland Apr 04 '15 at 17:42
  • 1
    Please provide some example input data, formatted separately in your question if the data is in multiple files, and show (and explain) clearly what output you want to achieve given that sample input. Please read http://stackoverflow.com/help/how-to-ask for other advice on how to present your question in a clear, answerable way. – Peter Duniho Apr 04 '15 at 17:42
  • to clarify, I am reading many text files that have 20 int variables one per line values between 1-40 and what im trying to do is copy them in ascending order to another file called "sorted" 1..2..3... as each comes in. that is what the method is to do. – Deland Apr 04 '15 at 17:46
  • peter, its just random int values on a text file one per line – Deland Apr 04 '15 at 17:47
  • 1
    Nevertheless, please be clear in your question what the input is. You've provided only a single list of integers. If you want to process multiple files, you need to be very clear how those files relate to each other and how you expect to process them together. In addition, you still have not provided any example output at all. Finally, please note that while you get notification of any comments on your question, commenters won't know you've responded unless you signal by typing the `@` symbol followed by their user name in your own comment. E.g. @Deland. – Peter Duniho Apr 04 '15 at 17:50
  • why are people so mean on this site with the negatives. its not a stupig question or it would have been solved quickly....right – Deland Apr 04 '15 at 17:51
  • i did not know that i'm new sorry – Deland Apr 04 '15 at 17:53
  • @PeterDuniho this is the goal..... read the file in the easiest way possible by line as that is the way it is formatted (one # per line) and if it corresponds to the # 1 or 2 or 3 etc that is the counter on the loop it passes it to the sorted file if not does nothing and reads the file again for "2" then "3" this sorting the file without having to bring into memory any arrays or variables aside from the 2 that are being copied and moves actually one. thanks for your interest – Deland Apr 04 '15 at 18:03
  • _"its not a stupig question or it would have been solved quickly"_ -- downvotes are used for a variety of things, and almost never to literally describe the question as "stupid". But yes, your question is poorly presented, difficult to understand, and possibly not useful to others; any or all of these problems can lead to downvoting. Worry less about the downvoting, and more about helping others to understand your question better. – Peter Duniho Apr 04 '15 at 18:07
  • @peter Duniho thanks for the advice but how should i revise the code to do what I need it to do......do you have any ideas – Deland Apr 04 '15 at 18:12
  • 1
    I don't know how to have any ideas, as I don't yet _know_ what you are trying to do. I understand that you _think_ you have adequately explained the goal, but please believe me when I say that you haven't. Please consider your question as a complete outsider, who has zero knowledge of the problem, and think about what that outsider would need in order to solve it. There is still a lot of detail missing from your question, as has been explained by various comments above (including my own). – Peter Duniho Apr 05 '15 at 01:13
  • A person could try to guess what you mean, but personally I'm not going to spend time writing an answer for a question that I have only _guessed_ is the question. It's far too easy to guess wrong and waste time formulating and writing up an answer that is completely irrelevant and useless. – Peter Duniho Apr 05 '15 at 01:14

1 Answers1

0

To make sure I correctly understand the problem you are trying to solve, I made a list of requirements based on your question and the comments below it.

  • Your input consists of text files which are several gigabytes large, and therefore cannot be fully loaded into memory
  • These text files consist of numeric values only, with each value being on its own line
  • These numeric values need to be written to another output file, in sorted order

It's not entirely clear to me what your input consists of so you might need to correct me here. Do you need to combine multiple (smaller) input files, sort the combined contents, and output that to a single (larger) file?

Example:

  • Input: file1_unsorted.txt (6GB), file2_unsorted.txt (6GB)
  • Output: file1_and_file2_sorted.txt (12GB)

If so, is each individual file small enough to be loaded in memory (but just not the combined whole?)

Example (assuming 1GB RAM):

  • Input: file1_unsorted.txt (600MB), file2_unsorted.txt (600MB), ..., file10_unsorted.txt (600MB)
  • Output: file1_through_file10_sorted.txt (6GB)

Or, can each individual input file be large enough that it does not fit in memory, and do these files each need to be sorted to a corresponding output file?

Example:

  • Input: file_unsorted.txt (6GB)
  • Output: file_sorted.txt (6GB)

Assuming that both your (unsorted) input and (sorted) output files are too large to fit into memory, you need a way to sort the contents of these files in chunks. The keyword you are looking for is External Sort.

Here is a good example of that on CodeProject (with source code and explanation): Sorting Huge Text Files

A somewhat similar StackOverflow question you might want to look into: Reading large text files with streams in C#

If you need any help with your actual implementation, please provide additional information on what your input and (desired) output looks like. The files themselves are obviously too big to upload - a screenshot of the directory with your input and output files would also work. Then I (and others) can see how large each file is and to what degree (if at all) they need to be aggregated.

Community
  • 1
  • 1
Fred Kleuver
  • 7,797
  • 2
  • 27
  • 38