#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    Scotland
    Posts
    185
    Rep Power
    12

    NEWBIE HELP - Why does this code produce no output???


    I am trying to learn C. I want to take a small text file and if the line begins NUMBER then add the number to a rolling count, if the line begins TEXT then print the line on screen.

    eg

    NUMBER=10
    TEXT=Hi
    NUMBER=3

    I would add the 10 + 3 and print the hi.

    I have the following code:

    #include <stdio.h>
    #include <string.h>

    main()

    {
    char word[50];
    char fChar[10];
    char *pntr;

    /* Get pointer */
    FILE *input_file_pointer;

    /* Open file */
    input_file_pointer = fopen("clist.cfg","r");

    /* if file is not present then stop */
    if (input_file_pointer == NULL)
    {
    printf("Fail\n");
    return 0;

    }
    else

    /*
    While not at the end of file, read each line into a variable
    called WORD.
    */
    while (fgets(word, 50, input_file_pointer) != EOF)
    {
    strncpy(fChar, word, 1); /* copy first char of word to fChar */
    pntr = strchr(word, '=');/* set pointer to first occurance of = within word */
    pntr++; /* move point up 1 space to get rid of = */
    if (fChar == 'T')
    {
    printf("%s", pntr);
    }
    else if (printf == 'N')
    {
    printf("numbertime");
    /* printf("%s\t", fChar); /* print the fChar */
    /* printf("%s", pntr); /* print the pntr */
    }

    }

    fclose(input_file_pointer);
    }

    When I run it I get comparing pointer to integer warnings but that isnt the main problem, the main problem is nothing happens when I run it!

    Can anyone offer a suggestion as to how I messed this up?
  2. #2
  3. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    one thing i noticed was that your pointer arithmetic is incorrect. when you increment a pointer, pntr++ you are actually incrementing it by 4 bytes, the size of a pointer.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    Arkansas
    Posts
    1
    Rep Power
    0
    I'm sorry I don't see exactly what the main problem is. (I'm pretty new at C as well) You might, however, want to change these lines like this:

    /*Old lines*/
    if (input_file_pointer == NULL)
    {
    printf("Fail\n");
    return 0;
    }

    /*New lines*/
    if (input_file_pointer == NULL)
    {
    fprintf(stderr, "Error opening file\n"); /*printing to stderr (usually the moniter) is usually desirable in case our error has affected the stdout display (which in your case I don't think it would but its a good habit nonetheless ) */
    exit (1); /*We want to exit with an error condition*/
    }
    Last edited by gleoetkr; June 22nd, 2003 at 03:42 PM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    Scotland
    Posts
    185
    Rep Power
    12
    Cheers guys, I re-worked it. Still dont know what is happening. The code is at the bottom, however, now I can display everything that is on lines begining with a T, but the numbers do not get added. Take a look.

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>


    main()

    {
    char word[50], *pntr;
    int resCmp, result, midResult;

    FILE *input_file_pointer;

    resCmp = result = midResult = 0;

    input_file_pointer = fopen("clist.cfg","r");

    if (input_file_pointer == NULL)
    {
    printf("Fail\n");

    }
    else

    while (fgets(word, 50, input_file_pointer) != EOF)
    {
    resCmp = memcmp("T",word, 1);
    pntr = strchr(word, '=');
    pntr++;
    if (resCmp != 0)
    {
    midResult = atoi(word);
    result += midResult;
    }
    else
    {
    printf("%s", pntr);
    }

    }

    fclose(input_file_pointer);
    printf("%d", result);
    }
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,242
    Rep Power
    2222
    Originally posted by infamous41md
    one thing i noticed was that your pointer arithmetic is incorrect. when you increment a pointer, pntr++ you are actually incrementing it by 4 bytes, the size of a pointer.
    Actually, in pointer arithmetic you increment by the size of the data type being pointed to, so (data type encoded into pointer names):
    char_ptr++ increments by one byte
    int_ptr++ increments by two bytes (assuming 2-byte ints)
    double_ptr++ increments by 8 bytes
    ptr_ptr++ increments by 4 bytes, the size of a pointer
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,242
    Rep Power
    2222

    Re: NEWBIE HELP - Why does this code produce no output???


    Originally posted by Jadams
    I am trying to learn C. I want to take a small text file and if the line begins NUMBER then add the number to a rolling count, if the line begins TEXT then print the line on screen.

    ...

    if (fChar == 'T')
    {
    printf("%s", pntr);
    }
    else if (printf == 'N')
    {
    printf("numbertime");
    /* printf("%s\t", fChar); /* print the fChar */
    /* printf("%s", pntr); /* print the pntr */
    }

    }

    fclose(input_file_pointer);
    }

    When I run it I get comparing pointer to integer warnings but that isnt the main problem, the main problem is nothing happens when I run it!

    Can anyone offer a suggestion as to how I messed this up?
    That line:
    else if (printf == 'N')
    is the problem.

    printf is the name of a function. When you use a function name with parentheses, then you are calling that function, but without the parentheses you are using the function name as a pointer to that function.

    A slight oddity of C is that it treats character literals as numeric values, commonly int's (see that some library functions that expect or return a character are declared with int instead of char). So your error message was generated because you were trying to compare a pointer (printf) with an int ('N'). Personally, I don't see what you were trying to do there.

    BTW, using a function pointer can be very handy at times, as in creating a jump table or a struct array in which you store the pointer to an item's handler. Not beginner stuff, but you may encounter it later.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    Scotland
    Posts
    185
    Rep Power
    12
    That line:
    else if (printf == 'N')
    is the problem.
    I noticed that after I posted. I don't know what I was thinking there. I re-worked the code and u can see above. Stilll open for suggestions.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    272
    Rep Power
    19
    You are doing the atoi on word rather than pntr.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    Scotland
    Posts
    185
    Rep Power
    12
    Ok That was dumb of me! :o However, if I substitute pntr for word, I get a segmentation fault.

    Anyone care to tell me what one of them is??? :confused:
  18. #10
  19. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,242
    Rep Power
    2222
    Originally posted by Jadams
    However, if I substitute pntr for word, I get a segmentation fault.

    Anyone care to tell me what one of them is??? :confused:
    It could be that pntr is set to NULL, which would definitely get you a segmentation fault.

    You need to add error checking. If strchr does not find the character ('=' in this case), then it returns NULL.

IMN logo majestic logo threadwatch logo seochat tools logo