Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 135,934 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,683 people online right now. Registration is fast and FREE... Join Now!




Searching and Sorting Text Files

 
Reply to this topicStart new topic

Searching and Sorting Text Files

kona_echinofu
10 Apr, 2008 - 09:15 AM
Post #1

New D.I.C Head
*

Joined: 3 Apr, 2008
Posts: 4


My Contributions
Hello everybody,

I was wondering how to search a text file for words, then sort them into an array, ignoring duplicates.

So for example;

"Hello I need help, I need good help"

Would be sorted as

0 Good
1 Hello
2 Help
3 I
4 Need

Help would be much appreciated!
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Searching And Sorting Text Files
10 Apr, 2008 - 10:59 AM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 48 times
Dream Kudos: 550
My Contributions
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Post your code like this: code.gif

Thanks.
User is offlineProfile CardPM
+Quote Post

kona_echinofu
RE: Searching And Sorting Text Files
11 Apr, 2008 - 04:45 AM
Post #3

New D.I.C Head
*

Joined: 3 Apr, 2008
Posts: 4


My Contributions
At the moment, I have only managed to program it to open the file entered by the user then display all contents of the file. Not sorting or filtering it.

I don't know where to start on that! unsure.gif

CODE
#include <stdio.h>

int main()
{
  FILE* fname;
  char filetext[100];
  char list[100];
  char userfile[100];
  char loop[1];
  loop[0] = 'y';
  int Counter;

  while (loop[0] == 'y')
  {
    printf("Please enter the name of the file you wish to open: ");
    scanf("%s", userfile);
    printf("\n\n");
    printf("Now opening %s...\n\n", userfile);
    fname = fopen((userfile), "r");

      if (fname == NULL)
      {
        printf("File could not opened\n");
        printf("\nDo you wish to try and open another file?(y/n)");
        scanf("%s", loop);
        printf("\n\n");
      }
      else
      {
        printf("\n****File opened, now displaying contents...**** \n\n");

          while (!feof(fname))
          {
            fgets(filetext, 70, fname);
        //Gets text from file and temporarily stores in filetext
            printf("%s", filetext);
        //prints whole contents of file
      }
        fclose(fname);
        printf("\n\n****File now closed****\n\n");
        printf("\nDo you wish to open another file?(y/n)");
        scanf("%s", loop);
        printf("\n\n");
      }
  }
  return 0;
}


Does anybody know how to sort and filter the text of the file to only display one occurence of a word and sorting it alphabetically?

Thanks.
User is offlineProfile CardPM
+Quote Post

kona_echinofu
RE: Searching And Sorting Text Files
15 Apr, 2008 - 05:28 PM
Post #4

New D.I.C Head
*

Joined: 3 Apr, 2008
Posts: 4


My Contributions
I have tried singling out words first, before putting them into an array using changing this part of the code

CODE

          while (!feof(fname))
          {
            fgets(filetext, 70,fname);
        //Gets text from file and temporarily stores in filetext
          while (filetext[Counter] != ' ')
            {
          Counter++;
          printf("%s", filetext);
          //print outcome to test if loop is singling out words
            }
            }


Although it Just seems to single the first word out and that word only. Its stuck in a loop producing many replicas of the same 1st word????

Even though I increase the 'Counter' so it skips the space?
User is offlineProfile CardPM
+Quote Post

kona_echinofu
RE: Searching And Sorting Text Files
22 Apr, 2008 - 02:20 PM
Post #5

New D.I.C Head
*

Joined: 3 Apr, 2008
Posts: 4


My Contributions
I have researched a lot into isolating words within strings, storing the words and sorting them.
I have managed to isolate the words, and store them into a struct. But there is a problem within my algorithm as it is only storing the first word of the text file. So for example if the text file held "Hello there" Only the "Hello" is stored in position [0] of the struct, "there" is not stored in position [1] though?

Can somebody help?

CODE

#include <stdio.h>
#include <string.h>
typedef int boolean;
typedef struct word
{
  char word[50];
}Word;

int main(int argc, char* argv[])
{
  FILE* fname;
  char list[1000];
  boolean readw;
  int Counter, i, j;
  Word* words;
  int num_words;

  num_words = 0;

  printf("\n\t\t\t""***Welcome To*** \n\t       Conor's File Opening & Searching Program""\n\n\n\n");
  printf("\n\n");
    for (Counter = 1; Counter < argc; Counter++)  
    {
     fname = fopen(("%s", argv[Counter]), "r");
      if (fname  == NULL)
      {
       printf("****Now opening %s...****\n\n", argv[Counter]);      
       printf("Sorry, the file '%s', could not be found/opened""\n\n", argv[Counter]);
      }
      else
      {
       printf("****Now opening %s...****\n\n", argv[Counter]);      
       printf("\n****File opened, now displaying contents...**** \n\n");
       while(!feof(fname))
       {
         while (fgets(list, 100, fname)!= NULL)
     {
       if (num_words == 0)
       {
        words = calloc(1, sizeof(Word));
       }
       else
       {
        words = realloc(words, (num_words + 1)* sizeof(Word));
       }
         for(i = 0, j = 0; list[i] != ' '; i++, j++)
         {
          words[num_words].word[j] = list[i];
         }
            words[num_words].word[j] = '\0';
           num_words++;
     }
       }
        for (i = 0; i < num_words; i++)
    {
     printf("%s\n", words[i].word);
    }
        if (words != NULL)
    {
     free(words);
     printf("%s's data has been removed from memory", argv[Counter]);
        }
       fclose(fname);
       printf("\n\n****File now closed****\n\n");
      }
    }
return 0;
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:28AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month