简体   繁体   English

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

[英]C++ read file transfer to 2d Array

So as the title says I'm trying to read from a file and save the characters into a 2d array with spaces but it gets stuck in the do while loop and last char of the file. 因此,正如标题所述,我正在尝试从文件中读取字符,并将字符保存到带有空格的2d数组中,但它卡在文件的do while循环和最后一个char中。 any suggestions on how to fix this? 对于如何解决这个问题,有任何的建议吗? i thought about using the eofbit but I couldn't figure it out then i tried exiting the do while loop by || 我考虑过要使用eofbit,但我无法弄清楚,然后我尝试通过||退出do while循环。 it with eof that didnt work. 它与没有工作。 it seems to just hang on the last letter of the file. 它似乎只是挂在文件的最后一个字母上。 thank you in advance for your advice 预先感谢您的建议

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;

} }

The loop is because of the temp != '\\n' , at the end of the file this condition is always true, so the second condition is never verified because you check it with a or. 循环是由于temp != '\\n'导致的,在文件末尾,此条件始终为true,因此永远不会验证第二个条件,因为您使用or对其进行了检查。 If you want to keep your code, bring the !infile.eof() at the first place. 如果要保留代码,请首先添加!infile.eof()

But if you want a best way and an easy way to read a file. 但是,如果您想要一种最佳的方法和一种简单的方法来读取文件。 You should use getline . 您应该使用getline

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

I didn't compile this but this should work: Read the comments and learn from it. 我没有编译它,但是它应该起作用:阅读注释并从中学习。

#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 will only return true after reading the end of the stream. iostream::eof仅在读取流的末尾后才返回true。 It does not indicate, that the next read will be the end of the stream. 它并不表示下次读取将是流的结尾。 Ref this . 引用这个 So, the modified code would look like the following code snippet. 因此,修改后的代码将类似于以下代码片段。

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;

}

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

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