944,138 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 12252
  • C++ RSS
Feb 16th, 2006
0

how can we sort data from text file

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
champ80 is offline Offline
1 posts
since Feb 2006
Feb 16th, 2006
0

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Feb 16th, 2006
0

Re: how can we sort data from text file

IF you're on unix:
C++ Syntax (Toggle Plain Text)
  1. sort -t ',' k- 1,1 -o newfile.csv oldfile.csv
will sort the file the way you want - outside C++.
Reputation Points: 62
Solved Threads: 10
Junior Poster
jim mcnamara is offline Offline
179 posts
since May 2004
Oct 13th, 2008
0

Re: how can we sort data from text file

i have made following programe but it doesn't work properly
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Amit99531 is offline Offline
2 posts
since Aug 2008
Oct 13th, 2008
0

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
Last edited by emotionalone; Oct 13th, 2008 at 5:29 am.
Reputation Points: 10
Solved Threads: 4
Light Poster
emotionalone is offline Offline
33 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: While loop error
Next Thread in C++ Forum Timeline: repetitive work





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC