![]() |
| ||
| how can we sort data from text file please tell me algorithm or code for following character sorting (Ascending) problem. For example Text file contains following data: 001, PSTR, abcdef, h.no.222 abcdefghik lmnopq rstuv, student, A-level 002, MKTO, abcdef, house. 2 abcdefghik lmnopq rstuv, student, rtv 003, LORR, abcdef, h.number.210 abcdefghik lmnopq rstuv, student, O-level 005, ASMP, abcdef, h.#.111 abcdefghik lmnopq rstuv, student, mpr 006, SBIL, abcdef, house.no.234abcdefghik lmnopq rstuv, student, h 007, CEGF, seefwd, h. 120 abcdefghik lmnopq rstuv, student, stvdedstg 008, XMEL, decfab, h.no.333 abcdefghik lmnopq rstuv, student, mstvuv 009, BSTU, abcefd, h.no.333 abcdefghik lmnopq rstuv, student, mstvuv 010, ZJRV, defabc, h.no.333 abcdefghik lmnopq rstuv, student, mstvuv Data is comma separated in the text file. How can i sort in the ascending order on the basis of data that is after first comma for example "PSTR" here. please tell me using file handling mechanism. I have done following: 1. used structure to store a string of Key and int line number: struct myData { std::string Key; int line; }; myData d[10]; 2. extract first number in Integer Line and Column in all object values. 3. Using nested for loop i have done BUBBLE Sort :( . and inserted the sorted sequence in new file. 4. By using nested while loop i have done following: external loop opens sorted text file first character that is Id. then walks into internal loop which reads line from old file and compares external Id from internal Id if both same than output is shown in the new file. but this doesn't works fine. it extracts only first 2 records out of 10. please tell me what should be done here and tell me what should be done when record has many entries (3000 or more) in text file with comma separation. please tell me easy way to solve the problem. waiting for reply |
| ||
| Re: how can we sort data from text file You've found the easiest way to do it. Read each line into a record, break the record up into fields, append the record to an array of records, and sort the array by your chosen field. If you have a lot of records, use a faster sorting algorithm such as shell sort or std::sort. If the file is huge, you may have to sort it in chunks and then merge the chunks into a result file, making use of hard disk storage to cover for any lack of physical memory. But I doubt you'll need to do that. |
| ||
| Re: how can we sort data from text file IF you're on unix: sort -t ',' k- 1,1 -o newfile.csv oldfile.csvwill sort the file the way you want - outside C++. |
| ||
| Re: how can we sort data from text file i have made following programe but it doesn't work properly #include<fstream.h> |
| ||
| Re: how can we sort data from text file Use code tags please. That kind of post is a real bitch to format properly for a clearer view. =3 |
| All times are GMT -4. The time now is 3:39 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC