I Want to create the function that
reads unsorted informtions from one text file
and sort them based on ID
and then put the sorted informations in that first opened file
but I dont know ho to do this !!
for example :this information are in the file
ID names // this line is not in the file
:::::::::::::::::::::::::::::::::::::::::::::::::::
555 * rahmat
642 * david
214 * john
321 * criss
:::::::::::::::::::::::::::::::::::::::::::::::::::
I want this result from function !!
and I want clean Previous information form the file
and I want put this sorted informations in file
214 * john
321 * criss
555 * rahmat
642 * david
:::::::::::::::::::::::::::::::::::::::::::::::::::
any one can help .....????
thank you.
2. I want clean Previous information form the file
You can read all information in the file, then close the file handle and use the same name to open an ofstream handle(the idea is to open in "w" mode, instead of "a" or "w+"mode), this will clear the existing file and get you ready to write.
3. I want put this sorted informations in file
Use a loop to run through all of the IDs, and write them to the file created on Step2.
void bubble_sort(/*your array or vector of strings - passed as reference*/)
{
//for_loop - based on vector size; ensure there are two comparable strings left!
//compare string[x] and string[x+1];
//If less; do nothing;
//If greater; copy first string to temp, copy second string to first, then copy the temp string to second string.
//repeat loop till there is only one string left
}
int main()
{
//initialize variables and file
//use a mean, to read data from file; I would suggest getline(cin,str_temp) embedded in a while-loop till eof.
bubble_sort(/*your array or vector of strings*/)
//write data back to file; ensure to overwrite with "w"
return 0;
}
You may want to work on one step(comment) at a time.