Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string to number

Status
Not open for further replies.

sarahnade

Programmer
Dec 22, 2005
49
US
I know where my problem is, but I don't know how to fix it, or why it's happening this way. I am reading input from a file that for some reason reads in everything as strings. So I have to change those strings into numbers. These numbers may, but won't necesarily, be decimals with no more than 15 decimal places. I'm having trouble knowing what to store these numbers as. Float, Long, Double, what's the deal? Here are the important bits of my code:
Code:
char *xllc = malloc(20 * sizeof(char));
char *yllc = malloc(20 * sizeof(char));
fscanf(aFile, "%s\n%s\n", xllc, yllc);

//here's where the trouble is
        long yLC = atof(yllc);
        long xLC = atof(xllc);
Should it be "float yLC = atof(yllc);"?
Is there something else I shold use instead of atof()?
I also don't know what to print it as.
Yup, I'm pretty clueless.

don't panic, I'm mostly harmless
Sarah
 
atof() - convert an ascii string to a float
atoi() - convert an ascii string to an int
atol() - convert an ascii string to a long

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Always use double for floating point numbers (atof() converts text to double, not to float). Use %lf (not %f) format specifier in scanf family function for double argument pointers (but you may use %f to print doubles in printf family functions).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top