|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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.
|
|
#3
|
|||
|
|||
|
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 02:42 PM. |
|
#4
|
|||
|
|||
|
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); } |
|
#5
|
||||
|
||||
|
Quote:
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 |
|
#6
|
||||
|
||||
|
Re: NEWBIE HELP - Why does this code produce no output???
Quote:
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. |
|
#7
|
|||
|
|||
|
Quote:
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. |
|
#8
|
|||
|
|||
|
You are doing the atoi on word rather than pntr.
|
|
#9
|
|||
|
|||
|
Ok That was dumb of me!
However, if I substitute pntr for word, I get a segmentation fault.Anyone care to tell me what one of them is??? ![]() |
|
#10
|
||||
|
||||
|
Quote:
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > NEWBIE HELP - Why does this code produce no output??? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|