将文本文件读入二维数组问题 [英] reading a text file into a 2d array problem

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

问题描述

您好。

我正在尝试阅读包含
的文本文件

1

2

3


看起来就像那样。我能够读取它并将每个数字分配给矩阵元素(或数组元素)。它读得很好,我试图将元素更改为int变量a,b,c,以便我可以说矩阵[i] [j] = {a,b,c};


在调整过程中的某些时刻,所有的矩阵元素都变成了0,我迷失了自己。

请看我的代码。

我意识到这不是编写代码的最佳编程方式,但我是科学家,我宁愿按照我的方式去做。如果它太烦人了,那么请提出别的建议。



我的代码如下。

Hello.
I am trying to read a text file that contains

1
2
3

it just looks like that. I was able to read it and assign each number to a matrix element (or array element). It was reading it all fine and I was trying to change the elements to int variables a,b,c so that I could say matrix[i][j] = {a,b,c};

At some point during tweaking all the matrix elements became 0 and I have lost myself.
Look at my code please.
I realise this is not the best programming way of writing code but I''m a scientist and I''d rather do it the way I have. If its too annoying to read then please suggest something else.



My code below.

展开 | 选择 | Wrap | 行号

推荐答案

好吧,你永远不会在你的程序中初始化a,b和c(这本身并不是一个好主意),但是输入矩阵[0] [0],矩阵[0] [1]和矩阵[0] [2]后,分别将这三个槽中的每一个分配给a,b和c。这不仅会覆盖您从文件中提取的任何信息,而且还会使用垃圾值填充数组。


您到底想要做什么?我想这里有一个比这更简单,更清洁的选择。
Well, you never initialize a, b, and c in your program (which in itself is not a good idea), but then, after you input into matrix[0][0], matrix[0][1], and matrix[0][2], you assign each of these three slots to a, b, and c, respectively. Not only does this override whatever information you pulled from the file, but it fills the array with garbage values.

What exactly are you trying to do? I imagine there''s a much simpler, cleaner option than all this.


我认为你这太难了。


首先,那里在C ++中真的不是2D数组。只有一维数组。那就是:
I think you are making this too hard.

First, there really aren''t 2D arrays in C++. There are only one-dimensional arrays. That is:
展开 | 选择 | Wrap | 行号


好吧,你永远不会在程序中初始化a,b和c(这本身并不是一个好主意),但是,在输入矩阵[0] [0],矩阵[0] [1]和矩阵[0] [2]之后,将这三个槽中的每一个分配给a,b和c,分别。这不仅会覆盖您从文件中提取的任何信息,而且还会使用垃圾值填充数组。

Well, you never initialize a, b, and c in your program (which in itself is not a good idea), but then, after you input into matrix[0][0], matrix[0][1], and matrix[0][2], you assign each of these three slots to a, b, and c, respectively. Not only does this override whatever information you pulled from the file, but it fills the array with garbage values.


您到底想要做什么?我想这里有一个比这更简单,更清洁的选择。
What exactly are you trying to do? I imagine there''s a much simpler, cleaner option than all this.



我正在尝试从文本文件中读取值并创建一个矩阵。由于c ++不理解矩阵是什么而没有矩阵头文件。我正在使用数组。我希望能够调用matrix [i] [j],输出应该是1,2,3,它是从文本文件中读取的。

这个矩阵将用于矩阵方程代数。矩阵代表一个3d点,我正在尝试平移和旋转这个3d点以匹配另一个3d点。我有所有的数学方程只是试图用c ++来实现它们,这就是困难。我在家里,不能确切地告诉你在矩阵上使用什么数学。这是否回答了你的问题?



[quote = Ganon11]输入矩阵[0] [0],矩阵[0] [1]和矩阵[ 0] [2],你将这三个插槽中的每一个分别分配给a,b和c


我试图将文本文件值读入矩阵的每个元素,当这没有工作时,我认为这是由于字符串到int问题。我认为通过将矩阵[0] [0]分配给int,它会将字符串更改为整数。基本上我不确定从文本文件中读取是否给了我一个字符串,或者因为文件只包含数字,如果它是一个int。我现在看到我所做的是有问题但我不知道怎么做我想要的。


如何初始化a,b,c?我说他们在代码的开头是int,但你的意思是初始化一个值?我已经看到人们在使用之前做了a = b = c = 0,但这样做不会使它们为0而我希望它们是文件中的值。


I''m trying to read values from a text file and make a "matrix". As c++ doesnt understand what a matrix is without a "matrix header file" i''m using arrays. I wanted to be able to call matrix[i][j] and the output should be 1,2,3 which was read from the text file.
This matrix will be used in matrix equation algebra. The matrix represents a 3d point and I''m trying to translate and rotate this 3d point to match up with another 3d point. I have all the maths equations its just trying to implement them in c++ that''s the dificulty. I''m at home and can''t tell you exactly what maths will be used on the matrix. Does that answer your question?


[quote=Ganon11] after you input into matrix[0][0], matrix[0][1], and matrix[0][2], you assign each of these three slots to a, b, and c, respectively

I was trying to read the text file values into each element of the matrix, when this didnt work I thought it was due a string to int problem. I thought that by assigning matrix[0][0] to an int a it would change the string to an integer. Basically I''m unsure if reading from the text file gives me a string or because the file only contains number if it will be an int. I see now what I did is problematic but I don''t see how to do what I want.

also how do initialize a,b,c? I said they were int at the beginning of the code but you mean initialize a value? I have seen people do a=b=c=0 before using them but does that then not make them 0 and I wanted them to be values from the file.


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

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