open file and sorting

I want to open a .txt file, then sorting the first 100 characters. Here is my code
#include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
using namespace std;
void insertion_sort (char list[], int n)
{
int i, j;
char next;
for (i=1; i<=n; i++)
{
next = list[i];
for (j=i-1; j>=0 && next <=list[j]; j--)
{
list[j+1] = list[j];
list[j] = next;
}
}
}
void quicksort(char list[], int left, int right)
{
int i, j;
char pivot;
if (left < right)
{
i = left;
j = right + 1;
pivot = list[left];
do
{
do i++; while (list[i] < pivot );
do j--; while (list[j] > pivot );
if (i < j) swap(list[i], list[j]);
} while (i < j);
swap (list[left], list[j]);
quicksort (list, left, j-1);
quicksort (list, j+1, right);
}
}
void sort (char list[], int length, int x)
{
if(length >= x)
quicksort (list, 0, length-1);
else
insertion_sort (list, length);
}
int main ()
{
string line;
int i;
char array[100];
ifstream myfile ("example.txt");
if (myfile.is_open())
{
cout << "This is content of the text file: \n\n";
while (!myfile.eof())
{
getline (myfile, line);
cout << line << endl;
}
}
else
cout << "Unable to open file. Please ensure you have file named example.txt on your PC";
myfile.read (array, sizeof(array));
sort (array,100,75);
cout << "The first 100 characters after sorting: \n";
for (i=0;i<100;i++)
cout << array[i] << " ";
getch();
return 0;
}


however there is problem when the sorting array is showed. Please help me fix it . Thank you
Are you sure that you want to sort the array yourself? Because there are inbuilt algorithm.

for example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int a[7] = {23, 1, 33, -20, 6, 6, 9};
    
    sort(a, a+7);
    
    for (int i=0; i<7; i++) {
        cout << a[i] << " ";
    }
    
    return 0;
}



http://www.cplusplus.com/reference/algorithm/sort/

You should close your file: myfile.close(); and I advise you to format your code. It will be more readable.

http://www.gidnetwork.com/b-38.html

here is my trouble


file.open ("example.txt",ios::in);
cout << "This is content of the text file: \n\n";
getline (file, line);
cout << line << endl;

file.read (array, sizeof(array));
function_sort (array,100,75);
cout << "The first 100 characters after sorting: \n";
for (i=0;i<100;i++)
cout << array[i] << " ";


the program can display my txt file, but after sentence "the first 100 characters after sorting" it displays "strange character" instead of sorted character. However, if I remove bold statements like this


file.open ("example.txt",ios::in);
cout << "This is content of the text file: \n\n";
file.read (array, sizeof(array));
function_sort (array,100,75);
cout << "The first 100 characters after sorting: \n";
for (i=0;i<100;i++)
cout << array[i] << " ";


The result of sorting is ok. Is there anything wrong with that bold statement?



Last edited on
If you read some text from your text file (getline (file, line);) then the file position is move away! So if you read again (file.read (array, sizeof(array));) I think your second reading can't read anything.

You should check the success of the file reading.

http://www.cplusplus.com/reference/iostream/fstream/
Check the good, eof, fail bad members.

I suspect the your second reading wasn't success therefore your program write uninitialized data from array. It is rewarding fill up the variables and arrays with zero before use it. You can use simple for loop for (int i = 0; i < 100; i++)array[i] = 0; or memset:

http://www.cplusplus.com/reference/clibrary/cstring/memset/

The solution is that you should close and open again the myfile, or you just read one time and print out screen and sort it.
Last edited on
Topic archived. No new replies allowed.