how can we sort data from text file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2006
Posts: 1
Reputation: champ80 is an unknown quantity at this point 
Solved Threads: 0
champ80 champ80 is offline Offline
Newbie Poster

how can we sort data from text file

 
0
  #1
Feb 16th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,614
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: how can we sort data from text file

 
0
  #2
Feb 16th, 2006
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 178
Reputation: jim mcnamara is on a distinguished road 
Solved Threads: 10
jim mcnamara jim mcnamara is offline Offline
Junior Poster

Re: how can we sort data from text file

 
0
  #3
Feb 16th, 2006
IF you're on unix:
  1. sort -t ',' k- 1,1 -o newfile.csv oldfile.csv
will sort the file the way you want - outside C++.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 2
Reputation: Amit99531 is an unknown quantity at this point 
Solved Threads: 0
Amit99531 Amit99531 is offline Offline
Newbie Poster

Re: how can we sort data from text file

 
0
  #4
Oct 13th, 2008
i have made following programe but it doesn't work properly
  1. #include<fstream.h>
  2. #include<conio.h>
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include<stdlib.h>
  6. struct ini{
  7. char itmna[35];
  8. int itmcd,qty;
  9. float prc;
  10. };
  11. class ret
  12. { ini in;
  13. int cd[20],qty1[20];
  14. float prc1[20],tot;
  15. char itmna1[20][35];
  16. public:
  17. void add()
  18. {char ch='y';
  19. ofstream q("initial.dat",ios::app);
  20. do
  21. {
  22. cout<<"\nEnter the item code: ";
  23. cin>>in.itmcd;
  24. cout<<"\nEnter item name: ";
  25. gets(in.itmna);
  26. cout<<"\nEnter the quantity: ";
  27. cin>>in.qty;
  28. cout<<"\nEnter it's unit price: ";
  29. cin>>in.prc;
  30. q.write((char*)&in,sizeof(in));
  31. cout<<"\nWant to add more items(y\n): ";
  32. cin>>ch;
  33. }while(ch=='y'||ch=='Y');
  34. clrscr();
  35. q.close();
  36. }
  37. void purch()
  38. { char ch='y';
  39. int i1=0,j,f;
  40. ofstream ou("initial.dat",ios::app);
  41. ifstream i("initial.dat");
  42. do
  43. {
  44. cout<<"\nEnter the item code: ";
  45. cin>>cd[i1];
  46. cout<<"\nEnter the quantity: ";
  47. cin>>qty1[i1];
  48. while(i)
  49. { if(in.itmcd==cd[i1])
  50. { i.read((char*)&in,sizeof(in));
  51. strcpy(itmna1[i1],in.itmna);
  52. prc1[i1]=in.prc;
  53. if(in.qty<qty1[i1])
  54. {
  55. cout<<"\nStock not available:";
  56. }
  57. else
  58. {
  59. in.prc-=prc1[i1];
  60. ou.write((char*)&in,sizeof(in));
  61. }
  62. break;
  63. }
  64. else
  65. {cout<<"\nNot available!!!!!";
  66. break;
  67. }
  68. cout<<"\nPurchase more item(y\n): ";
  69. cin>>ch;
  70. i1++;
  71. }
  72. }while(ch=='y'||ch=='Y');
  73. clrscr();
  74. gotoxy(4,1);
  75. cout<<"****BILL****";
  76. gotoxy(2,2);
  77. cout<<"Item code:\tItem name:\tM.R.P\tQuantity:\tTotal";
  78. for(j=0;j<=i1;j++)
  79. { gotoxy(2,j+3);
  80. cout<<cd[j]<<"\t\t"<<itmna1[j]<<"\t\t"<<prc1[j]<<"\t"<<qty1[j]<<"\t"<<prc1[j]*qty1[j];
  81. tot+=(qty1[j]*prc1[j]);
  82. }
  83. gotoxy(2,i1+4);
  84. cout<<"TOTAL:\t\t\t\t\t\t\t\t"<<tot;
  85. i.close();
  86. ou.close();
  87. }
  88. }s;
  89. void main()
  90. { clrscr();
  91. s.add();
  92. s.purch();
  93. getch();
  94. }
Last edited by Narue; Oct 13th, 2008 at 9:27 am. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 32
Reputation: emotionalone is an unknown quantity at this point 
Solved Threads: 4
emotionalone emotionalone is offline Offline
Light Poster

Re: how can we sort data from text file

 
0
  #5
Oct 13th, 2008
Use code tags please. That kind of post is a real bitch to format properly for a clearer view.
=3
Last edited by emotionalone; Oct 13th, 2008 at 5:29 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC