如何将文本文件读入矩阵 [英] How to read text file into matrix

查看:113
本文介绍了如何将文本文件读入矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假定文本文件包含以下数据:
A; B; 2
A; C; 4
B; C; 5

我想将其转换为矩阵[i] [j]形式的多维矩阵.我正在考虑将指针数组分配给数组.
现在的问题是如何将文本文件中的表单转换为matrix [i] [j].
行和列将根据数据大小而变化.

我正在寻找的输出是这样的:

Assume the text file contains data as below:
A;B;2
A;C;4
B;C;5

I would like to convert it to multi-dimensional matrix in the form of matrix[i][j].I''m thinking of allocate array of pointers to arrays.
The problem now is how to convert the form as in the text file into matrix[i][j].
The row and column will change according to the data size.

The output that I''m looking for is something like this:

         0  1  2 (columns)
      0  1  2  4
(rows)1  2  1  5
      2  4  5  1


注意:if(i = j)矩阵[i] [j] = 1
对称:matrix [i] [j] =矩阵[j] [i]


Note: if(i=j) matrix[i][j]=1
symmetrical: matrix[i][j] = matrix [j][i]

推荐答案

使用文件处理来逐字符获取字符,您可以将其存储在多维矩阵..


看看这个用于从文件中检索数据


http://www.mycplus.com/tutorials/c-programming-tutorials/file-handling / [^ ]


如果您要接收字符格式的数据,并且想要将其存储为整数,则必须键入强制转换,但是我想根据您上面显示的示例,您必须使用字符数组

谢谢
use file handling to fetch character by character data and you can store it in your multidimensional matrix..


have a look at this for retrieving data from file


http://www.mycplus.com/tutorials/c-programming-tutorials/file-handling/[^]


if you are reteiving data in character format and you want to store it in integer then you have to type cast it but i suppose according to your above shown example you must be using character array

Thanks


忘记使用文件输入并弄清楚将规则"转换为等效矩阵的算法.您可以通过将上述字符串添加到程序中并在开发算法时对其进行解析来模拟它.哪个值确定行或列的数量?每行或每列的值范围是多少?等等.
Forget about using file input and figure out the algorithm to convert your "rules" to their matrix equivalent. You could simulate it by adding the above strings into your program and parsing them while you develop your algorithm. What value(s) determines the number of rows or colums? What are the ranges of values in each row or column? etc.


这篇关于如何将文本文件读入矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆