Thread: Creating a list of words

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    8

    Creating a list of words

    Hi, need some help with this one. Here is the question:

    Compose an algorithm for a program that creates a list of words encountered when reading a series of text files. Write a program to open, and subsequently close, each input file, terminating should an attempt to open any file fail. Derive the file names from the command line.

    I've heard of the "argc" and "argv" features to do this, but i have never used them before and have no idea how they work. Is someone able to point me in the right direction?

    All help is greatly appreicated.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I've heard of the "argc" and "argv" features to do this, but i have never used them before and have no idea how they work. Is someone able to point me in the right direction?
    We have a tutorial/FAQ on the topic.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    32
    What you trying to do create an unique array of words in the files and a count of how many times the word showed up in the files listed?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( int argc, char *argv[] )
    {
        int files = argc-1, i = 0;
        FILE *file;
    
        for(i = 1; i <= files; i++)
        {
                    printf("opening FILE filename = &#37;s\n",argv[i]);
    
                    if ( (file = fopen( argv[i], "r")) == NULL )
                    {
                            fprintf(stderr, "Can't Open file %s\n", argv[i]);
                            exit(0);
                     }
                    fclose( file );
        }
    return 0;
    }

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    1
    Hi, I am working on something similar to this and have more or less the same code as above.

    What i am having difficulty with is alphabetically sorting the text file, i am experimenting with qsort without much sucess.

    Thanks in advance if anyone can help.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    huntley, start your own thread detailing your problem, your attempt at a solution, and how does it not work.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a Sorted Linked List, Help Please!
    By larry_2k4 in forum C Programming
    Replies: 4
    Last Post: 04-28-2009, 01:12 AM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  4. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  5. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM