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 February 19th, 2003, 02:07 AM
cel cel is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 5 cel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy file reading

I am reading in a file using getc using this while loop:

while( ((ch = getc(fileIn)) != EOF) && (ch != ']') )

ch is defined as an integer(to avoid problems with conversion from integer->char->integer). My problem is that later in the loop, I use atoi(ch) which wants a pointer to a char. If I cast ch to a pointer inside atoi -> atoi( (char *)ch ), I recieve a segmentation fault. However if I remove explicit casting -> atoi(&ch), I recieve a warning from the compiler, but I recieve no segmentation fault(on my machine at least). How do I remedy this problem?
Thanks in advance.

Last edited by cel : February 19th, 2003 at 02:31 AM.

Reply With Quote
  #2  
Old February 19th, 2003, 10:25 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,861 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 22 h 29 m 29 sec
Reputation Power: 462
OK, real quickly. atoi() is expecting a character string, an array of characters. Character strings in C are null-terminated, which means that they end with '\0', AKA "null" which has an ASCII value of zero. The need for the null-termination is why you always need to allocation one extra character for your strings.

atoi() scans the string until it reaches a null (or a non-digit character, I believe). If it doesn't encounter one, then it just keeps on going, which could take it outside its memory segment, hence your segmentation error.

(I'm composing this on-line and so I cannot refer back to your message) I believe you tried casting your character value to a character pointer, which has it interpreted as an address which undoubtedly points outside its memory segment, which would again lead to a segmentation error.

Here's a suggestion:
The ASCII values for '0' through '9' are 48 through 57 decimal or 0x30 through 0x39. Subtracting 48 from the character will give you its value. Also, ANDing 0x0F to the character will also give you its value:

int int_val;
int_val = ch - 48;
int_val = ch & 0x0F;

Hope that helps.

Reply With Quote
  #3  
Old February 19th, 2003, 10:59 AM
cel cel is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 5 cel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks for your help, dwise1_aol. I don not know why I did not think of that. (Actually ch is a integer, so I need to AND 0x0000000F to ch, correct?)

Reply With Quote
  #4  
Old February 19th, 2003, 02:14 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,861 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 22 h 29 m 29 sec
Reputation Power: 462
Quote:
Originally posted by cel
Thanks for your help, dwise1_aol. I don not know why I did not think of that. (Actually ch is a integer, so I need to AND 0x0000000F to ch, correct?)


Yes, so subtracting the ASCII value of '0' would be the simpler way to go.

Even simpler and more portable (and more C-ish):

int_value = ch - '0';

That way, you won't need to know the ASCII code for '0', plus it should work in any other character-code system, like EBCDIC.

Now, if you have multiple digits that you want to convert to their numeric value -- eg, "42" -- , then atoi() would be the way to go. Just copy the characters into a char array, add a null ('\0') and pass the array name to atoi(). Otherwise, you could construct the integer value yourself, multiplying the previous value by ten and then adding the new digit.

Reply With Quote
  #5  
Old February 19th, 2003, 03:09 PM
cel cel is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 5 cel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thank you.
That is what I was looking for.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > file reading


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 1 hosted by Hostway
Stay green...Green IT