Análisis de datos int de un archivo .txt para cálculos
Frecuentes
Visto 724 veces
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.
Esto es lo que tengo hasta ahora.
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());
}
}
}
2 Respuestas
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.
respondido 27 nov., 13:07
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
var is easier to type on my tablet, when I can't verify the syntax in VS. filename is string as above... - Nickolai Nielsen
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();
}
...
respondido 27 nov., 13:09
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas c# arrays or haz tu propia pregunta.
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
stackoverflow.com/questions/16136383/… - Monika
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
Si sabes qué you have 20 integers, then why do you only try to read 11 of them (
for (int i = 0; i < 11; i++)
)? - Corakcan you provide sample your txt file format? - Grundy