vendredi 26 février 2021

Sorting out int variables from a .txt file (fstream, c++)

I have a .txt file with these this line on the top '12 4 25 257' each of the numbers spaced out by a ' ' and the line ends with '\n'

I have these variables and a function getFirstLine:

int A;
int B;
int C;
int D;
ifstream File("a.txt");
void getFirstLine(int &A, int &B, int &C, int &D)
{
 int list[] = {A, B, C, D};
    string myText;
    getline(File, myText);
    int size = myText.size();
    cout << size;
    for (int i = 0; i < size; i++)
    {
        if (myText[i] != ' ')
        {
            list[i] = (int)myText[i] - 48;
            cout << list[i] << endl;
        }
    }
}

I want to basically save the first number on A, second on B, third C etc...

I cant seem to get this working sadly :( Can someone help me with this?

Output should look like:

A = 12,
B = 4,
C = 25,
D = 257,

Aucun commentaire:

Enregistrer un commentaire