如何从文件中获取整个整数有字符串和整数并将它们存储到 C++ 中的数组中?

How to obtain the whole integers from file has strings and integers and store them into array in C++?

我想从也有字符串的文件中获取整数,并将它们存储到数组中以对它们进行一些操作。整数可以是 1 或 12 或 234,所以是 3 位数字。我正在尝试这样做,但是当我 运行 代码

时输出停止
void GetNumFromFile (ifstream &file1, char & contents)
{
    int digits[20];
    file1.get(contents);
    while(!file1.eof())
    {
        for (int n = 0; n < 10; n++)
        {
            if(('0' <= contents && contents <= '9') && ('0' >= contents+1 && contents+1 > '9'));
            digits[n]=contents;
            if(('0' <= contents && contents <= '9') && ('0' <= contents+1 && contents+1 < '9'));
            digits[n]=contents;
            if(('0' <= contents && contents <= '9') && ('0' <= contents+1 && contents+1 <= '9') && ('0' <= contents+2 && contents+2 < '9'));
            digits[n]=contents;
        }
        continue;
    }
    for (int i = 0; i <= 20; i++)
    {
        cout << *(digits + i) << endl;
    }
}

第一个观察结果:您迭代超出了数组的范围:

int digits[20];
for (int i = 0; i <= 20; i++)

20 个元素和 21 次迭代。这是一个未定义的行为,所以这里一切皆有可能(如果你的程序最终到达这里)。

接下来,您从文件中读取一次,然后出现无限循环,因为对于程序 运行 的其余部分,表达式 !file1.eof() 要么为真,要么为假。这不就是"output stops"的原因吗?

第三个发现:您的 if 语句无用,因为语句后有分号:

if(('0' <= contents && contents <= '9') && ('0' >= contents+1 && contents+1 > '9'));
            digits[n]=contents;

你只是分配 digits[n]=contents; 没有任何检查。

我看不出有任何理由在函数中提供对 char 的引用。为什么不让它成为局部变量?

您还需要先在循环内添加 get() 功能才能到达文件末尾。

一旦发现 char 是一个整数,继续尝试添加一个 while 循环以继续询问下一个字符。

例如

int digits[20];
int i = 0;
ifstream file1("filepath");
char contents;
while (!file1.eof())
{
    file1.get(contents); // get the next character

    if (contents <= '9' && contents >= '0' && i < 20) // if character is in number range
    {
        digits[i++] = contents - '0'; // converting the chat to the right integer
        file1.get(contents);

        while (contents <= '9' && contents >= '0' && i < 20) // while is integer continue on
        {
            digits[i++] = contents - '0';
            file1.get(contents);
        }
    }
}

// do other stuff here

你要处理找到的号码的位数:

int digits[20];
int i = 0;
short int aux[3]; // to format each digit of the numbers

ifstream file1("filepath");
char contents;

file1.get(contents); //first char

if (!file1.eof()) //test if you have only one char in the file
{
    while (!file1.eof() && i < 20) // limit added to read only 20 numbers
    {
        if (contents <= '9' && contents >= '0') // if character is in number range
        {
            aux[0] = contents - '0'; // converting the char to the right integer
            file1.get(contents);

            if (contents <= '9' && contents >= '0') // if contents is number, continue on
            {
                aux[1] = contents - '0';

                if (!file1.eof()) // if has mor char to read, continue on
                {
                    file1.get(contents);

                    if (contents <= '9' && contents >= '0') // if is integer, continue on
                    {
                        aux[2] = contents - '0';
                        file1.get(contents); // will read same of last char if eof, but will have no effect at all
                        //aux[0] *= 100; // define houndred
                        //aux[1] *= 10; // define ten
                        digits[i++] = (aux[0] * 100) + (aux[1] * 10) + aux[2];
                    }
                    else
                    {
                        //aux[0] *= 10; // define ten
                        digits[i++] = (aux[0] * 10) + aux[1];
                    }
                }
                else
                {
                    digits[i++] = (aux[0] * 10) + aux[1];
                }
            }
            else
            {
                digits[i++] = aux[0];
            }
        }
    }
}
else if (contents <= '9' && contents >= '0' && i < 20) // check if the only one char is number
{
    digits[i++} = contents - '0';
}

如果你想读取一个未定义大小的数字,那么你将不得不分配内存以使用 new (c++) 或 malloc(c/c++) 格式化数字的每个数字。