Heya,
I need to alphabetically sort a textfile and store the ordered result in another textfile.
How can this be solved?
thought of the following solutions:Code:#define LENGTH 255 int main() { FILE *filein; FILE *fileout; char puffer[LENGTH]; filein=fopen("namein.txt","r"); fileout=fopen("nameout.txt", "a"); while(fgets(puffer,LENGTH,filein)) { fputs(puffer,fileout); }
1. idea: store the data in some array and then just use a regular sort algorithm on it.
1. issue: I don't know the amount of entries in advance, thus the size of the array is unknown. I mean I would have to once read through the full textfile to count lines and then create the array based on that information, or is there a better approach?
2. idea: use a second puffer to read through the textfile and sort on the fly by comparing puffer1/puffer2 and storing the very "smallest" in the new textfile
2. issue: sort algorithm needs me to to "delete" or flag entries of the first textfile once they were put into into the second one, and I'm not quite sure if that's even possible with the "while fgets puffer" approach.
could you suggest me some ideas on how to best solve this issue? :/ maybe with tries or something even, I dunno :X
thanks in advance