while(fgets(line, 100, file)!=NULL) { /* keep looping until NULL pointer... */
"I have detected danger Will Robinson"
More specifically I have detected that line was declared char line[20]; so is a 20 byte array but you have passed a pointer to line and told fgets to copy up to 100 characters into it leaving yourself open to a buffer overrun. For safety this should be
while(fgets(line, sizeof line, file)!=NULL) { /* keep looping until NULL pointer... */
And again
numbers[i]=atoi(line); /* convert string to int */
but numbers is declared double numbers[]={0}; with no array size but an initaliser list with a single entry means that numbers has a size of 1 so as soon as i > 0 you have an out of bounds array access.
Additionally your file appears to contain floating point values and you are using double type to store your converted value but you are calling int atoi ( const char * str ); which returns int and explains many of your conversion errors. You should try calling double atof ( const char * str ); .
To get your numbers array the right size you are going to have to do something using dynamic memory allocation with malloc/realloc.
Banfa
Practically a Master Poster
600 posts since Mar 2010
Reputation Points: 486
Solved Threads: 92
what Banfa said about your character array's size and the number of characters read by fgets() must match.
but you don't necessarily have to allocate your "numbers" array dynamically. you can, for simplicity's sake, just declare it to be some arbitrarily large size.
double numbers[100];
since these are "doubles" (double floating point) you can not use "atoi" but must use "atod". you must also use the format specifier "%f" when printing.
numbers[i]=atod(line); /* convert string to double float*/
you also can not use "sizeof(numbers)" in your for loop like you think you can, for two reasons. first reason is that (at least with a static array size) the number of elements used will vary. the second reason, even if you were to dynamically allocate the array, is that the size of the array is equal to the number of bytes used and not the number of values contained. each double value uses at least 8 bytes.
so if you were to dynamically allocate your array size with malloc, you could use sizeof to determine the number of elements if you divide the array size by the size of each element sizeof(numbers)/sizeof(double) , but i think a simpler way is to just use the count of the number of elements that you read, since you are already counting them anyhow
totalNums = i;
for (i=0 ; i<totalNums ; i++) {
printf("\nThe number %d of %d is: %f", i+1, totalNums, numbers[i]);
}
.
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
Dear Banfa and Jephthah,
Thank you so much for the useful answers.
I made the changes except :
because when I change atoi to atod, it shows:
With atoi it runs well and shows integers in the output but I need to store double floating numbers in the array.
When you change your code from doing the wrong thing to doing the right thing and you get errors in compilation the correct course of action is not to change back to doing the wrong thing but to understand and fix the source of the errors.
You do not need line 7 of your lastest listing as atof is declared in stdlib.h. In fact it is very poor style to re-declare a standard library function like this.
If you want to convert floating point numbers you will need to call atof.
Banfa
Practically a Master Poster
600 posts since Mar 2010
Reputation Points: 486
Solved Threads: 92
Post the code, it sounds like you plunked in an unnecessary prototype again.
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
Post the code, it sounds like you plunked in an unnecessary prototype again.
Or not, but it was a decent guess...
Ok. Well, there's been discussion of atod but I find no such function out there (I may be ignorant of some bit of C lore here). atof should do the job for you (from cplusplus.com "On success, the function returns the converted floating point number as a double value.")
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
Then write your own atod() . Conversion functions are simple since you have the numbers right there in your string.
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
I also could not find info about atod. It seems that there is not such a function in stdlib.h.
atof does the job but the precision is up to 6 digits after point(i.e. up to 0.123456 is shown and longer digits are rounded up: 0.123457).
No it should give 15 digits of precision or 14 after the point assuming 1 before the point.
May be this is the cause of the confusion, atof returns double not float so there is no atod as it would be superfluous.
Banfa
Practically a Master Poster
600 posts since Mar 2010
Reputation Points: 486
Solved Threads: 92