简体   繁体   English

如何从文本文件中获取数据并将其拆分为数组?

[英]How do I take data from a text file and split it into Arrays?

I have a project where I need to split data from a text file into an array or an "array of structure" as my professor said, and I'm pretty confused as to how to go about doing it. 正如我的教授所说,我有一个项目需要将数据从文本文件拆分为数组或“结构数组”,而我对如何执行此操作感到非常困惑。

Basically I have data arranged like this in a text file (doesn't have to be like this.. can be one thing per line): 基本上,我在文本文件中安排了这样的数据(不必这样。每行可以是一件事):

PR214;MR43T;RBL8;14K22
PR223;R43;RJ6;14K24
PR224;R43N;RN4;14K30
PR246;R46N;RN8;14K32
PR247;R46TS;RBL17Y;14K33
PR248;R46TX;RBL12-6;14K35
PR324;S46;J11;14K38
PR326;SR46E;XEJ8;14K40
PR444;47L;H12;14K44

The numbers in the first column (PR...) represent a CCC number. 第一列(PR ...)中的数字表示CCC编号。 The 2nd, 3rd, and 4th columns represent part numbers for three different companies (column 2 is one company, column 3 another, etc). 第二,第三和第四列代表三个不同公司的零件号(第2列是一个公司,第3列是另一个公司,依此类推)。 The user will tell me the company, and the part number under the company by selecting a radio button and then typing in the part (ie company 3, and "RJ6") and then I'm supposed to give the customer the CCC number ("PR223"). 用户将通过选择单选按钮然后输入零件(即公司3和“ RJ6”)来告诉我公司和公司下的零件号,然后我应该给客户提供CCC编号( “ PR223”)。

I'm not asking you guys to do my homework for me but I have a hard time grasping arrays. 我不是要你们为我做功课,但我很难掌握数组。 Could you point me in the right as to how to go about doing this? 您能指出我正确的方法吗?

The other answers are correct. 其他答案是正确的。 You read in each line from the text file and insert the values into a structure defined by your data. 您从文本文件中读取每一行,并将值插入到数据定义的结构中。 For each new line you need to use an instance of the structure, so you do that be making an array of structures which just a group of structures. 对于每一行,您都需要使用该结构的一个实例,因此您需要制作一个结构数组,而该数组只是一组结构。

If you are struggling understanding arrays, you really need to work on that. 如果您在努力理解数组,那么您确实需要做些工作。 Think of arrays like group of boxes. 将数组想像成一组盒子。 Let's start with 3 boxes in row. 让我们从连续3个框开始。 Each is the same size and can only hold what fits inside them. 每个都是相同的大小,只能容纳适合它们内部的内容。 You can assigned type of item (data) to the boxes but all the boxes can only hold that type of item. 您可以将项目(数据)的类型分配给框,但是所有框只能容纳该类型的项目。 The boxes are numbered from 0 to 2. In some languages, you can name the boxes (an associative array) but not in C# AFAIK. 框的编号从0到2。在某些语言中,可以命名框(关联数组),但不能使用C#AFAIK命名。 So that gives you a one dimensional array. 这样就可以得到一维数组。

For two dimensonal array think of a Chess board. 对于两个二维阵列,请考虑一个国际象棋棋盘。 Do you know how to record a chess match? 你知道如何下棋吗? Chess notation uses A to H for the horizontal position and 1 to 8 for the vertical position. 国际象棋符号使用A到H表示水平位置,使用1到8表示垂直位置。 So the white queen starts in position D1. 因此,白皇后从位置D1开始。 Now if I was programming a chess game I would use and 8x8 array just like the chess board. 现在,如果我正在编写象棋游戏,我将使用象棋盘一样的8x8阵列。 Of course the numbering on my array will be 0-7 across the top and 0-7 up the side. 当然,阵列上的编号从顶部开始是0-7,从侧面开始是0-7。 The Position of the white queen in my array would then be [3][0]. 这样,我数组中白皇后的位置将为[3] [0]。 So placing data in a two dimensional array is like placing piece on a chess board. 因此,将数据放置在二维数组中就像将棋子放在棋盘上一样。 The game battleship is another example of a 2 dimensional array. 游戏战舰是二维阵列的另一个示例。

Adding dimensions to your array is like adding a new coordinate to your graph. 向数组添加尺寸就像向图形添加新坐标。 You start with X , then you add Y and they add Z . 您从X开始,然后添加Y ,然后又添加Z。 So on and so forth. 等等等等。

You can use the ReadLine method of a StreamReader to read your file one line at a time. 您可以使用StreamReader的ReadLine方法一次读取一行文件。 Then you can use the String.Split(char[]) method to get a string array of the values in each line. 然后,您可以使用String.Split(char [])方法获取每行中值的字符串数组。 If you already know how to make structures you should be set. 如果您已经知道如何制作结构,则应该进行设置。

If you look at each line of data there's a common delimiter between the 'columns', so you want to get each line split into four separate entities. 如果查看数据的每一行,“列”之间都有一个公共的定界符,因此您希望将每一行分成四个单独的实体。 As you work line by line and get those four discrete elements should can assign each to the appropriate part of the structure. 当您逐行工作并获得这四个离散元素时,可以将每个元素分配给结构的适当部分。 You'll of course have one 'structure' for each line, so an array of structure here at the end. 当然,每一行都会有一个“结构”,因此最后是一个结构数组。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何从列表框中获取数据并将其拆分为 2 个不同的列表框? - How can i take data from Listbox and split into 2 different listboxes? 将文本文件中的字符串拆分为字符串数组 - Split strings from text file into string arrays 如何从记事本文本文件中的值取到datagrid标头? 我只想获取标头而不是数据 - How Can I Take Values from notepad text file to datagrid header ? i only want to take headers not data 如何从文本框中获取文本并将其转换为C#中可用的文件路径? - How do I take the text from a textbox and make it into a usable file path in C#? 如何从.json文件中获取数据,仅使用其中的某些项,然后使用该数据填充组合框? - How do I take the data from a .json file, use only some of the items in it, and populate a combobox with that data? 如何有效地将值从文本文件拆分到程序中? - How do I efficiently split the values from a text file into my program? 如何使用 3 个空行作为分隔符拆分文本文件? - How do I split text file using 3 empty lines as a delimiter? 如何将文本文件中的“拆分文本”字符串输入文本框并更改单选按钮的文本? - How do I string Split text in a text file into a text box and change the text of the radio button? 将数据从文本文件拆分为并行数组 - Splitting data from a text file into parallel arrays 如何从Unity的文本字段中获取文本信息并将其分配给C#中的变量? - How do I take text info from a text field from Unity and assign it to a variable in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM