Problem with .txt sorting algorithm [SOLVED]

I am writing a program which takes .txt file, finds all words that end with a specific letter, puts them into another .txt. Then I have to sort new .txt in alphabetical order. I made the first work. When I try to sort after first cycle program crashes. I am totally new to input/output and vector functions so this code doesn't look pretty. My final plan is to make it into code with a bunch of functions.

* Might it be crashing because of constant opening/closing of .txt ?
* Too many for cycles makes it crash ?

UPDATE:

Cleared up the code, made it more readable and changed up few things. Works fine.

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <fstream>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <vector>

using namespace std;

void isvestis();
void rikiavimas(char);


main(){
	
	char abc[] = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ";
	
	isvestis();
	remove("tekstas_out.txt");
	for (int i=0; i<= 50; i++) rikiavimas(abc[i]);
	remove("temp.txt");
	return 0;
	
}
	
void isvestis(){
	
	// Sukuria laikina temp.txt su pasirinktais zodziais
	
		string line;
		ifstream input ("tekstas.txt");
	
	if (input.is_open()){
		
		// Suranda zodzius su pasirinkta raide.
		while (!input.eof()){
	
			input >> line;
			
			// Suranda zodzius, kurie baigiasi pasirinkta raide.	
			if (line.rfind("e") == line.size()-1 || line.rfind("E") == line.size()-1){
					
						ofstream output;
						output.open ("temp.txt", ios::out | ios::app);
						if (output.is_open()){
						output << line << endl;
						} else printf("Nepavyko irasyti eilutes.");
						  
			}
			
			// Suranda zodzius, kurie baigiasi su zenklais	
			else if ((line.rfind("e") == line.size()-2 || line.rfind("E") == line.size()-2) && (line.rfind(",") == line.size()-1 || line.rfind(".") == line.size()-1 || line.rfind('"') == line.size()-1 )){
					
						ofstream output;
						output.open ("temp.txt", ios::out | ios::app);
						if (output.is_open()){
						output << line << endl;
						} else printf("Nepavyko irasyti eilutes.");
						  
			}
			
		} input.close();
		
	} else printf("Nepavyko atidaryti failo.\n");
	
}

void rikiavimas(char raide){
	
	string line, zodis;
	vector<string> myV;
		
			// Nuskaito "temp.txt".	
			ifstream input;
			input.open ("temp.txt");
		
			if (input.is_open()){
							
					// Sukuria vektoriu su zodziais, kurie prasideda pasirinkta raide.	
					while (!input.eof()){	
						input >> line;
						if (line.find(raide) == 0) myV.push_back(line);	
					}
			} input.close();
	
	
	// Patikrina ar sukurtas vektorius nera tuscias.
	if (myV.size() != 0){
			
			// Surikiuoja myV vektoriu didejimo tvarka.	
			for (unsigned int j=0; j< myV.size()-1; j++){
			for (unsigned int i=0; i< myV.size()-1; i++){
			if (myV[i] > myV[i+1]) swap(myV[i], myV[i+1]); }
			}
						
			// Iraso zodzius i "tekstas_out.txt
			ofstream output;
			output.open ("tekstas_out.txt", ios::out | ios::app);
			
		if (output.is_open()){
				
			for (unsigned int k=0; k<myV.size(); k++){
			zodis = myV[k];
			output << zodis << endl; 	
			} output.close();
			
		} else cout << "Nepavyko atidaryti failo";

	} 
}
Last edited on
It could be the opening of the file inside your loops is causing problems, I suggest you just open your files once at the beginning of the functions.

Also look at this snippet:
1
2
3
4
5
6
7
8
9
10
11
12
	
	for (int p=97; p<=122; p++){	
			
			// Kartoti pagal abc[] per visa abecele.	
			while (!input.eof()){	
				input >> line;
				if (line.find(char (p)) == 0) myV.push_back(line);	
			}
			
		
		//Surikiuoja vektoriu
		for (int j=0; j< myV.size()-1; j++){



I doubt that the find function is doing what you expect. You may want to find the documentation for this function and pay attention to the return value, this function will only return zero if the specified letter is at the beginning of the string.

What happens if no matching letters are found?
My intention was to filter .txt file with my chosen words for their first letters, put them into a vector, sort them inside and output it into new .txt file. if (line.find(char (p)) == 0) myV.push_back(line); Actually works by converting p (from 97 to 122, that is ASCII from a to z) to char and looking for it in the first position of my line. The only problem I am left with is that p doesn't change value inside this line for some reason while it does in other parts of the for loop. Tomorrow I will try to rewrite this program with functions instead of endless looping, I think it will work.
The problem is probably the fact that you are executing the while() until eof() is reached. eof() is an error condition, and once the stream fails no further processing can occur on that stream until the error state is cleared.

There are probably better ways of doing this instead of looping through the file again and again.

Also why are you using the int in your for loop, it would probably be clearer if you used a char instead.

1
2
3
4
5
for(char p = 'a'; p <= 'z'; ++p){
...
//      if(line.find(p) == 0)
        // This would probably be more self explanatory. 
        if(line[0] == p) 

Also I recommend you start using the C++ style casts instead of the C style casts.


Topic archived. No new replies allowed.