C++读取文件传输到2d数组

C++ read file transfer to 2d Array

本文关键字:2d 数组 传输 读取 文件 C++      更新时间:2023-10-16

因此,正如标题所说,我试图从文件中读取并将字符保存到一个带有空格的2d数组中,但它被困在文件的do-while循环和最后一个字符中。关于如何解决这个问题有什么建议吗?我想过使用eofbit,但我想不通,然后我试着用eof退出do-while循环,但没用。它似乎只是挂在文件的最后一个字母上。提前感谢你的建议

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;

}

循环是因为temp != 'n',在文件末尾,此条件始终为true,因此永远不会验证第二个条件,因为您使用或对其进行检查。如果您想保留您的代码,请将!infile.eof()放在第一位。

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

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

我没有编译它,但它应该可以工作:阅读评论并从中学习。

#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;
}

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

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;
}