如何在C#中将数据读入列表结构?
我正在编写一个使用StudentStruct(structure)的程序,该程序包括:studentID、studentName和grades(这是一个单独的列表)。该程序应该读入一个连续的文本文件,并包含允许用户执行以下操作的方法:添加学生、添加分数(对特定学生)、更改学生姓名和删除学生。在程序结束时,应使用当前程序会话中所做的新更改覆盖上一个文件 我的问题是如何将文本文件中的数据读入列表中单独的结构变量 我的文本文件如下所示:如何在C#中将数据读入列表结构?,c#,list,structure,C#,List,Structure,我正在编写一个使用StudentStruct(structure)的程序,该程序包括:studentID、studentName和grades(这是一个单独的列表)。该程序应该读入一个连续的文本文件,并包含允许用户执行以下操作的方法:添加学生、添加分数(对特定学生)、更改学生姓名和删除学生。在程序结束时,应使用当前程序会话中所做的新更改覆盖上一个文件 我的问题是如何将文本文件中的数据读入列表中单独的结构变量 我的文本文件如下所示: 00000,Mimzi Dagger,100,50,75,70,
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>();
}
类程序
{
静态字符串pathSource=@“C:\schoolfiles\StudentGrades.txt”;
学生结构
{
公共字符串学生ID;
公共字符串studentName;
公开名单职系;
}
静态void Main(字符串[]参数)
{
StudentStruct student=新StudentStruct();
List myList=新列表();
student.grades=新列表();
}
更新:以下是我到目前为止的想法:
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);
for(int i=0;i
更新:上面的代码运行得非常好。下面是我代码中涉及这个问题的最上面部分:
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 Main(字符串[]args)
{
//声明变量
字符串输出;
//将文件读入数组
string[]fileLines=File.ReadAllLines(路径源);
StudentStruct student=新StudentStruct();
List myList=新列表();
for(int i=0;i
然后,要将列表添加回文件,我执行了以下操作:
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);
静态void ExitModule(列表myList)
{
//声明变量
字符串inputChoice=null;
字符串输出=null;
System.IO.StreamWriter file=new System.IO.StreamWriter(路径源);
Console.Clear();
Console.WriteLine(“您确定要退出程序吗?是/否”);
inputChoice=Console.ReadLine();
if(inputChoice==“Y”| | inputChoice==“Y”)
{
for(int i=0;i
试试这个代码。它对我有用
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>();
// get all lines from text file
string[] allLinesFromFile = File.ReadAllLines(pathSource);
// iterate through each line and process it
foreach(string line in allLinesFromFile)
{
// split each line on ','
string[] sections = line.Split(',');
student.studentID = sections[0];
student.studentName = sections[1];
// use this loop to add numbers to list of grades
for(int i =2; i < sections.Length; i++)
{
student.grades.Add(sections[i]);
}
// add this student to list
myList.Add(student);
// create new object of student
student = new StudentStruct();
student.grades = new List<string>();
}
}
类程序
{
静态字符串pathSource=@“C:\schoolfiles\StudentGrades.txt”;
学生结构
{
公共字符串学生ID;
公共字符串studentName;
公开名单职系;
}
静态void Main(字符串[]参数)
{
StudentStruct student=新StudentStruct();
List myList=新列表();
student.grades=新列表();
//从文本文件中获取所有行
字符串[]allLinesFromFile=File.ReadAllLines(路径源);
//迭代每一行并处理它
foreach(allLinesFromFile中的字符串行)
{
//在“,”上拆分每一行
string[]sections=line.Split(',');
student.studentID=节[0];
student.studentName=节[1];
//使用此循环可将数字添加到等级列表中
对于(int i=2;i
由于这显然是一项家庭作业,因此现在可能是您学习的好时机。这将帮助您高效地存储从文本文件中读入的信息,允许您在修改记录后更轻松地将其写出
以下是几个其他随机指针:
方法将是开始读取文本文件每一行的好地方System.IO.File.ReadLines(pathSource)
- 除非您精通其语义和用途,否则几乎不应该在C#中使用
结构(尤其是像您的示例中那样可变的结构)。请改用
类
- 一些事情
(1) 我不知道你的老师/教授为什么要你在这里使用结构。一个类更合适<
Program {
private const string PathSource = @"C:\schoolfiles\StudentGrades.txt";
private struct StudentStruct
{
public string StudentID;
public string StudentName;
private List<string> _gradeList;
public List<string> GradeList
{
// if _gradeList is null create a new instance
get { return _gradeList ?? (_gradeList = new List<string>()); }
private set { _gradeList = value; }
}
}
private static void Main( string[] args )
{
var studentFile = File.ReadAllLines( PathSource );
// If you haven't learned about delegates or extensions methods...
// You can just change it to a foreach statement
var myList = studentFile.Select( GetStudent ).ToList();
// make sure we have everything
foreach (var studentStruct in myList)
{
Console.WriteLine( "{0} {1}", studentStruct.studentName, studentStruct.studentID );
foreach (var grade in studentStruct.GradeList) { Console.Write( grade + " " ); }
Console.WriteLine();
}
}
private static StudentStruct GetStudent( string line )
{
var student = new StudentStruct();
var studentDelimited = line.Split( ',' );
student.studentID = studentDelimited[ 0 ];
student.studentName = studentDelimited[ 1 ];
for ( int i = 2; i < studentDelimited.Length; i++ ) { student.GradeList.Add( studentDelimited[ i ] ); }
return student;
}
}