read and write to text files

hi, i am trying to read a list of names from a txt file and then sort it out. Once sorted i will have to write them to a new txt file. i have done this method multiple times already, but this is the first time i have encountered this error D:. I don't know what i am doing wrong since i know i wrote the process of opening a new file correctly or at least it should be correct. Any suggestions will be great, thanks!

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 <string>
using namespace std;

int main()
{
	int i, location;
	string list[5], temp;
	
	ifstream file;
	file.open("UnsortedNames.txt"); //file has 5 names separated by \n
	
	ofstream test;
	test.open("SortedNames.txt");
	
	if(!file)
	{
		cout<<"No file exists. Please create a txt file named \"UnsortedNames\"";
	}
	else
	{
		cout<<"Your list is sorted."<<endl
		<<"UnsortedNames.txt\n"<<endl;
		
		while(!file.eof())
		{	
			getline(file, list[i], '\n');
			cout<<list[i]<<endl;
			i++;
		}	
		file.close();
		
		for(i = 1; i <= 5; i++)
		{
			if (list[i] < list[i-1])
			{
				temp = list[i];
				location = i;
				do
				{
					list[location] = list[location - 1];
					location--;
				}while (location > 0 && list[location - 1 ] > temp);
				list[location] = temp;
			}
		}
		cout<<"\n";
		
		for (i = 0; i < 5; i++)
		{
			cout<<list[i]<<endl;
			test<<list[i]<<endl;
		}
		test.close();
	}
	return 0;
}
Is your program:
1) Not compiles
    * Is other code compiles correctly, i. e. is your compiler configured properly?
    * If it is, give us error message and corresponding code part.
2) Not running
    * Are you sure that it is not a problem with automatically closing console?
    * How do you run it?
    * Is there any error messages?
3) Gives an error when run
    * Is it system error message or error reported in console?
    * Give us error message and input which leads to error.
4) Not giving correct results
    * Tell what you entered, what you expected, and what you got.
    * Are you sure that results you expected are correct?
Everytime i run my program, a message will pop out "insertSort.exe has stopped working A problem caused the program to stop working correctly. Windows will close the program and notify you if a solution is available." When i use getline in line 28, i get the 5 names that i listed down separated by '\n' in a file. I would then reorganize those names in alphabetical order. If i output the array in its new sorted organization, it works out fine. However, if i try to place the new sorted names into a new file, i get that error message.
for(i = 1; i <= 5; i++)list[5] does not exist. it contains only five elements.
ah yes. i changed that mistake. But it still gives me the stopped running message ):
How did you fix it?
changed it to this for(i = 0; i < 5; i++)
1
2
3
for(i = 0; i < 5; i++)
//...
if (list[i] < list[i-1])
There is no list[-1] either.
ahhhhh thank you for pointing that out for me!!! However, instead of continuing with just i = 0, i changed it back cuz this code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 for(i = 1; i <= 4; i++)
		{
			if (list[i] < list[i-1])
			{
				temp = list[i];
				location = i;
				do
				{
					list[location] = list[location - 1];
					location--;
				}while (location > 0 && list[location - 1 ] > temp);
				list[location] = temp;
			}
		} 

i suppose to sort the names thats why location - 1 is there. But since list[5] doesnt exist like you said, i changed the the i <=5 to i<=4 instead and now it works! haha thanks for the point out!
Is there any reason for you to write your own sort instead of using already existing one?

And using raw arrays instead of something more flexible and supporting any amount of names in your program? like that:
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
#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

int main()
{
    std::ifstream infile("UnsortedNames.txt");
    if(!infile) {
        std::cout << "No file exists. Please create a txt file named \"UnsortedNames.txt\"";
        return 0;
    }
    std::cout << "Your list is sorted.\nUnsortedNames.txt\n\n";

    std::vector<std::string> list;
    {
        std::string temp;
        while(std::getline(infile, temp))
            list.push_back(std::move(temp));
    }
    std::sort(list.begin(), list.end());

    std::ofstream outfile("SortedNames.txt");
    for (const auto& s: list)   {
        std::cout << s << '\n';
        outfile   << s << '\n';
    }
}
@MiiNiPaa Probably becuase of Universities. Most universities give out strict instructions on what to do (including mine) So you dont have much of a choice even if you would LOVE to use vectors rather than arrays etc.
Last edited on
^yes. That sort was a given that we had to use. I am aware of simpler sorting programs, but they require us to do so with the givens :(. oh wells
Topic archived. No new replies allowed.