简体   繁体   中英

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,

Maybe something like this?

std::string line;
std::getline(file, line);
line = line.substr(1, line.length() - 2);
std::istringstream iss(line);
iss >> A >> B >> C >> D;
#include<iostream> 
#include<vector>
#include<ifstream>

using namespace std; 

    struct pairs
    {
        char ch; 
        int value;
    };
    int main()
    {   
        vector<pairs> store;
        ifstream is("koord.txt"); 
        pairs temp;
        char ch = 'A';
        while (is)
        {
            temp.ch = ch; 
            is >> temp.value; 
            store.push_back(temp);
        }
        for (int i = 0; i < store.size(); ++i)
        {
            cout << store[i].ch << " " << store[i].value << endl;
        }
        return 0;
    }

you can use this. But if you read more entries char will overflow. you can use string instead of char.

This is what I ended up doing, might not be the most efficient way of doing it but it seems to work. But I will look into std::istringstream . It seems like good way to do it.

void getFirstLine(int &A, int &B, int &C, int &D)
{
    string myText;
    string temp;
    vector<int> save;
    int size = myText.size();
    getline(File, myText); //get the first line of text not including \n
    for (int i = 0, j = 0, size = myText.length(); i < size; i++)
    {
        temp.push_back(myText[i]); //adds the myTest[i] to the end of temp
        if (myText[i] == ' ')
        {
            save.push_back(stoi(temp)); //stoi(temp) to an int --> push_back appened to save at the end
            j++;
            temp.clear();
        }
        if (i == (size - 1)) //because the end of the line doesnt have a space
        {
            save.push_back(stoi(temp));
            j++;
            temp.clear();
        }
    }
     for (int i = 0; i < 5; i++){
         cout << "list "<< i << ": "<< save[i] << endl;
     }
     //retruning the values
     A = save[0];
     B = save[1];
     C = save[2];
     D = save[3];
}

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