studentID, studentName 및 성적 (별도의 목록)으로 구성된 StudentStruct (구조)를 사용하는 프로그램을 작성 중입니다. 이 프로그램은 순차적 텍스트 파일을 읽어야하며 사용자가 학생 추가, 특정 학생에게 성적 추가, 학생 이름 변경, 학생 삭제 등의 작업을 수행 할 수있는 방법을 포함합니다. 프로그램이 끝나면 이전 파일을 현재 프로그램 세션의 새로운 변경 사항으로 덮어 씁니다.C#의 목록 구조에 데이터를 읽는 방법?
제 질문은 텍스트 파일의 데이터를 목록 내의 개별 구조 변수로 어떻게 읽습니까?
내 텍스트 파일은 다음과 같다 :
목록 내에서 구조 변수가 작성되면00000,Mimzi Dagger,100,50,75,70,45,10,98,83
00001,Alexander Druaga,89,45,80,90,15,73,99,100,61
00002,Nicholas Zarcoffsky,100,50,80,50,75,100,100
00003,Kantmiss Evershot,50,100
, 어떻게 위의 파일과 같은 형식으로 목록 구조의 내용으로 파일을 덮어합니까?
등급이 다양하고 다양하기 때문에 각 등급을 반복하여 성적 목록에 추가하는 방법은 무엇입니까?
어쩌면 이미 말할 수 있듯이 저는 C#을 처음 접했고 이것이 제 첫 프로젝트입니다. 어떤 도움이라도 대단히 감사하겠습니다!
class Program
{
static string pathSource = @"C:\schoolfiles\StudentGrades.txt";
struct StudentStruct
{
public string studentID;
public string studentName;
public List<string> grades;
}
static void Main(string[] args)
{
StudentStruct student = new StudentStruct();
List<StudentStruct> myList = new List<StudentStruct>();
student.grades = new List<string>();
}
업데이트 : 여기에 내가 지금까지 함께 온 것입니다 :
for (int i = 0; i < fileLines.Length; i++)
{
output = fileLines[i];
string[] outputArray = output.Split(',');
student.grades = new List<string>();
student.studentID = outputArray[0];
student.studentName = outputArray[1];
for (int j = 2; j < outputArray.Length; j++)
{
student.grades.Add(outputArray[j]);
}
myList.Add(student);
UPDATE : 위의 코드는 멋지고 밖으로 일했다. 다음은이 질문에 관련된 내 코드의 상단 부분입니다 :
static void Main(string[] args)
{
//Declare variables
string output;
//Read the File into an array
string[] fileLines = File.ReadAllLines(PathSource);
StudentStruct student = new StudentStruct();
List<StudentStruct> myList = new List<StudentStruct>();
for (int i = 0; i < fileLines.Length; i++)
{
output = fileLines[i];
string[] outputArray = output.Split(',');
student.grades = new List<string>();
student.studentID = outputArray[0];
student.studentName = outputArray[1];
for (int j = 2; j < outputArray.Length; j++)
{
student.grades.Add(outputArray[j]);
}
myList.Add(student);
}
MainMenu(myList);
}
그리고 내가 이런 짓을 파일로 다시 목록을 추가 :
static void ExitModule(List<StudentStruct> myList)
{
//Declare variables
string inputChoice = null;
string output = null;
System.IO.StreamWriter file = new System.IO.StreamWriter(PathSource);
Console.Clear();
Console.WriteLine("Are You Sure You Want To Exit The Program? Y/N");
inputChoice = Console.ReadLine();
if (inputChoice == "Y" || inputChoice == "y")
{
for (int i = 0; i < myList.Count; i++)
{
output = (myList[i].studentID + "," + myList[i].studentName);
for (int j = 0; j < myList[i].grades.Count; j++)
{
output += ("," + myList[i].grades[j]);
}
file.WriteLine(output);
}
file.Close();
Environment.Exit(0);
중첩 된 목록으로 목록을 전달하는 방법에 대한 구문 – AsfK
(새 학생 구조 매번 사용) 목록에 노드를 추가 한 후 (쉼표 따라) 및 라인을 구문 분석 할 수 – Wallatrixx
주석에 코드를 게시하지 마십시오. 질문을 편집하고 코드를 입력하십시오. –