C
C#10mo ago
js

extracting data from a text file into an array

im given multiple text files containing student marks and info etc and I need to access these files and extract each type of data such as exam marks student name etc into accessible arrays so I can do calculations on this. So far I have only managed to create a menu to select a file but I dont know how to extract the information from the text files
4 Replies
js
jsOP10mo ago
using System;
using System.Collections.Generic;
using System.Linq;

Console.WriteLine("Please select a file to load.");

string[] filePaths = Directory.GetFiles(@"C:\Users\Joe\Documents\Uni\Programming Portfolio\Summative4\Summative4", "*.mark");

int count1 = 1;

foreach (string file in filePaths)
{
Console.WriteLine($"{count1}. {Path.GetFileName(file)}");
count1 = count1 + 1;
}
int selectedNum = int.Parse(Console.ReadLine());

string[] selectedFile = File.ReadLines(filePaths[selectedNum - 1]).ToArray();

StreamReader reader = new StreamReader(filePaths[selectedNum - 1]);

int numberOfLines = 0;
while (!reader.EndOfStream)
{
reader.ReadLine();
numberOfLines++;
}

reader.BaseStream.Seek(0, SeekOrigin.Begin);

ScoreDB[] records = new ScoreDB[numberOfLines];
struct ScoreDB
{
public int studentID;
public string lastName;
public int firstName;
public int challengeMarks;
public int examMarks;
public int cpMarks;
}
using System;
using System.Collections.Generic;
using System.Linq;

Console.WriteLine("Please select a file to load.");

string[] filePaths = Directory.GetFiles(@"C:\Users\Joe\Documents\Uni\Programming Portfolio\Summative4\Summative4", "*.mark");

int count1 = 1;

foreach (string file in filePaths)
{
Console.WriteLine($"{count1}. {Path.GetFileName(file)}");
count1 = count1 + 1;
}
int selectedNum = int.Parse(Console.ReadLine());

string[] selectedFile = File.ReadLines(filePaths[selectedNum - 1]).ToArray();

StreamReader reader = new StreamReader(filePaths[selectedNum - 1]);

int numberOfLines = 0;
while (!reader.EndOfStream)
{
reader.ReadLine();
numberOfLines++;
}

reader.BaseStream.Seek(0, SeekOrigin.Begin);

ScoreDB[] records = new ScoreDB[numberOfLines];
struct ScoreDB
{
public int studentID;
public string lastName;
public int firstName;
public int challengeMarks;
public int examMarks;
public int cpMarks;
}
this is what I have so far the text files are in this format
Student:[ID:202077805,LastName:White,FirstName:Mathew],Marks:[Challenges:[3,2,0,1,2,0,1,1,2],Exam:2,Capstone:33]
Student:[ID:202077805,LastName:White,FirstName:Mathew],Marks:[Challenges:[3,2,0,1,2,0,1,1,2],Exam:2,Capstone:33]
i like chatgpt
i like chatgpt10mo ago
Use regex to extract the data. See my temporary repo here https://github.com/suugbut/SuperTrashBin/blob/main/extractor_with_regex.cs @js
Angius
Angius10mo ago
If the structure is consistent, write a simple parser
Pobiega
Pobiega10mo ago
the simplest kind of parser just uses string splitting and indexof, more or less
Want results from more Discord servers?
Add your server