简体   繁体   English

从文件读取行数并在c ++函数中使用它

[英]Reading number of lines from file and using it in functions in c++

My problem may be trivial, but I don't know how to deal with it. 我的问题可能微不足道,但是我不知道如何处理。 I have a program that reads matrix from file and then collects it from file, displays it and performs a numeric method on it. 我有一个程序,该程序从文件中读取矩阵,然后从文件中收集矩阵,将其显示并对其执行数字方法。 When I insert number of unknowns(in this case it equals the number of lines in file) by cin I get everything right. 当我通过cin插入未知数(在这种情况下,它等于文件中的行数)时,一切正常。 But when I try to read number of lines from file and then read matrix, display it and perform method - although I get the number of lines correctly, the program reads matrix as if it consists of 0. Here is the code of counting lines: 但是,当我尝试从文件中读取行数,然后读取矩阵,进行显示并执行方法时-尽管我正确地获得了行数,但程序读取的矩阵好像是由0组成的。这是计数行的代码:

int countLines(ifstream &file){
string line;
int l = 0;
do{
getline(file, line);
    l++;
}while(!file.eof());
return l;}

And here I try to use it: 在这里,我尝试使用它:

string nameoffile = "";
 nameoffile = "Matrix1.txt";
  ifstream file;
    file.open(nameoffile.c_str());
    if (file.good()==true)
    {
        cout <<"The file is available<<endl;
        n = countLines(file);
        cout << n << endl;
        collectMatrix(file,n);
    }
    else
    {
        return 0;
    }
    displayMatrix(n);

For example, collectMatrix looks like that: 例如,collectMatrix如下所示:

void collectMatrix(ifstream &file, int n){
for(int i = 0; i <n; i++)
{
    for(int j = 0; j <n; j++)
    {
        file>>A[i][j]; //matrix
    }
}      
for(int k=0; k<n; k++ )
{
    file>>b[k]; //vector of results
}            }

And all of it worked as long as I had cin>>n in the code instead of trying to read it from file. 只要我在代码中输入了cin >> n,所有这些方法就可以正常工作,而不是尝试从文件中读取它。 And to be honest, I have to read it from file but I'm not so good in programming, so I'd be thankful for any hints and help. 老实说,我必须从文件中读取它,但是我在编程方面不太出色,因此感谢任何提示和帮助。

ifstream internally keeps track of how far into the file it has read. ifstream在内部跟踪它已读入文件的距离。 When your countLines method returns, the ifstream has read all the way to the end of the file, and it still holds the end of the file as its current position in the file when you pass it to collectMatrix. 当countLines方法返回时,ifstream一直读取到文件末尾,当您将其传递给collectMatrix时,ifstream仍将文件末尾作为其在文件中的当前位置。 When you try to fill in the matrix, there is nothing left to read from the file, so the matrix values are not set. 当您尝试填写矩阵时,没有什么要读取的文件,因此未设置矩阵值。

You have a couple of options to fix this. 您有几种选择可以解决此问题。 The first is to calculate the size of the matrix as you are filling in the values. 第一种是在填写值时计算矩阵的大小。 Your code seems to indicate that the matrix is square; 您的代码似乎表明矩阵是正方形; if so, you can determine the size of the matrix simply by looking at the first line. 如果是这样,您只需查看第一行即可确定矩阵的大小。 The other, simpler, but much less efficient approach would be to just create a second ifstream with the same file and pass that to collectMatrix. 另一种更简单但效率更低的方法是仅创建具有相同文件的第二个ifstream并将其传递给collectMatrix。 This approach would be fairly slow, as you pass through the file twice. 当您两次浏览文件时,这种方法将相当慢。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM