从文件读取数据,文件名作为输入

时间:2022-08-03 19:51:37

I am writing a program which reads data from different files, which are given as input strings, and stores them into a vector of vectors. The problem I am not able to debug the loop which reads different files. I have closed the ifstream object, cleared the string using empty function... but still it just terminates when i give second file name as input.

我正在编写一个程序,它从不同的文件中读取数据,这些文件作为输入字符串给出,并将它们存储到向量矢量中。问题我无法调试读取不同文件的循环。我已经关闭了ifstream对象,使用空函数清除了字符串...但是当我将第二个文件名作为输入时它仍然会终止。

I am copying the code for your perusal. It is a function called by another another function. Transposectr transposes a matrix.

我正在复制代码供你细读。它是另一个函数调用的函数。 Transposectr转置矩阵。

code:

vector<vector<float> > store1,store2;
ifstream bb;

string my_string;

float carrier;
vector<float> buffer;

cout<<"enter the file name"<<endl;
getline(cin,my_string);

while (my_string!="end")
{

    bb.open(my_string.c_str());
    while (!bb.eof())
    {
        bb >> carrier;

        if (bb.peek() == '\n' || bb.eof() )
        {
            buffer.push_back(carrier);
            store1.push_back(buffer);
            buffer.clear();
        }

        else
        {
            buffer.push_back(carrier);
        }


    }

    bb.close();
    buffer.clear();
    transposectr1(store1);
    storex.push_back(store1[1]);
    storey.push_back(store1[0]);
    store1.clear();
    my_string.empty();
    cout<<"done reading the file"<<endl;
    cout<<"enter the file name"<<endl;
    getline(cin,my_string);
}

3 个解决方案

#1


I'm really not clear what you are trying to do. But I have one golden ruile when it comes to using istreams:

我真的不清楚你想做什么。但是在使用istreams时我有一个金色的ruile:

Never use the eof() function!

切勿使用eof()函数!

It almost certainly does not do what you think it does. Instead you should test if a read operation succeeded.

它几乎肯定不会做你认为它做的事情。相反,您应该测试读取操作是否成功。

int x;

while( in >> x ) {
   // I read something successfully
}

You might also want to avoid peek() too. Try re-writing your code with this advice in mind.

您可能也想避免使用peek()。尝试使用此建议重新编写代码。

#2


Add

bb.clear();

after the bb.close() you may get the right thing. bb.close() doesn't reset the cursor I think.

在bb.close()之后,你可能会得到正确的结果。我认为bb.close()不会重置光标。

#3


Neil Butterworth is right

尼尔巴特沃思是对的

Never use the eof() function!

切勿使用eof()函数!

This link explains why.

此链接解释了原因。

#1


I'm really not clear what you are trying to do. But I have one golden ruile when it comes to using istreams:

我真的不清楚你想做什么。但是在使用istreams时我有一个金色的ruile:

Never use the eof() function!

切勿使用eof()函数!

It almost certainly does not do what you think it does. Instead you should test if a read operation succeeded.

它几乎肯定不会做你认为它做的事情。相反,您应该测试读取操作是否成功。

int x;

while( in >> x ) {
   // I read something successfully
}

You might also want to avoid peek() too. Try re-writing your code with this advice in mind.

您可能也想避免使用peek()。尝试使用此建议重新编写代码。

#2


Add

bb.clear();

after the bb.close() you may get the right thing. bb.close() doesn't reset the cursor I think.

在bb.close()之后,你可能会得到正确的结果。我认为bb.close()不会重置光标。

#3


Neil Butterworth is right

尼尔巴特沃思是对的

Never use the eof() function!

切勿使用eof()函数!

This link explains why.

此链接解释了原因。