| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
alphabetically sort a file in c++
i have a file of company names and i need to sort them alphabetically. i am using borland c++ 5.1.
thanks for the help gonzopb |
|
#2
|
|||
|
|||
|
using an array-name[], of length-length
for (int i =0;i<length<i++){ for (int j=0; j<length-1-i; j++){ if name[j]>name[j+1]{ swap the names; }//end if }//end for j }//end for i I think that works, been years since i've done it. |
|
#3
|
|||
|
|||
|
Binary tree implementation
Got bored so knocked up a binary tree version of this as an alternative.
Hope it helps someone, -KM- #include <stdio.h> #include <stdlib.h> #include <string.h> #define LINE_LEN 128 typedef struct T { char *data; struct T *left; struct T *right; } node; node *makeNode (char *data) { node *temp = (node *) malloc (sizeof (node)); temp->data = calloc (LINE_LEN, sizeof (char)); strcpy (temp->data, data); temp->left = NULL; temp->right = NULL; return temp; } node *addNode (char *line, node *root) { if (root == NULL) return makeNode (line); if (strcmp (line, root->data) < 0) root->left = addNode (line, root->left); else root->right = addNode (line, root->right); return root; } node *readFile (FILE *in_fp) { node *root = NULL; char *line = (char *) calloc (LINE_LEN, sizeof (char)); while (!feof (in_fp)) { fscanf (in_fp, "%s", line); root = addNode (line, root); } return root; } void writeFile (node *root, FILE *out_fp) { if (root->left != NULL) writeFile (root->left, out_fp); fprintf (out_fp, "%s\n", root->data); if (root->right != NULL) writeFile (root->right, out_fp); } void sort (char *in, char *out) { FILE *in_fp = fopen (in, "r"); FILE *out_fp = fopen (out, "w"); node *root = readFile (in_fp); writeFile (root, out_fp); fclose (in_fp); fclose (out_fp); } int main (int argc, char **argv) { if (argc != 3) printf ("Usage: sort unsorted_filename sorted_filename\n"); else sort (argv [1], argv [2]); return 0; } |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > alphabetically sort a file in c++ |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|