C++ 从.txt文件(fstream、c+;+;)中排序int变量

C++ 从.txt文件(fstream、c+;+;)中排序int变量,c++,file,c++11,fstream,ifstream,C++,File,C++11,Fstream,Ifstream,我有一个.txt文件,上面的这一行是'12 4 25 257'每个数字都用'隔开,这一行以'\n'结尾 我有这些变量和一个函数getFirstLine: int A; int B; int C; int D; #包括 #包括 #包括 使用名称空间std; 结构对 { char ch; int值; }; int main() { 向量存储; ifstream是(“koord.txt”); 双温; char ch='A'; while(is) { 温度ch=ch; 是>>温度值; 储存。推回

我有一个.txt文件,上面的这一行是
'12 4 25 257'
每个数字都用
'
隔开,这一行以
'\n'
结尾

我有这些变量和一个函数
getFirstLine

int A;
int B;
int C;
int D;
#包括
#包括
#包括
使用名称空间std;
结构对
{
char ch;
int值;
};
int main()
{   
向量存储;
ifstream是(“koord.txt”);
双温;
char ch='A';
while(is)
{
温度ch=ch;
是>>温度值;
储存。推回(温度);
}
对于(int i=0;i可能是这样的吗

std::字符串行;
std::getline(文件,行);
line=line.substr(1,line.length()-2);
标准::istringstream iss(线);
iss>>A>>B>>C>>D;

这是我最后做的,可能不是最有效的方法,但它似乎有效。但我将研究一下
std::istringstream
。这似乎是一个很好的方法

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];
}
void getFirstLine(int&A、int&B、int&C、int&D)
{
字符串myText;
字符串温度;
矢量保存;
int size=myText.size();
getline(文件,myText);//获取不包含的第一行文本\n
对于(inti=0,j=0,size=myText.length();ipush_back显示为在末尾保存
j++;
温度清除();
}
if(i==(size-1))//因为行尾没有空格
{
保存。推回(stoi(temp));
j++;
温度清除();
}
}
对于(int i=0;i<5;i++){

CUT大致上是一条很好的路径,但是正如你已经发现的,它是超级JANKY。考虑使用标准化的解析函数(<代码> STD::STOI),或者直接使用提取运算符提取数字。什么不起作用?请添加一个Read a行,这是您已经做过的。然后将该行放入
std::istringstream
中,然后使用该行解析数字。您现在所做的事情与您想要的完全不同,没有任何意义,并且将导致未定义的行为r、 顺便说一句,我忘了增加char。如果添加++ch;,push_-back后函数字母将增加。谢谢-这很有用!使用结构实际上对我需要做的事情很有意义:)
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;
        }
    }
}
A = 12,
B = 4,
C = 25,
D = 257,
#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;
    }
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];
}