| | |
how can we sort data from text file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2006
Posts: 1
Reputation:
Solved Threads: 0
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
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
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.
•
•
Join Date: May 2004
Posts: 178
Reputation:
Solved Threads: 10
IF you're on unix:
will sort the file the way you want - outside C++.
C++ Syntax (Toggle Plain Text)
sort -t ',' k- 1,1 -o newfile.csv oldfile.csv
•
•
Join Date: Aug 2008
Posts: 2
Reputation:
Solved Threads: 0
i have made following programe but it doesn't work properly
C++ Syntax (Toggle Plain Text)
#include<fstream.h> #include<conio.h> #include<stdio.h> #include<string.h> #include<stdlib.h> struct ini{ char itmna[35]; int itmcd,qty; float prc; }; class ret { ini in; int cd[20],qty1[20]; float prc1[20],tot; char itmna1[20][35]; public: void add() {char ch='y'; ofstream q("initial.dat",ios::app); do { cout<<"\nEnter the item code: "; cin>>in.itmcd; cout<<"\nEnter item name: "; gets(in.itmna); cout<<"\nEnter the quantity: "; cin>>in.qty; cout<<"\nEnter it's unit price: "; cin>>in.prc; q.write((char*)&in,sizeof(in)); cout<<"\nWant to add more items(y\n): "; cin>>ch; }while(ch=='y'||ch=='Y'); clrscr(); q.close(); } void purch() { char ch='y'; int i1=0,j,f; ofstream ou("initial.dat",ios::app); ifstream i("initial.dat"); do { cout<<"\nEnter the item code: "; cin>>cd[i1]; cout<<"\nEnter the quantity: "; cin>>qty1[i1]; while(i) { if(in.itmcd==cd[i1]) { i.read((char*)&in,sizeof(in)); strcpy(itmna1[i1],in.itmna); prc1[i1]=in.prc; if(in.qty<qty1[i1]) { cout<<"\nStock not available:"; } else { in.prc-=prc1[i1]; ou.write((char*)&in,sizeof(in)); } break; } else {cout<<"\nNot available!!!!!"; break; } cout<<"\nPurchase more item(y\n): "; cin>>ch; i1++; } }while(ch=='y'||ch=='Y'); clrscr(); gotoxy(4,1); cout<<"****BILL****"; gotoxy(2,2); cout<<"Item code:\tItem name:\tM.R.P\tQuantity:\tTotal"; for(j=0;j<=i1;j++) { gotoxy(2,j+3); cout<<cd[j]<<"\t\t"<<itmna1[j]<<"\t\t"<<prc1[j]<<"\t"<<qty1[j]<<"\t"<<prc1[j]*qty1[j]; tot+=(qty1[j]*prc1[j]); } gotoxy(2,i1+4); cout<<"TOTAL:\t\t\t\t\t\t\t\t"<<tot; i.close(); ou.close(); } }s; void main() { clrscr(); s.add(); s.purch(); getch(); }
Last edited by Narue; Oct 13th, 2008 at 9:27 am. Reason: added code tags
![]() |
Similar Threads
- connect to text file database (Visual Basic 4 / 5 / 6)
- writing students data to a text file (Assembly)
- getting data from a text file and putting it in an excel file using visual basic 6.0 (Visual Basic 4 / 5 / 6)
- Read comma separated data from a text file (C)
- Help Please, how do i read from text file into array? (Visual Basic 4 / 5 / 6)
- How to select data frm text file based on a condition (C)
- conversion of text file into data base file (Visual Basic 4 / 5 / 6)
- Need Help in Reading characters from a text file (C++)
- Problems of looping in saving to a text file (C)
Other Threads in the C++ Forum
- Previous Thread: Sum of Positive and Negative numbers
- Next Thread: repetitive work
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






