transfer info from one file to another

I need to transfer the content from the file text.txt to file sort.txt .
The data in the text.txt is "struct" (info about people)but the same data needs to be sorted by the name and sent to sort.txt by using a function .I be gratfull if someone can help me
Show us what you have so far.
void sort1(int n)
{
// n= num of people
int i, j;
int temp_int;
string temp_string;
long long temp_long_long;
ifstream input("text.txt");
ofstream output("sort.txt");
n=n-1;
j=0;

while (!input.eof())
{

input>>aplikant[j].prezime;
input>>aplikant[j].ime;
input>>aplikant[j].maticenbroj;
input>>aplikant[j].datum_na_aplikacija;
input>>aplikant[j].licna_karta;
input>>aplikant[j].pasos;
input>>aplikant[j].vozacka_dozvola;
j++;
}


for (i=0; i<j; i++)

for(int k =(i+1); k < j; k++)

if (aplikant[i].prezime < aplikant[k].prezime)
{
temp_string= aplikant[i].ime;
aplikant[i].ime = aplikant[k].ime;
aplikant[k].ime = temp_string;

temp_string= aplikant[i].prezime;
aplikant[i].prezime = aplikant[k].prezime;
aplikant[k].prezime = temp_string;

temp_long_long = aplikant[i].maticenbroj;
aplikant[i].maticenbroj = aplikant[k].maticenbroj;
aplikant[k].maticenbroj = temp_long_long;

temp_int = aplikant[i].datum_na_aplikacija;
aplikant[i].datum_na_aplikacija = aplikant[k].datum_na_aplikacija;
aplikant[k].datum_na_aplikacija = temp_int;

temp_string = aplikant[i].licna_karta;
aplikant[i].licna_karta = aplikant[k].licna_karta;
aplikant[k].licna_karta = temp_string;

temp_string = aplikant[i].pasos;
aplikant[i].pasos = aplikant[k].pasos;
aplikant[k].pasos = temp_string;

temp_string = aplikant[i].vozacka_dozvola;
aplikant[i].vozacka_dozvola = aplikant[k].vozacka_dozvola;
aplikant[k].vozacka_dozvola = temp_string;
}




for(i=0; i<j-1; i++)
{
output<<aplikant[i].ime;
output<<endl;
output<<aplikant[i].prezime;
output<<endl;
output<<aplikant[i].maticenbroj;
output<<endl;
output<<aplikant[i].datum_na_aplikacija;
output<<endl;
output<<aplikant[i].licna_karta;
output<<endl;
output<<aplikant[i].pasos;
output<<endl;
output<<aplikant[i].vozacka_dozvola;
output<<endl;
}
1) Do not do swap line by line. Make use of operator= (which you do not even need to provide: default version is fine here) or std::swap function:
1
2
3
4
5
6
7
8
temp_string= aplikant[i].ime;
aplikant[i].ime = aplikant[k].ime;
aplikant[k].ime = temp_string;
//[...]
temp_string = aplikant[i].vozacka_dozvola;
aplikant[i].vozacka_dozvola = aplikant[k].vozacka_dozvola;
aplikant[k].vozacka_dozvola = temp_string;
std::swap(aplikant[i], aplikant[k])

2) Do not loop on eof. It is not doing what you think and can be dangerous.
3) Provide operators << and >> for your struct, so you could do
1
2
3
input >> aplikant[j];
//...
output << aplikant[i];

4) There is already function which implements efficient sorting: std::sort
can you give me specific answer im beginner so i dont understand much
Note that this code assumes that your string do not contain spaces. If they do, you need to change input operator.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>

struct person //Do not know original name
{
    std::string prezime;
    std::string ime;
    int maticenbroj;
    long long datum_na_aplikacija;
    std::string  licna_karta;
    std::string  pasos;
    std::string  vozacka_dozvola;
};

//Helper functions to handle inout and output traditional way;
std::istream& operator>>(std::istream& in, person& val)
{
    in >> val.prezime >> val.ime >> val.maticenbroj >> val.datum_na_aplikacija >>
          val.licna_karta >> val.pasos >> val.vozacka_dozvola;
    return in;
}

std::ostream& operator<<(std::ostream& out, const person& val)
{
    out << val.ime                 << '\n' <<
           val.prezime             << '\n' <<
           val.maticenbroj         << '\n' <<
           val.datum_na_aplikacija << '\n' <<
           val.licna_karta         << '\n' <<
           val.pasos               << '\n' <<
           val.vozacka_dozvola     << '\n'   ;
    return out;
}

//Functor for sorting
struct sort_by_name
{
    bool operator()(const person& lhs, const person& rhs)
    { return lhs.prezime < rhs.prezime; }
};

void sort()
{
    std::ifstream input("text.txt");
    std::vector<person> aplikant;
    person temp;
    while(input >> temp) aplikant.push_back(temp);
    std::sort(aplikant.begin(), aplikant.end(), sort_by_name());
    std::ofstream output("sort.txt");
    for(const auto& p: aplikant) output << p;
}

int main()
{
    sort();
}
i dont know why but it doesnt work it shows a lot of errors, any other idea
it doesnt work
It does. Do you have C++11 support turned on?

it shows a lot of errors
For the future, nobody here can read minds and see errors which werent posted here.

any other idea
Read this code, understand what roughtly it does, do the same. Or replace parts which gives you an error with something different.
i corected most of it but i dont know what to do with line 52 it says ISO C++ forbids declaration of ā€˜pā€™ with no type
Ah, it is just a common for loop with slightly different C++11 syntax. Just iterate over a vector like you would normally.

And I strongly suggest to upgrade to modern C++ standard.
Topic archived. No new replies allowed.