Thread: Passing My Counters

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    32

    Angry Passing My Counters

    ok im still going on my "read from many files in one function" but with another annoying problem (arent they all annoying!). anyway what i want to do is to read in a word, then increment my passed in counter (it has to be passed cos there is 3 files and 3 arrays to read to, each with different amount of lines).

    basically i have no compile errors, but once i 'leave' the function the count does not go into the global variable but just resets to 0 again for the next time.

    i think i need to make it equal the global variable or something and not just plus 1 everytime and do nothing to it.

    anyway plz have a look.

    DECLARATIONS:
    Code:
    FILE *file = NULL;
    char comp_street_name[MAX_SIZE][STRING_MAX_SIZE];
    char comp_street_type[MAX_SIZE][STRING_MAX_SIZE];
    char comp_suburb_name[MAX_SIZE][STRING_MAX_SIZE];
    int st_name = 0, st_type = 0, su_name = 0;
    void readFile(char *read_file, char read_to[][STRING_MAX_SIZE], int count);
    FUNCTION CALL:
    Code:
    readFile("street_name.txt", comp_street_name, st_name);
    readFile("suburb_name.txt", comp_suburb_name, su_name);
    readFile("street_type.txt", comp_street_type, st_type);
    FUNCTION:
    Code:
    void readFile(char *read_file, char read_to[][STRING_MAX_SIZE], int count) /* Define readFile function */
    {                                                    /* Start readFile function */
       char z = 0;                                       /* Declare temporary read in variable */
       int i = 0, j = 0, eof_test = FALSE;               /* Declare temporary counting variables */
       file = fopen(read_file, "r");                     /* Open source file for reading using read parameter */
       if(file == NULL)                                  /* Check if read file is corrupted */
       {                                                 /* Start if statement */
           printf("Cannot open input file, please check the filename try again.\n"); /* Print error message */
           printf("Input files should be named: street_name.txt, street_type.txt, & suburb_name.txt.\n\n"); /* Print error message */
           exit(1);                                      /* Exit due to read file error */
       }                                                 /* End if statement */
       while(eof_test == FALSE)                          /* Continue read loop while end of file is not found */
       {                                                 /* Start while loop */
          for(i = 0; i < MAX_SIZE; i++)                  /* Run loop until maximum address book size */
          {                                              /* Start for loop */
             for (j = 0; j < STRING_MAX_SIZE; j++)       /* Run loop until maximum string size */
             {                                           /* Start for loop */
                z = fgetc(file);                         /* Gather character from file to temporary variable */
                if(z == '\n')                            /* Check if read in character is the end of line */
                {                                        /* Start if statement */
                   read_to[i][j] = (char) NULL;          /* Place null in data array field if end of line */
                   break;                                /* Break from for loop for new line */
                }                                        /* End if statement */
                else if(z == EOF)                        /* Check if read character is the end of file */
                {                                        /* Start if else statement */
                   eof_test = TRUE;                      /* Set end of file tester to true */
                   break;                                /* Break from for loop if true */
                }                                        /* End else if statement */
                else                                     /* Else write the read character to data field */
                {                                        /* Start else statement */
                   read_to[i][j] = z;                    /* Move read in character into array */
                }                                        /* End else statement */
             }                                           /* End for loop */
             if(eof_test == TRUE)                        /* Check if end of file has been found */
             {                                           /* Start if statement */
                break;                                   /* Break from for loop if true */
             }                                           /* End if statement */
             count++;
          }                                              /* End for loop */
       }                                                 /* End while loop */
       fclose(file);                                     /* Close read in checker file */
    }
    any help would be greatly appreciated. thanks in advance.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    you need to pass by reference

    Code:
    main()
    {
    int *counter;
    }
    
    function(int *counter)
    {
       *counter++;
    }
    Last edited by misplaced; 03-26-2005 at 12:17 AM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    If I understand what you are trying to do with count properly. You did not declare count as a global variable,

    Code:
    void readFile(char *read_file, char read_to[][STRING_MAX_SIZE], int count);
    Is only a function prototype and the count variable is just local to that readFile function thus loses its scope once the function finishes. So you can either declare it as static or just place an 'int count' inside your declarations. So something like this maybe what you are looking for:

    Code:
    FILE *file = NULL;
    int count;
    char comp_street_name[MAX_SIZE][STRING_MAX_SIZE];
    char comp_street_type[MAX_SIZE][STRING_MAX_SIZE];
    char comp_suburb_name[MAX_SIZE][STRING_MAX_SIZE];
    int st_name = 0, st_type = 0, su_name = 0;
    void readFile(char *read_file, char read_to[][STRING_MAX_SIZE]);
    If you have define a 'int count' in your readFile prototype then the readFile function would use the local variable as opposed to the global one. So you need to remove it like I done above.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    32
    errrr, what more can i write? i want to pass in the global counter (st_name) to the function and plus one everytime i read a new line (the outside for loop) - count++. then once i have found the end of file, i want to break from the for loop, keep the count, and finish the while loop.

    the problem occurs when i leave the function the count value is not passed into the globals (st_name), and is reset for the next time.

    is there a problem with the way im passing the variable, or where i dont pass it back properly, or what?

    plz check the code in the first post, and help me PLEASE.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    try a something absurd like

    st_name = some value

    that seems kind of wierd, but ussually when i want to change the value of something, i use the '=' sign.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    32
    DONT WORRY EVERYONE, thanx to 0rion for his help in scoping. i got it. thanx so much

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    In the case you want to modify different variables for different calls then I would suggest using pass by reference as misplaced said earlier. Something like these lines:

    Code:
    FILE *file = NULL;
    char comp_street_name[MAX_SIZE][STRING_MAX_SIZE];
    char comp_street_type[MAX_SIZE][STRING_MAX_SIZE];
    char comp_suburb_name[MAX_SIZE][STRING_MAX_SIZE];
    int st_name = 0, st_type = 0, su_name = 0;
    void readFile(char *read_file, char read_to[][STRING_MAX_SIZE], int* count);
    And to call the function simply add in the ampersand-& operator:

    Code:
    readFile("street_name.txt", comp_street_name, &st_name);
    readFile("suburb_name.txt", comp_suburb_name, &su_name);
    readFile("street_type.txt", comp_street_type, &st_type);

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    32
    thanx everyone, i've worked it out now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newb Question on Passing Objects as Parameters
    By Mariano L Gappa in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2006, 01:08 PM
  2. Passing counters in functions?
    By xkrja in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2006, 03:03 AM
  3. Passing by reference not always the best
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2005, 07:08 PM
  4. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  5. passing counters between function
    By BungleSpice in forum C Programming
    Replies: 18
    Last Post: 02-21-2004, 06:16 PM