Thread: Command Line Arguements

  1. #16
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    what am I doing wrong here? Can somebody help me figure it out? I'm grateful for everything I've been shown thus far, I wish I would have known about this site sooner.
    Code:
    void openFile(char *pFPIn, int argc, char strArray[][MAXLEN])
    {
     int i;
     
     if (argc !=  3) 
       printf("\nNot enough Command Line arguments.");      
      else if (argv[2] < 1 || argv[2]  > 15)
       printf("\nNumber %d is not between 1 and 15.", argv[1]);
     
     if((*pFPIN = fopen(argv[2], "r")) !=NULL)
      {
        for (i = 0; i < argv[2]; i++)
        fscanf(*pFPIN, "%s", strArray[i]); 
       
      }
      
     
    }

  2. #17
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    fscanf(*pFPIN, "%s", &strArray[i]);
    You've dereferenced a pointer to the file. So you end up passing the wrong thing to fscanf: just send it the pointer.
    Also notice my addition of the address-of operator.

    Code:
    if (argc !=  3) 
       printf("\nNot enough Command Line arguments.");      
    else if (argv[2] < 1 || argv[2]  > 15)
       printf("\nNumber %d is not between 1 and 15.", argv[1]);
    Do this stuff in main() especially because you didn't pass the argv[] array to openFile()

  3. #18
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Code:
    #include <stdio.h> 
    #include <ctype.h>
    #include <string.h>
     
     
    #define MAXWORD 15
    #define MAXLEN 20
    
     
    
    void printName (void);
    void sort(char strArray[][MAXLEN], int currentsize);
    int binSrch(char strArray[][MAXLEN], char *item,
                int lowerbound, int upperbound);
    void printArray(char strArray[][MAXLEN], int currentsize);
    void openFile(char *pFPIN, char strArray[][MAXLEN]);
    int srch(char srtArray[][MAXLEN], int *item);
    int getValue(void);
     
    
    int main (int argc, char *argv[])
    {
     char strArray[MAXWORD][MAXLEN];
     int item, result;
     int currentsize;
    
     printName(void);   
     
      if (argv[2] !=  3)
       printf("\nNot enough Command Line arguments.");
      else if (argv[2] < 1 || argv[2]  > 15)
       printf("\nNumber %d is not between 1 and 15.", argv[1]);
    
     openFile(argv[2], *strArray);
    
     return 0;
    }
    
    
    
    
    
    void openFile(char *pFPIN, char strArray[][MAXLEN])
    {
     if((*pFPIN = fopen(argv[2], "r")) !=NULL)
      {
        for (i = 0; i < argv[2]; i++)
        fscanf(*pFPIN, "%s", &strArray[i]);
     
      }
       
      
    }
    What else do I need to add/modify to get this thing to work (just read in the strings), I'm still confused as to how to make all of this work... Everytime I think I get the hang of it, it throws me for another loop.
    The code above is just the function definition for the openFile code, as well as the main( ) code.
    What else needs to be done?

  4. #19
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    if (argv[2] !=  3)
       printf("\nNot enough Command Line arguments.");
      else if (argv[2] < 1 || argv[2]  > 15)
    1. if(argc != 3)
    2. argv[2] is a string, if you think it is a number (actually as I undestand it should be argv[1]) - you should convert string to number using for example strtol before comparing it to 1 and 15
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Command line arguements in functions.
    By SlyMaelstrom in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2005, 07:41 AM
  2. Comparing command line arguements to a string.
    By SlyMaelstrom in forum C++ Programming
    Replies: 14
    Last Post: 10-30-2005, 04:56 AM
  3. command line arguements
    By screamer903 in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 07:59 AM
  4. Arguements for gnome?
    By mart_man00 in forum Tech Board
    Replies: 12
    Last Post: 07-30-2003, 04:46 PM
  5. Command Line Arguements
    By scaven in forum C Programming
    Replies: 2
    Last Post: 04-13-2003, 05:36 PM