作者descent (返無)
看板C_and_CPP
標題Re: [問題] atoi的方法..
時間Fri Sep 28 15:59:41 2012
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
FILE *fptr;
char ch;
fptr=fopen("C01","r");
char string[10]="";
while(fgets(string, 9, fptr)!=NULL)
{
printf("str: %s\n", string);
int num=atoi(string);
printf("%d\n",num);
}
fclose(fptr);
return 0;
}
現在比較建議使用
long int strtol(const char *nptr, char **endptr, int base);
※ 引述《perhot9 (小樹)》之銘言:
: 已經有爬過文 但還是沒辦法字串轉數字...
: #include<stdio.h>
: #include<stdlib.h>
: int main(void){
: FILE *fptr;
: char ch;
: fptr=fopen("C01","r");
: while((ch=fgetc(fptr))!=EOF){
: int num=atoi(ch);
: printf("%d",num);
: }
: fclose(fptr);
: return 0;
: }
: 我的C01是類似於
: 10
: 20
: 30
: 這種資料
: 爬文的文章有說 atoi是char* 不是char
: 但我還是不懂該怎麼改...
: 可以稍微指點迷津嗎!?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.125.239.46
→ MOONRAKER:這是標準方法。 09/28 16:24
推 qsort:strtol之類的,要用到對,也不簡單,要寫wrapper來簡化 09/29 00:18