Thread: function pointer not pointing

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    93

    function pointer not pointing

    Hi,

    am I right in thinking that calling p_fp[IN_FILE] will always go to the _start_ of the file ?

    in the snippet below, I am getting a count from LAST_FILE but not from IN_FILE, I have swapped them round just in case and when I do I get 5 0 instead of 0 5 and I should be getting 9 5 or 5 9

    I think I have included everything that is relevant,

    Code:
    #define OPEN_FILE_COUNT  6
    #define IN_FILE          0
    #define OUT_FILE         1
    #define LOG_FILE         2
    #define YEAR_FILE        3
    #define HIST_FILE        4
    #define LAST_FILE        5
    
    
    FILE * p_fp[OPEN_FILE_COUNT];
    
    
    void file_open(char * inp_file, char * last_month, FILE ** fp)
    {
       if(!(fp[IN_FILE] = fopen(inp_file, "r")))
          error_func("In file_open().", "Error : fopen(INPUT_FILE)", SYS | FATAL);
       ... /* same for all files */
    
    
    void check_activity(FILE ** p_fp)
    {
    int countA = 0, countB = 0;
    char ch;
    
       while((ch = fgetc(p_fp[IN_FILE])) != EOF)
          if(ch == '\n')
             countA++;
    
       while((ch = fgetc(p_fp[LAST_FILE])) != EOF)
          if(ch == '\n')
             countB++;
    
       printf("this month, last month %d %d\n", countA, CountB);
       ...


    tia,

  2. #2
    root
    Join Date
    Sep 2003
    Posts
    232
    >FILE * p_fp[OPEN_FILE_COUNT];
    Is this global? If so why do you bother having the fp and p_fp parameters to those two functions?

    >/* same for all files */
    All six? I only see two parameters that could be used as file paths to open.

    >
    Code:
    while((ch = fgetc(p_fp[IN_FILE])) != EOF)
       if(ch == '\n')
          countA++;
    
    while((ch = fgetc(p_fp[LAST_FILE])) != EOF)
       if(ch == '\n')
          countB++;
    Why not:
    Code:
    /* Line Count */
    unsigned long lc(FILE *fd) {
      unsigned long rc;
      int c;
    
      rewind(fd);
      rc = 0;
      while ((c = getc(fd)) != EOF) {
        if (c == '\n')
          rc++;
      }
    
      return rc;
    }
    
    ...
    
    countA = lc(p_fp[IN_FILE]);
    countB = lc(p_fp[LAST_FILE]);
    >am I right in thinking that calling p_fp[IN_FILE] will always go to the _start_ of the file ?
    As long as you've just opened it and done nothing with the loc pointer, or rewound the loc pointer, or done an fseek or fsetpos to the beginning.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    As I mentioned previously, fgetc returns an int.
    Code:
    char ch;
    while((ch = fgetc(p_fp[IN_FILE])) != EOF)
    Do you still have this problem after changing ch to an int?

    Also, you have a "global" p_fp and a parameter p_fp for the function check_activity. Do you know which one hides the other?

    [EDIT]
    "function pointer not pointing"
    A function pointer is not the same as a FILE pointer.
    [/EDIT]
    Last edited by Dave_Sinkula; 09-16-2003 at 03:17 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    93
    twm:
    Dave_Sinkula:

    Hi,

    I was trying not to post the whole code, because it is probably larger than I can fit in one post, either by the rules of the post, the amount of chars or just spamming

    No, FILE ** is not global, it is restricted to int main(), hence the reason for it being passed round the functions, and yes ch is already an int after your last advice.

    IN_FILE and LAST_FILE and all the others have already been opened, and used by other functions before we get here, but as I am not specifically resetting LAST_FILE, I wondered why I could count one and not the other ?


    thanks for your help, as usual

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Replies: 7
    Last Post: 07-04-2007, 12:46 PM
  3. Function Pointer help
    By Skydt in forum C Programming
    Replies: 5
    Last Post: 12-02-2005, 09:13 AM
  4. Glib and file manipulation
    By unixOZ in forum Linux Programming
    Replies: 1
    Last Post: 03-22-2004, 09:39 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM