Thread: Function input arguments

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    5

    Question Function input arguments

    I have an assignment for one of my classes in which we were given a few files to modify to create a program tokenizing text files and inserting the words into a linked list. One of the functions they gave us is this:

    Code:
    void tokenize_file(FILE *file, list_t *list)
    {
        char *word;
        char buf[101];
        buf[100] = 0;
    
        while (!feof(file)) {
    	/* Skip non-letters */
    	fscanf(file, "%*[^a-zA-Z0-9'_]");
    	/* Scan up to 100 letters */
    	if (fscanf(file, "%100[a-zA-Z0-9'_]", buf) == 1) {
    	    word = strdup(buf);
    	    if (word == NULL)
    		fatal_error("out of memory");
    	    list_addlast(list, word);
    	}
        }
    }
    What I am having problems with is to give the function the proper input. I am totally new in C programming and all the pointers etc are throwing me a bit off course here. I tried reading the FAQ, but it didn't help all that much with this specific problem.

    What do I have to put in the main function to get this function to run as it should?
    Edit: We are supposed to take input (name of the text file(s) to tokenize) from the command line

    Thanks,
    Jonnar
    Last edited by Jonnar; 01-22-2007 at 06:31 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you should read about fopen

    and about NOT using feof to control loops

    also I hope you free the allocated memory, when you remove word from the list
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    5
    Code:
    char *file = argv;
    char *mode = "r";
    FILE *read = fopen ( file, mode );
    Ok, now the errors and warnings disappeared, so I am assuming the file is now open.

    Next is my call on the function "tokenize_file".
    It is supposed to look like this when I call it:
    Code:
    void tokenize_file(FILE *file, list_t *list)
    But I have only got this so far, where read is the file I opened earlier:
    Code:
    tokenize_file(read);
    How do I use a list as an argument?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Jonnar
    Code:
    char *file = argv;
    char *mode = "r";
    FILE *read = fopen ( file, mode );
    Ok, now the errors and warnings disappeared, so I am assuming the file is now open.
    You should't. You should check the return value of fopen to be sure that file is opened

    How do I use a list as an argument?
    It depends on the processing of the list.
    Probably - you should create variable of type list_t, initialize it in some way and pass pointer to this var
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    5
    I still haven't figured out how to call the function
    Code:
    tokenize_file(FILE *file, list_t *list)
    in a proper way.

    My data structure looks like this (double-linked list):
    Code:
    struct node {
    	struct node *next;
    	struct node *prev;
    	char word[100];
    };
    
    typedef struct node node_t;
    I have opened a file, using
    Code:
    FILE *inputFile = fopen ( file, mode );
    but how do I give the function the proper input? I keep getting "parse error before ..."

    So my question: How do I give the function tokenize_file() the input it's supposed to get using my data structure (linked list) and a file?

  6. #6
    Registered User simguy's Avatar
    Join Date
    Jan 2007
    Location
    Dallas-Ft Worth, TX
    Posts
    10
    BTW, your statement of "buf[100] = 0;" will only set the 100th member of the array to zero. Look at the function "memset" to intialize an array.

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    5
    Quote Originally Posted by simguy
    BTW, your statement of "buf[100] = 0;" will only set the 100th member of the array to zero. Look at the function "memset" to intialize an array.
    I know. Unless the teachers made a faulty code it's supposed to be that way. I'm sure they have their reasons...

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > while (!feof(file))
    If your teacher also told you to do this, point them to our FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Jan 2007
    Posts
    5
    Quote Originally Posted by Salem
    > while (!feof(file))
    If your teacher also told you to do this, point them to our FAQ
    The whole function tokenize_file was provided by them... They also told us that char pointers and arrays are the same thing, which the FAQ also tells us is a no-no...

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >They also told us that char pointers and arrays are the same thing
    Time to find some new teachers. The ones you have don't know C from a sack of steamed potatoes.
    My best code is written with the delete key.

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, at least the function works. If fscanf doesn't do anything horrible if it is called after EOF, using feof to control the loop doesn't hurt either. See, we don't process the (empty) data that we'll read because of it.

    buf[100] = 0; looks kinda stupid - fscanf inserts the null-terminator for you, otherwise any of this wouldn't work - but may-be it's a force of habit?

  12. #12
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    void tokenize_file(FILE *file, list_t *list)
    You could find out the actual paramter for this function from its prototype. From the above statment you can see that it is taking a file pointer which is nothing but the pointer to a file, i.e the return value of fopen which you have done before.

    And the second is the address of the head node. In the your case the structure has been typedef to list_t. So some where in the main you have to declare the list pointer of type list_t.

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Custom Made Safe Input Function
    By Beast() in forum C Programming
    Replies: 6
    Last Post: 08-21-2004, 10:19 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM