Right, I'm writing some code using databases, and someone at my school was talking about some specific command that could automatically get a file and sort it.
I think I have a grasp on how to sort this file manually, but it requires a lot of work, so I was wondering if anyone knew this function.
edit: using DEV C++ , Library #stdio.h and # conio.h , (i can add the extra library if necessary) no idea what the specific complier is however.
As others have mentioned there is
sort
command in Unix:see
man sort
for more options.And there is
SORT
command on Windows:or
for more help see: http://www.computerhope.com/sorthlp.htm
With that you could write simple C program that would use those commands.
Here we just expect from user to enter file he wants to sort and then we are using this file and just give it to
sort
command. But first we need to make sure that user actually entered something. That is why we haveif
on beginning ofmain
function.Second approach would be to write
sort
command ourself. The fun starts here. :DIdea is to have some dynamic "lines container" whose initial size is 16. Every time we see that we need more space for storing our lines, we double the size of our "lines container".
Also, we have some buffer that we use to store every line and then we copy that line into next line container slot.
Here I limited size of one line to 10^6 characters. But you could dynamically allocate space for buffer too by using
ftell
andfseek
functions. I suggest you to do that for practice. :)For actual sorting of strings we are using
qsort
function fromstdlib.h
:http://www.cplusplus.com/reference/cstdlib/qsort/
assuming your input file is data.txt (Linux), you can use the system command
sort
: