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());
}
}
}