C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 22nd, 2003, 12:08 PM
Jadams Jadams is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Scotland
Posts: 185 Jadams User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 40 m 14 sec
Reputation Power: 6
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?

Reply With Quote
  #2  
Old June 22nd, 2003, 01:47 PM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 26
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.

Reply With Quote
  #3  
Old June 22nd, 2003, 02:40 PM
gleoetkr gleoetkr is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Arkansas
Posts: 1 gleoetkr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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 02:42 PM.

Reply With Quote
  #4  
Old June 22nd, 2003, 02:53 PM
Jadams Jadams is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Scotland
Posts: 185 Jadams User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 40 m 14 sec
Reputation Power: 6
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);
}

Reply With Quote
  #5  
Old June 22nd, 2003, 03:03 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,837 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 10 h 28 m 40 sec
Reputation Power: 446
Quote:
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

Reply With Quote
  #6  
Old June 22nd, 2003, 03:12 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,837 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 10 h 28 m 40 sec
Reputation Power: 446
Re: NEWBIE HELP - Why does this code produce no output???

Quote:
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.

Reply With Quote
  #7  
Old June 22nd, 2003, 04:26 PM
Jadams Jadams is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Scotland
Posts: 185 Jadams User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 40 m 14 sec
Reputation Power: 6
Quote:
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.

Reply With Quote
  #8  
Old June 22nd, 2003, 06:13 PM
3dfxMM 3dfxMM is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 266 3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 19 h 59 m 30 sec
Reputation Power: 13
You are doing the atoi on word rather than pntr.

Reply With Quote
  #9  
Old June 23rd, 2003, 02:24 AM
Jadams Jadams is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Scotland
Posts: 185 Jadams User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 40 m 14 sec
Reputation Power: 6
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???

Reply With Quote
  #10  
Old June 23rd, 2003, 09:36 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,837 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 10 h 28 m 40 sec
Reputation Power: 446
Quote:
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???

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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > NEWBIE HELP - Why does this code produce no output???


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway