Click here to Skip to main content
15,905,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a file F.txt with the numbers:
-9
-8
-7
-6
-5
-4
-3
-2
-1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16


I have to sort the numbers in file F in descending order and put the numbers to file G. Im stuck pls help.

What I have tried:

C++
#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <fstream>
#include <algorithm>
#include <vector>

int main()
{
  int skaitli,skaitli2, it = 0;
  std::string rinda;
  std::ifstream F("F.txt");
  std::ofstream G;
  G.open("G.txt");

  while(std::getline(F, rinda))
  {
    try
    {
      skaitli = std::stoi(rinda);
    }
catch(std::invalid_argument& e)
    {
      std::cout << "Nav skaitlis " << it << '\n';
      continue;
    }

   it++;
}
G.close();

}
Posted
Updated 20-May-21 8:09am
v3
Comments
Richard MacCutchan 20-May-21 7:03am    
The numbers are already in ascending order.

Try
C++
#include <fstream>
#include <iterator>
#include <set>
#include <algorithm>
using namespace std;
int main()
{
  auto is = ifstream{"F.txt"};
  auto os = ofstream{"G.txt"};
  auto s = set<int, greater<int>>{istream_iterator<int>{is}, istream_iterator<int>{}};
  std::copy(s.begin(), s.end(), ostream_iterator<int>{os, "\n"});
}
 
Share this answer
 
Comments
merano99 20-May-21 19:43pm    
error C2065: "greater": undeclared identifier
With other header would work for me.
// #include <algorithm>
#include <functional>

Note: A set cannot store duplicates. Ignoring duplicates was not required here.
It's not complicated:

1) Read the lines from the input file, one by one.
2) Convert each value to a number, and store it in an array (depending on your skill level, it may be simpler to read the file twice: once to get the number of lines so you know how big an array to allocate, then again to fill the array).
3) Sort the array. You can write your own function, or use the qsort function[^] from the C standard library.
4) Write each array element to a new file.

None of this is complicated, and it's all stuff you have probably done in previous exercises. If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
You are including <vector> and <algorithm> so why not make use of them?

You should do the steps Mr. Griff has described, just slightly differently. You can store the numbers in a vector so declare a std::vector to save the values. Then convert the values you read to a number and store them in the vector using its push_back method. After you have read the file then sort it but use std::sort for that. You can adjust the comparison function it uses to get the sorting order you want.

Here are some references that might be helpful :

C++ : vector
C++ : sort
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900