C ++读取文件传输到2d阵列

时间:2016-04-07 01:37:50

标签: c++ arrays file-io

因为标题说我试图从文件中读取并将字符保存到带有空格的二维数组中,但它会卡在do while循环和文件的最后一个字符处。对于如何解决这个问题,有任何的建议吗?我想过使用eofbit,但我想不出来然后我试着通过||退出do while循环它与eof没有用。它似乎挂在文件的最后一个字母上。提前感谢您的建议

char ch[100][100];
int row_count =0;
int col_count = 0;
int col[100];
char temp; 
bool exit = false;
ifstream infile("data.txt");
if( infile.is_open())
{
    while(!infile.eof())
    {
        do
        {
            infile.get(temp);
            char[row_count][col_count] = temp;
            col_count++;

        }while(temp != '\n' || !infile.eof());
        col[row_count] = col_count;
        row_count++;
        col_count= 0;
    }
}

for(int i = 0; i <= 2; i++)
{
    for(int j=0; i <= col[i]; j++)
    {
        cout << ch[i][j];
    }
    cout << endl;
}

return 0;

}

3 个答案:

答案 0 :(得分:0)

循环是因为temp != '\n',在文件末尾这个条件总是为真,所以第二个条件永远不会被验证,因为你用a或者检查它。如果您想保留代码,请将!infile.eof()放在第一位。

但是如果你想要一种最好的方法和简单的方法来阅读文件。您应该使用getline

std::string line;
while(std::getline (infile,line))
{
    //do something for every char in the string...
}

答案 1 :(得分:0)

我没有编译这个,但这应该有效: 阅读评论并从中学习。

#include <iostream> // for cout
#include <fstream> // for infile     

int main()
{
    const int RowSize = 100, ColSize = 100;
    char ch[RowSize][ColSize];
    int row_count = 0;
    int col_count = 0;
    int col[ColSize];
    char temp;
    bool exit = false;

    std::ifstream infile;

    infile.open("data.txt"); // open the file

    if (!infile.is_open()) // check if opened succseffuly
    {
        std::cout << "File didn't load successfully!" << std::endl; // prompt the user
        getchar(); // wait for any input
        return 0; // terminate
    }

    while (infile.get(temp)) // while reading succesffuly
    {
        // if you are here, means you have valid data

        if (temp == '\n') // check if end of the line
        {
            col_count = 0; // reset cols
            row_count++; // increment rows
            ch[RowSize][ColSize] = NULL; // lock char array
            continue; // continue the loop iteration
        }
        else
        {
            ch[row_count][col_count] = temp; // store temp in your array
        }
        col_count++; // increment cols on every read
    }

    for (int i = 0; i <= 2; i++) // this will display junk if your line is less than 100 characters (check NULL if you don't want that)
    {
        for (int j = 0; i <= col[i]; j++)
        {
            std::cout << ch[i][j];
        }
        std::cout << std::endl;
    }

    return 0;
}

答案 2 :(得分:0)

iostream::eof只有在读完流结束后才会返回true。它并不表示下一次读取将是流的结束。参考this。因此,修改后的代码看起来像下面的代码片段。

char ch[100][100];
int row_count =0;
int col_count = 0;
int col[100];
char temp; 
bool exit = false;
ifstream infile("data.txt");
if( infile.is_open())
{
    while(!(infile>>temp).eof())
    {

        ch[row_count][col_count] = temp;
        col_count++;

        if(temp == '\n'){
            col[row_count] = col_count;
            row_count++;
            col_count= 0;
        }
    }
}

for(int i = 0; i <= 2; i++)
{
    for(int j=0; i <= col[i]; j++)
    {
        cout << ch[i][j];
    }
    cout << endl;
}

return 0;

}