0

This is homework. Currently working with binary file access; I'm trying to read data of type int from a text file. I need to calculate the mean, high/low value, and # of values in the data. I have a method for retrieving and displaying the data from the file, but I don't know how to store/use the values in the file for calculation. The data file has 20 values of type int.

Here's what I've got so far.

    static void Main(string[] args)
    {
        /* Initializing FileStream and BinaryReader 
         * for file access and reading int data from file
         */
        FileStream filStream;
        BinaryReader binReader;

        //Instructions to user to open specific data file
        Console.WriteLine("Enter IntData.txt for name of file: ");
        string fileName = Console.ReadLine();
        try
        {
            filStream = new FileStream(fileName, FileMode.Open,
                                        FileAccess.Read);
            binReader = new BinaryReader(filStream);

            RetrieveAndDisplayData(binReader);

             //Declared array for possible calculations
            int[] numbers = new int[20]; 
            for (int i = 0; i < numbers.Length; i++) 
            {
                //Numbers from file go here
                //How to fill array with data values from file?
            }

            binReader.Close();
            filStream.Close();
        }
            //Exception Handling
        catch (FileNotFoundException exc)
        {
            Console.WriteLine(exc.Message);
        }
        catch (InvalidDataException exc)
        {
            Console.WriteLine(exc.Message);
        }
        catch (EndOfStreamException exc)
        {
            Console.WriteLine(exc.Message);
        }
        catch (IOException exc)
        {
            Console.WriteLine(exc.Message);
        }
        Console.ReadKey();
    }

    public static void RetrieveAndDisplayData(BinaryReader binReader)
    {
        // Read string data from the file
        Console.WriteLine(binReader.ReadString());

        // Read integer data from the file
        for (int i = 0; i < 11; i++)
        {
            Console.WriteLine(binReader.ReadInt32());
        }

        // Read decimal data from the file
        Console.WriteLine(binReader.ReadDecimal());
    }
}

}

azlnick
  • 11
  • 4
  • 3
    It's not clear what you don't know at this point. Do you know how to create an array? Do you definitely need a 2-dimensional array? Do you know how many values you'll need to read? – Jon Skeet Nov 27 '13 at 07:09
  • http://stackoverflow.com/questions/16136383/reading-a-text-file-using-openfiledialog-in-windows-forms – Monika Nov 27 '13 at 07:10
  • I have 20 values to work with. Would using something like this work? int[] numbers; numbers = new int[20]; then using the array.getlength for the # of values? – azlnick Nov 27 '13 at 07:13
  • I understand arrays, but for this specific example, I'm not sure how to instantiate an array using the values in the .txt file. – azlnick Nov 27 '13 at 07:18
  • 1
    If you *know* you have 20 integers, then why do you only try to read 11 of them (`for (int i = 0; i < 11; i++)`)? – Corak Nov 27 '13 at 07:24
  • 1
    can you provide sample your txt file format? – Grundy Nov 27 '13 at 07:26
  • I fixed the for loop to i < 20. @Corak – azlnick Nov 27 '13 at 07:40
  • @Tobberoth That would require me to save the .txt file into a .bin file, correct? – azlnick Nov 27 '13 at 07:41
  • @Grundy No, I don't think that I can. – azlnick Nov 27 '13 at 07:41
  • @azlnick - "Would using something like this work?" - well, did you try it? `int[] numbers = new int[20]; for (int i = 0; i < numbers.Length; i++) { // do arcane voodoo to fill numbers array }` – Corak Nov 27 '13 at 07:45
  • @corak Sorry, it's 1am. Thanks for letting me know to use the length property. – azlnick Nov 27 '13 at 07:47
  • @azlnick Nevermind. Yes, using `int[] numbers` will be good enough. `List numbers` is easier to work with though since you don't have to worry about arraysize etc with it. – Tobberoth Nov 27 '13 at 07:48
  • Well, I've declared an array `int[] numbers = new int[20]; for (int i = 0; i < numbers.Length; i++) { //Numbers from file go here }` but I'm still unclear on how to perform the main calculations that I need. – azlnick Nov 27 '13 at 08:04
  • @azlnick - okay, but those are different questions now. You might want to ask them seperately, but for the sake of learning (which is the *purpose* of homework), you might be better off, figuring that out on your own. A general hint that works surprisingly good on a lot of programming questions: how would you do it manually? How do you know that the average of 3, 4 and 8 is 5? How do you know what the highest or lowest number is (when you can't immediately see it)? Imagine a big book with one number on each page (unsorted). What would you do to determine the highest/lowest number? – Corak Nov 27 '13 at 09:39

2 Answers2

0
string filename = "....";
var content = System.IO.File.ReadAllText(filename);
var arr = content.Split("\n");
var intArr = arr.Select(x=> int.Parse(x)).ToArray(); // will return arr as int

Given a text file input like

1
2
3

*code not verified, I will do later if it won't work.

Nickolai Nielsen
  • 932
  • 6
  • 19
  • 1
    Sort of off-topic, but this is one of those situations where I feel the `var` keyword is detrimental. To a beginner like the OP, this code is way more unclear than if types were written properly. Why is filename a string, but content a var? – Tobberoth Nov 27 '13 at 07:51
  • var is easier to type on my tablet, when I can't verify the syntax in VS. filename is string as above... – Nickolai Nielsen Nov 27 '13 at 07:56
0

for store data from file to array try change your code like this

...
filStream = new FileStream(fileName, FileMode.Open,
                                    FileAccess.Read);
binReader = new BinaryReader(filStream);

//Declared array for possible calculations
int[] numbers = new int[20]; 
for (int i = 0; i < numbers.Length; i++) 
{
    numbers[i] = binReader.ReadInt32();
}
...
Grundy
  • 13,356
  • 3
  • 35
  • 55