Thread: Problem with the function parameters when calling a function

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    1

    Problem with the function parameters when calling a function

    When I am trying to call a function, the parameters that I have tried so far have just resulted in the terminal saying that 'too few arguments to function' and what I can tell that it wants written there is the parameters from when it was declared. I have read a few different documents about this and what the calling parameters are and the different between calling by value or by reference but I am still not able to figure out the problem.
    Below is the main section of code that has the call functions in it along with some variables.

    Code:
    int main(int argc, char *argv[])
    {//main()
    
    char *listwords;
    
    processfile(listwords); //<- this is the line that is causing the problem
    
    WordList mylist;
    
    initialiselist(&mylist);
    
    addword(&mylist, createfillednode(listwords));
    
    printlist(&mylist);
    }//main()
    Code:
     //process the file
    void processfile(WordList *wordList, int argc, char *argv[])
    {//process file
        //file pointer
        FILE *f;
        //open the file
        f = fopen(argv[1], "r");
        //check it opened correctly
        if(f == NULL)
        {//if statement
            printf("cannot read file\n");
        }//if statement
    
        fseek(f, 0, SEEK_END);
    
        //declare variables
        char *listwords;
        long size = ftell(f);
        char *token;
    
        //seek beginning of file
        fseek(f, 0, SEEK_SET);
        //set the size of array to the file size
        listwords = (char*)malloc(size+1);
        listwords[size] = '\0';
        //reads the data from the file
        fread(listwords, size, 1, f);
    
        int i;
        for(i=0; (token = strsep(&listwords, " ")); i++)
        {//for loop replace certain characters with spaces
            if (token != ".")
            {
                //pointer from the token to the dictionary
                wordList->token;
            }else if (wordList->token != "!")
            {
                //pointer from the token to the dictionary
                wordList->token;
            }else if (token != "?")
            {
                //pointer from the token to the dictionary
                wordList->token;
            }else if (token != "\"")
            {
                //pointer from the token to the dictionary
                wordList->token;
            }else if (token != ","){
                //pointer from the token to the dictionary
                wordList->token;
            }else
            {
                //pointer from the token to the dictionary
                wordList->token;
            }
            //increment token to the next word
            token++;
        }//for loop replace certain characters with spaces
        fclose(f);
        return;
    }//process file
    I'm not sure if you would need any more code but I will post any more thats necessary. Thank you in advance.
    Last edited by JaneDoor; 02-01-2016 at 10:11 AM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You should post the definition (or at least the prototype) of the "processfile()" function.

    If you're getting a "too few arguments" error, then the function expects more than just the single argument you're passing to it.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Please post the smallest possible complete program that illustrates the problem. The snippet you've shown is not nearly enough to figure out your problem.

    Jim

  4. #4
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    70
    Check the value of the 'argc' in main function

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I didn't see that you edited your post with the requested code.

    Code:
    void processfile(WordList *wordList, int argc, char *argv[])  // the function expects three arguments
    Code:
    processfile(listwords);  // you are passing only one argument to this function
    And, as ArunS pointed out, be sure to check the value of "argc" before attempting to access "argv[1]".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function calling problem
    By johnnish in forum C Programming
    Replies: 5
    Last Post: 08-06-2009, 09:59 AM
  2. problem calling function
    By dudeomanodude in forum C++ Programming
    Replies: 4
    Last Post: 02-05-2008, 03:23 AM
  3. Calling a function pointer with unknown parameters
    By C+/- in forum C++ Programming
    Replies: 10
    Last Post: 06-14-2007, 11:33 AM
  4. Problem calling the function
    By HAssan in forum C Programming
    Replies: 6
    Last Post: 12-30-2005, 04:36 PM
  5. problem with function calling
    By Andystudent in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2004, 06:12 PM