C ++从文件中读取文本并将字符串解析为int数组

时间:2022-06-28 19:56:05

First off I am not a C++ expert and am still learning so any insight would be greatly appreciated. This issue at first seemed a simple issue but after a few months, I am still unable to find a viable solution. I expect it has a simple answer that I am merely overlooking.

首先,我不是C ++专家,我仍然在学习,所以任何见解都会受到高度赞赏。这个问题起初似乎是一个简单的问题,但几个月后,我仍然无法找到可行的解决方案。我希望它有一个简单的答案,我只是忽略了。

Below is some code that I wrote from reading similar threads here and on other sites. Basically I am trying to read from a text file that contains GF256 multiplication array(256 lines each 256 long). Reading line by line seems to be just fine evidenced by the line (cout << multi_ << endl;) where I see exactly what I need as a string for each line. It is at this point that I problems. I cannot seem to find a way to parse this string into an int array. I have many iterations of this code with different attempts at doing this, and yet none have worked for me. I assume there is a very simple solution for this. Again, any insight on this issue would be greatly appreciated.

下面是我在这里和其他网站上阅读类似线程时编写的一些代码。基本上我试图从包含GF256乘法数组的文本文件中读取(每256行256行)。逐行读取似乎很好地证明了行(cout << multi_ << endl;),其中我确切地看到了我需要作为每行的字符串。正是在这一点上我遇到了问题。我似乎无法找到将此字符串解析为int数组的方法。我对此代码进行了多次迭代,并尝试了不同的尝试,但没有一个对我有用。我假设有一个非常简单的解决方案。再次,非常感谢对此问题的任何见解。

int kGF = 0;
int multiGFtemp[256 * 256];

int n;

int main()
{
    string multi_;

    ifstream fileGF("GF256MultiTable.txt");

    if(fileGF.is_open())
    {
        while(getline(fileGF,multi_))
        {


            cout << multi_ << endl;
            cin.get();
        }
        fileGF.close();
    }
    else
    {
        cout << "File is not open" << endl;
    }

    cin.get();
    cout << multi_;

    stringstream stream2(multi_);

    while(stream2)
    {
        stream2 >> n;

        cout << n << endl;

        multiGFtemp[kGF] = n;
        kGF++;
    }

    for(int k = 0; k < 256 * 256; k++)
    {
        /*
        cout << multiGFtemp[k] << " ";
        */
    }

    cin.get();
    return 0;
}

3 个解决方案

#1


0  

Your program parses the contents of multi_, which at that point is the last line of the file.
You could move that into the loop, but that would only complicate things – if you just want the 256 * 256 numbers in a plain array, you can ignore the file structure.

您的程序会解析multi_的内容,此时此内容是文件的最后一行。您可以将其移动到循环中,但这只会使事情变得复杂 - 如果您只想要普通数组中的256 * 256数字,则可以忽略文件结构。

This is one way of slurping that file into an array:

这是将该文件压入数组的一种方法:

int multiGFtemp[256 * 256];

int main()
{
    ifstream fileGF("GF256MultiTable.txt");
    int kGF = 0;
    while (kGF < 256 * 256 && fileGF >> multiGFtemp[kGF])
    {
        kGF++;
    }

    for(int k = 0; k < kGF; k++)
    {
        cout << multiGFtemp[k] << " ";
    }
}

#2


0  

I assume you're trying to read a string in a known format into arguments. For example you have file that contains a line: "X = 1035, Y = 6658". If this is the case, i prefer to use sscanf.

我假设您正在尝试将已知格式的字符串读入参数。例如,您的文件包含一行:“X = 1035,Y = 6658”。如果是这种情况,我更喜欢使用sscanf。

for example:

string format("X = %d, Y = %d");
string str("X = 1035, Y = 6658");
int x, y;
sscanf(str.c_str(), format, &x, &y);

#3


0  

The Boost library might help you here. Have a look at Boost String Algo Split for example.

Boost库可能会对您有所帮助。例如,看看Boost String Algo Split。

#1


0  

Your program parses the contents of multi_, which at that point is the last line of the file.
You could move that into the loop, but that would only complicate things – if you just want the 256 * 256 numbers in a plain array, you can ignore the file structure.

您的程序会解析multi_的内容,此时此内容是文件的最后一行。您可以将其移动到循环中,但这只会使事情变得复杂 - 如果您只想要普通数组中的256 * 256数字,则可以忽略文件结构。

This is one way of slurping that file into an array:

这是将该文件压入数组的一种方法:

int multiGFtemp[256 * 256];

int main()
{
    ifstream fileGF("GF256MultiTable.txt");
    int kGF = 0;
    while (kGF < 256 * 256 && fileGF >> multiGFtemp[kGF])
    {
        kGF++;
    }

    for(int k = 0; k < kGF; k++)
    {
        cout << multiGFtemp[k] << " ";
    }
}

#2


0  

I assume you're trying to read a string in a known format into arguments. For example you have file that contains a line: "X = 1035, Y = 6658". If this is the case, i prefer to use sscanf.

我假设您正在尝试将已知格式的字符串读入参数。例如,您的文件包含一行:“X = 1035,Y = 6658”。如果是这种情况,我更喜欢使用sscanf。

for example:

string format("X = %d, Y = %d");
string str("X = 1035, Y = 6658");
int x, y;
sscanf(str.c_str(), format, &x, &y);

#3


0  

The Boost library might help you here. Have a look at Boost String Algo Split for example.

Boost库可能会对您有所帮助。例如,看看Boost String Algo Split。