Hi every1,

is there some1 how could give a code to sort a text file in c.
for example, you write a text in kladblok and you write a code to
to sort the text in alphabet. please if there is some1 who knows how to write the code, it'll be great for me,
thank you

the question is :
a. Given a word, it lists how many times that word appears in the text.
b. List all the words that occur in the text.
c. List all the words from the textfile that alphabetically come.
for example:
a
appel
b
book
like that.

Read the file into a linked list, sort it, then write it back out. That will work for files that are not very large. For really huge files (1+gig) there are file sort programs you can get (see google).

a) Read the words into a linked list of structures. If the word is already in the list then just increment the structure's counter variable member. If the word is not in the list, create a new structure and add it to the list in sorted order or just to the end of the list.

struct word
{
    char* sword;
    int count;
};

b) just iterate through the linked list and print out the words

c) If you add new nodes in sorted order then this option is the same as the previous one. Otherwise if you just added new words to the end of the list then you will have to sort the list first.

Hi Ancient Dragon,
thanx for your reaction.
i wrote the code of A,

#include <stdio.h>
#include <stdlib.h>



FILE * pFile;

int main(void)
{
	char c;

	// Open the books.xml file
	pFile = fopen("output.txt", "r");

	// If this fails it will return NULL and thus
	if (pFile == NULL) {
		printf("File could not be opened.\n");
	}
	// otherwise continue
	else
	{
		printf("File opened successfully.\n");

		while(c != EOF)
		{
			c = fgetc(pFile);

			putchar(c);
		}
	}

	// Always close the file
	fclose(pFile);

	system("PAUSE");
	
	// Return a 0 to shut down the program
	return 0;
}

en now how could i add the struct word to my code?
thanx a lot for your help

I have wrote the code but i have still one thing,
how to sort the words on alphabet,
en that is what i wrote:

#include <stdio.h>
#include <string.h>

/* Some defines used by the application */
#define FILE_NAME		"test.txt"
#define WORDS_AMMOUNT	10000
#define WORDS_LENGTH	50

/* 
	This is the structure, which holds the words 
	
	_word_ is the array of characters that holds
	each string.
	_ammount_ holds the ammount of occurrences of
	the string.
*/
typedef struct
{
	char word[WORDS_LENGTH];
	int  ammount;
} WORDS;

/* Fills an array of characters with NUL (\0) */
void clearString(char *string)
{
	memset(string, '\0', sizeof(string));
}

/* Initializes the list of words */
void initialize(WORDS *list, int size)
{
	int x;
	for(x = 0; x < size; x++)
	{
		clearString(list[x].word);
		list[x].ammount = 1;
	}
}

/* 
	Check if the specific string (needle) is in the
	array (haystack) - the array is a fixed size (ammount).
*/
int inArray(WORDS *haystack, char *needle, int ammount)
{
	int i;
	for(i = 0; i < ammount; i++)
		if(strcmp(haystack[i].word, needle) == 0) /* We found it, return index-position */
			return i;
	return -1; /* We didn't find anything, return not-true */
}

int main()
{
	FILE  *fileHandle;
	WORDS  wordList[WORDS_AMMOUNT];
	char   string[WORDS_LENGTH];
	int    stringCount  = 0;
	int    listCount    = 0;
	int    listIndex, i;
	
	/* Initialize list and clear string */
	initialize(wordList, WORDS_AMMOUNT);
	clearString(string);
	
	/* Create file stream */
	fileHandle = fopen(FILE_NAME, "r");
	do
	{
		/* We get one character from the filestream into the string */
		string[stringCount] = getc(fileHandle);
		
		/* Check if the counter is at a space or newline, ... */
		if(string[stringCount] == 32 || string[stringCount] == 10)
		{
			/* ... if it is, clear that character. */
			string[stringCount] = '\0';
			
			/* Then check if the words already is in the list */
			if((listIndex = inArray(wordList, string, listCount)) != -1)
				wordList[listIndex].ammount++; /* if it is, increase the occurrence of the word... */
			else
			{
				/* else insert the word and increase the counter of the list */
				strcpy(wordList[listCount].word, string);
				listCount++;
			}
			
			/* When we are finished using the string we're clearing it, and set the counter to zero */
			clearString(string);
			stringCount = 0;
		}
		else
			stringCount++;
	} while(string[stringCount-1] != EOF);
	
	/* Close the file stream */
	fclose(fileHandle);
	
	/* Print the result to the user */
	for(i = 0; i < listCount; i++)
	{
		printf("WORD #%d\n", i);
		printf(" - Word:          %s\n", wordList[i].word);
		printf(" - Times occured: %d\n", wordList[i].ammount);
		printf("\n");
	}
 getchar();
	return 0;
}
void clearString(char *string)
{
memset(string, '\0', sizeof(string));
}

That will not work because sizeof( any pointer here ) always = 4 with 32-bit compilers. To erase a character array all you have to do is set the first character to 0.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.