文本文件中的C++排序数

文本文件中的C++排序数,c++,sorting,fstream,C++,Sorting,Fstream,我试图创建一个程序,用户在其中输入数字的数量、用户选择的递增或递减顺序以及数字本身。然后,它创建一个输入文件input.txt,其中包含数字量、用户选择和数字本身。然后,该文件通过一个函数传递,在该函数中,它读取输入文件并创建一个输出文件output.txt,其中包含数字的数量和排序后的数字。目前,当程序试图输入尺寸时,它会给我错误,我不知道为什么。此外,我已经阅读了绝对Walter Savitch的AWNSER C++,但我不能找到它说,你可以有一个程序读取输入文件中的数字和字符。如有任何帮助

我试图创建一个程序,用户在其中输入数字的数量、用户选择的递增或递减顺序以及数字本身。然后,它创建一个输入文件input.txt,其中包含数字量、用户选择和数字本身。然后,该文件通过一个函数传递,在该函数中,它读取输入文件并创建一个输出文件output.txt,其中包含数字的数量和排序后的数字。目前,当程序试图输入尺寸时,它会给我错误,我不知道为什么。此外,我已经阅读了绝对Walter Savitch的AWNSER C++,但我不能找到它说,你可以有一个程序读取输入文件中的数字和字符。如有任何帮助或建议,将不胜感激。所有代码如下

void inputFile(int numbers[], char choice, int size)
{
ifstream inFile;
cout << "\n\n Creating input.txt"<<endl;
inFile.open("input.txt");
//checks if it fails to open
if(inFile.fail())
{
    cout<<"Error#1: Input file failed to open";
    exit(1);
}
cout<<"Inputing user choice and size"<<endl;
//Inputs choice and size into input.txt
inFile >> size >> "\n";
inFile >> choice >> "\n";
cout << "Iputing numbers"<<endl;
//inputs all the numbers
for(int x=0; x<size; x++)
{
    inFile >> numbers[x] >> "\n";
}
outputFile(inFile);
cout << "Closing input file"<<endl;
inFile.close();
}
void outputFile(ifstream& inData)
{
ofstream outFile;
cout << "Creating output.txt"<<endl;
//creates the output.txt file
outFile.open("output.txt");
//check if it fails to open
if(outFile.fail())
{
    cout<<"Error#2: Output file failed to open";
    exit(1);
}
//initilizing variables
int n=0;
int *sort;
int next;
int size;
int totalTimesTrue;
char choice;
cout << "Reading size in input.txt"<<endl;
//takes the size and puts in in the output file
while(inData >> next)
{
    if(next == '\n')
    {
        break;
    }
    size = next;
    outFile << size<<"\n";
}
//creating a dynamic array
sort = new int[size];
cout << "Reading user choice"<<endl;
//reads the user choice from the input file. (a)scending or (d)escending
inData.get(choice);
if(choice = 'a')
{
    cout<<"Sorting numbers in ascending order"<<endl;
    while(inData >> next && n<size)
    {
        if(next == '\n')
        {
            continue;
        }
        sort[n]= next;
        n++;
    }
    for(int x=0; x<size; x++)
    {
        totalTimesTrue=0;
        for(int y=0; y<size; y++)
        {
            if(sort[x]<sort[y])
            {
                totalTimesTrue++;
            }
        }
        sort[totalTimesTrue]=sort[x];
    }

}
else if(choice = 'd')
{
    cout <<"Sorting numbers in decending order"<<endl;
    while(inData >> next && n<size)
    {
        if(next == '\n')
        {
            continue;
        }
        sort[n]=next;
        n++;
    }
    for(int x=0; x<size; x++)
    {
        totalTimesTrue=0;
        for(int y=0; y<size; y++)
        {
            if(sort[x]>sort[y])
            {
                totalTimesTrue++;
            }
        }
        sort[totalTimesTrue]=sort[x];
    }
}
cout<<"Creating the output"<<endl;
for(int x=0; x<size; x++)
{
    outFile << sort[x] << "\n";
}
delete sort;
cout<<"Closing output.txt"<<endl;
outFile.close();
}

代码太多了。你能把范围缩小到你遇到麻烦的地方吗?你的代码分三行恢复:当用户输入数字时,v.push_back读取的数字;std::sort std::begin v,std::end v,用户指定的比较器std::copy std::begin v,std::end v,std::ostream_迭代器文件流'\n';好的,我把范围缩小到函数。@Manu454726我目前不知道push_back、sort、begin和end的作用。还有ostream_迭代器做什么?。push_back在向量的最后一个元素之后,在向量的末尾添加一个新元素。排序按升序对元素进行排序。begin返回第一个元素,end返回最后一个元素。好的,但是我被指示为.txt文件中的每个元素都有新行。这些函数是否读取“\n”命令?