简体   繁体   中英

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

I want to obtain integers from file that has strings too, and store them into array to do some operation on them. the integers can be 1 or 12 or 234, so 3 digits. I am trying to do that but the output stops when I run the code

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

First observation: you iterate out of bounds of the array:

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

20 elements and 21 iteration. That is an undefined behavior, so everything is possible here (if your program eventually gets here).

Next, you read from file once and then you have an infinite loop because the expression .file1.eof() is either true or false for the rest of the program run. Isn't that the reason of "output stops"?

The third finding: your if statements are useless because of the semicolon after the statement:

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

You just assign digits[n]=contents; without any check.

I neither see any reason of providing a reference to char in the function. Why not to make it a local variable?

You will need first to add get() functionality inside the loop as well in order to reach end of file.

Forthmore try to add a while loop once a char was found to be an integer to continue in asking for the next character.

eg

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

You have to deal with the number of digits of the number found:

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

while (!file1.eof())
{
    if (contents <= '9' && contents >= '0' && i < 20) // 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' && i < 20) && !file1.eof()) // if is integer and has mor char to read, continue on
        {
            aux[1] = contents - '0';
            file1.get(contents);
            if ((contents <= '9' && contents >= '0' && i < 20) && !file1.eof()) // if is integer and has mor char to read, continue on
            {
                aux[2] = contents - '0';
                file1.get(contents);
                aux[0] *= 100; // define houndred
                aux[1] *= 10; // define ten
                digits[i++] = aux[0] + aux[1] + aux[2];

            }
            else
            {
                aux[0] *= 10; // define ten
                digits[i++] = aux[0] + aux[1];
            }
        }
        else
        {
            digits[i++] = aux[0];
        }
    }
    i++;
}

If you want read an undefined size number, then you will have to allocate memory to format each digit of the numers with new (c++) or malloc(c/c++).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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